无法通过GCM发送PUSH通知
问题描述:
我的一些代码在我的本地主机上正常工作,但是当我将它全部移到我的服务器时,它似乎无法工作。无法通过GCM发送PUSH通知
我的服务器在防火墙后面,所以我决定使用PHP脚本来使用GCM。再次,我测试了所有本地主机,它的工作原理,但在我的服务器上,没有任何东西被发送。
这里是我的PHP脚本:
<?php
include('tools.php');
// Replace with real BROWSER API key from Google APIs
$apiKey = "xxxx";
// Replace with real client registration IDs
//$registrationIDs = array($_POST['devices']);
$registrationIDs = $_POST['devices'];
$proxy = 'http://proxy.vmsrv.redbrick.dcu.ie:3128';
// Message to be sent
$message = $_POST['message'];
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array("message" => $message),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_PROXY, $proxy);
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
print_as_json($result);
// Report all PHP errors
error_reporting(-1);
?>
输出的唯一我在控制台中看到我的Java程序之后调用的脚本是:
[java] DB: Result: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in /var/www/tools.php on line 5[]
反正我可以尝试得到一些信息回来找出问题是什么?
编辑 tools.php
<?php
function print_as_json($result) {
$all = array();
while($r = mysql_fetch_assoc($result)) {
$all[] = $r;
}
echo(json_encode($all));
}
?>
答
删除线
print_as_json($result);
,并使用简单的回声来代替。
echo $result;
或者你可能想尝试看看它是什么类型的对象:
var_dump($result);
我不知道什么$结果如下。如果是json字符串,则可以将其转换为数组:
$array = json_decode($result);
+0
啊,我得到一个发件人不匹配的错误,谢谢! – TomSelleck 2013-04-07 10:23:47
您的tools.php文件的外观如何? – 2013-04-06 19:52:06
现在更新后的帖子。 – TomSelleck 2013-04-06 19:59:39