【问题标题】:GCM onMessageReceived() not called on specific (or some) devicesGCM onMessageReceived() 未在特定(或某些)设备上调用
【发布时间】:2016-02-16 14:08:15
【问题描述】:

我正在使用 Google Cloud Messaging 在我的 android 应用程序中实现推送通知。 在大多数设备上一切正常,但是当我在 Samsung Galaxy S Dous GT-S7562 (Android 4.0.4, API 15) 上测试应用程序时,当我发送通知时,它不会调用onMessageReceived() 方法。

由于在其他设备上一切正常,我很确定我的代码的功能。 我使用下面的代码检查 GooglePlayServices 的可用性,它返回 true

private boolean checkPlayServices() {
    GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
    int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (apiAvailability.isUserResolvableError(resultCode)) {
            apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
                    .show();
        } else {
            Log.i(TAG, "This device is not supported.");
            finish();
        }
        return false;
    }
    return true;
}

虽然 RegistrationIntentService() 将设备的令牌发送到我的服务器,并且 GCM 调用来自我的服务器响应“success”,但该应用不会调用 onMessageRecieved() 方法,因为我把日志放在它的开头。

还有什么我必须检查才能调试问题的吗?

感谢您的帮助。

【问题讨论】:

  • 您找到解决方案了吗? FCM 也发生在我身上。

标签: java android android-studio notifications google-cloud-messaging


【解决方案1】:

检查您的脚本是否在有效负载中包含通知

请看下面的链接

https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages

【讨论】:

  • 我使用数据并自己管理:{"registration_ids":[" ... mytoken ... "],"data":{"type":"mynotification","title": "1","消息":"2"}}
  • 我也试过这个,但没有运气:{"registration_ids":[" ...mytoken ..."],"data":{"title":"1"," 2":"message","type":"mynotification"},"notification" : { "body" : "伟大的比赛!","title" : "Portugal vs. Denmark", "icon" : "myicon"} }
【解决方案2】:

这是一个特殊情况。首先,需要检查此链接后的注册令牌。 通过 curl 检查注册令牌:请参阅此link

# api_key=YOUR_API_KEY
# curl --header "Authorization: key=$api_key" \
   --header Content-Type:"application/json" \
   https://gcm-http.googleapis.com/gcm/send \
   -d "{\"registration_ids\":[\"ABC\"]}"

“如果您想确认注册令牌的有效性,可以通过将“ABC”替换为注册令牌来实现。”

我认为最好在检查 curl 的结果后进行调查。 你也可以参考这个site

另外,最好检查一下设备的通知设置。

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多