我正尝试在PHP上安装Google Cloud Messaging(请参阅本教程:http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/)。我的设备通过GCM成功注册,然后与服务器注册(regId存储在数据库中)。GCM消息没有得到API或发送,但成功标志返回
当我尝试发送到设备没有收到,虽然我得到这样的响应:
{ “multicast_id”:4701392840778619841, “成功”:1, “失败”:0, “canonical_ids”:0 ,“results”:[{“message_id”:“0:1355907831651938%978fee92f9fd7ecd”}]}
Google API报告没有请求。
这是在PHP我send_notification功能:
public function send_notification($registration_ids, $message) {
// include config
include_once './config.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registration_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'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);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
从研究,我已经看到了类似的问题已经问到这一点,但是他们略有不同 - 例如,不同的编程语言并没有解决,我有发现已经解决了这个问题。
额外的信息:
我已经检查和卷曲已启用。
2.我onMessage方法
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
(价格在上述刚检索存储这样的信息作为其:$消息=阵列( “价格”=> $消息);被给予前作为
参数传递给send_notification功能在服务器上。我已经检查的logcat和“已收到消息”不会出现。
您确定您的API密钥是否正确,并且您的服务器IP列为受信任的证书? data属性不能是多维数组。您需要在顶层定义它们:'data.price''data.message' – Daniel
感谢您的回复Daniel,我已经检查过API密钥是否正确,目前它允许任何IP。我曾尝试使用'data.price'=>“测试”,也'数据'=>“测试”,但仍然没有运气:( – user1804590
我也有这个问题...请参考此答案http:// stackoverflow .com/a/12349953 然后它就像一个魅力GCM消息来到我的真实设备... – 2013-01-05 13:20:17