2017-06-19 118 views
0

我有一个应用程序在哪个服务器使用GCM服务器发送一些推送通知,实现在两端完成,但我面临的问题是,我无法从消息正文获取通知,它总是向我显示“null”。但是我可以使用调试在消息体内看到消息。如何从android中的GCM消息体获取消息?

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}] 
+0

您必须将您的通知作为JSON发送。你的身体应该包含这样的内容{“gcm.notification.title”:“”, “gcm.notification.body”:“”},你可以得到像这样的身体bundle.getString(“gcm.notification.body” ) –

回答

1

尝试后,

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

太棒了它的工作 – Niraj

0

它接收正确的数据仅

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

问题是打印消息转换斯汀

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

getStringExtra()不会显示错误 – Niraj

+0

使用相同的bundle.getString – sasikumar

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。