GCM消息未进入API或已发送,但返回了成功标志

时间:2012-12-19 09:18:00

标签: php android curl google-api google-cloud-messaging

我正在尝试使用PHP在Android上设置Google Cloud Messaging(遵循本教程:http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/)。我的设备与GCM成功注册,然后与服务器注册(regId存储在数据库中)。

当我尝试发送到设备时,没有收到任何信息,但我得到了这个回复:

{ “multicast_id”:4701392840778619841, “成功”:1, “失败”:0, “canonical_ids”:0 “结果”:[{ “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;
}

从研究中我已经看到了类似的问题,但是它们略有不同 - 例如,不同的编程语言,我找不到的解决方案已经解决了这个问题。

额外信息:

1。我已检查并启用了cURL。

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);
}

(上面的价格只是检索邮件,因为它的存储方式如下:$ message = array(“price”=> $ message);然后作为参数提供给服务器上的send_notification函数。

我已检查过LogCat并且未显示“已接收消息”。

3 个答案:

答案 0 :(得分:2)

刚刚解决了这个问题(虽然我不知道造成原始问题的原因)。我关闭了我的设备并再次启动它,它收到了我过去几天收到的所有消息。现在它在发送时收到它们,所以假设其他一切都是正确的,如果有人遇到这个问题,请尝试重新启动设备。

答案 1 :(得分:0)

请生成并使用服务器api密钥查看设备上的实时通知。

这篇文章中没有提到

http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

答案 2 :(得分:0)

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

相关问题