如何从android中的GCM消息体获取消息?

时间:2017-06-19 10:14:47

标签: android

我有一个应用程序,其中服务器使用GCM服务器发送一些推送通知,实现是在双方完成但我面临的问题是我无法从消息正文获得通知,它总是显示我“null” 。但我可以使用调试在消息体中查看消息。

 String message = bundle.getString("message");
    Log.e(TAG, "onMessageReceived::" + message);
    Log.e(TAG, "from::" + from);

和邮件正文是: -

Bundle[{google.sent_time=1497866966996, google.message_id=0:1497866967011288%466cbbd8466cbbd8, notification=Bundle[{priority=high, body=Your wallet has been credited with 1.000 point(s)., title=Wallet credited}], collapse_key=com.s.baty}]

3 个答案:

答案 0 :(得分:1)

尝试,

Bundle notificationData = bundle.getBundle("notification");
String body = notificationData.getString("body");

答案 1 :(得分:0)

仅收到正确的数据

   String bodymessage = bundle.getString("body");

问题是转换Sting后的打印消息

 Log.e(TAG, "onMessageReceived::" + message.toString());

答案 2 :(得分:0)

您可以在GCMIntentService实现类的onMessage()函数中接收消息。我已经创建了一个获取正确信息和密钥的功能。

@Override
protected void onMessage(Context context, Intent intent) {
     dumpIntent(intent);
}


public void dumpIntent(Intent i) {

        Bundle bundle = i.getExtras();
        if (bundle != null) {
            Set<String> keys = bundle.keySet();
            Iterator<String> it = keys.iterator();
            while (it.hasNext()) {
                String key = it.next();
                Log.e("Intent Data", "[" + key + "=" + bundle.get(key) + "]");
            }

        }
    }

之后,您可以使用NotificationManager轻松设置消息。

相关问题