2017-06-19 81 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。