【问题标题】:Android: GCM Push notification Implementation, in onMessageReceived method soemtimes "from" is nullAndroid:GCM 推送通知实现,在 onMessageReceived 方法中有时“from”为空
【发布时间】:2016-04-08 22:16:54
【问题描述】:

我已经使用 google 提供的教程在我的 android 应用程序中实现了 GCM。一切正常,但有时 onMessageReceived 方法的“from”参数为空。以下是我的 GcmListenerService

enter code herepublic class MsgLstnrSvc extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {

    String message = data.getString("message");
    if (from.startsWith("/topics/")) {
        // message received from some topic.
    } else {
        // normal downstream message.
    }
}

    private String LOG_AREA = "MsgLstnrSvc";
}

我可以知道这可能是什么原因吗?在此之前我从未做过 GCM 集成。提前致谢

【问题讨论】:

    标签: android google-cloud-messaging


    【解决方案1】:

    仔细检查您使用的key是否从服务器和应用程序中发送相同。

    例如:

    服务器

    {
    "to": "/topics/foo-bar",
    "data": {
    "message": "This is a GCM Topic Message!",
    }
    }
    

    客户

    @Override
    public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);
    
    if (from.startsWith("/topics/")) {
    // message received from some topic.
    } else {
    // normal downstream message.
    }
    
    // ...
    }
    

    有关更多信息,请查看此SO question,了解如何为服务器和客户端应用程序实现 GCM 消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多