2012-12-19 75 views
1

我正尝试在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和“已收到消息”不会出现。

+0

您确定您的API密钥是否正确,并且您的服务器IP列为受信任的证书? data属性不能是多维数组。您需要在顶层定义它们:'data.price''data.message' – Daniel

+0

感谢您的回复Daniel,我已经检查过API密钥是否正确,目前它允许任何IP。我曾尝试使用'data.price'=>“测试”,也'数据'=>“测试”,但仍然没有运气:( – user1804590

+0

我也有这个问题...请参考此答案http:// stackoverflow .com/a/12349953 然后它就像一个魅力GCM消息来到我的真实设备... – 2013-01-05 13:20:17

回答

2

就解决了这个(虽然我不知道是什么原因导致原问题做)。一世 关闭我的设备并再次启动它,并且它在接收我在过去几天发送的所有消息时变得生气。现在,它们在发送时会收到它们,因此假设其他所有信息都是正确的,如果有人遇到此问题,请尝试重新启动设备。

+2

是什么让我这么做是因为我在谷歌论坛上看到,WiFi连接的设备有时会在一段时间后无法响应GCM。 – user1804590

+0

关闭Wi-Fi也解决了我的问题。 – stschindler

0

何况我经历了一个非常类似的(相同?)的问题。我的问题是我在PHP代码中将字符串从“价格”中更改了,但我没有在Android代码中完成。你需要做这两件事。也许这会帮助别人。 :)

相关问题