【问题标题】:GCM messages not getting to API or sent, but success flag returnedGCM 消息未到达 API 或未发送,但返回成功标志
【发布时间】:2012-12-19 09:18:00
【问题描述】:

我正在尝试使用 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,"success":1,"failure":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;
}

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

额外信息:

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

(上面的 price 只是检索消息,因为它是这样存储的:$message = array("price" => $message); 在作为参数提供给服务器上的 send_notification 函数之前。

我检查了 LogCat 并且没有出现“收到的消息”。

【问题讨论】:

  • 您确定您的 API 密钥是正确的,并且您的服务器 IP 被证书列为受信任的吗? data 属性不能是多维数组。您需要在顶层定义它们:data.pricedata.message
  • 感谢您的回复 Daniel,我已检查 API 密钥是否正确,目前它允许任何 IP。我尝试过使用 'data.price' => "test" 和 'data' => "test",但仍然没有运气:(
  • 我也遇到了这个问题......请参考这个答案stackoverflow.com/a/12349953 然后它就像一个魅力 GCM 消息来到我的真实设备......

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


【解决方案1】:

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

【讨论】:

  • 让我这样做的原因是我在谷歌论坛上读到,WiFi 连接的设备有时会在一段时间后对 GCM 无响应..
【解决方案2】:

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

这篇文章中没有提到

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2013-08-28
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多