14

我已經爲推送通知消息遷移gcm to fcm。 但是我如何從RemoteMessage獲取bundle數據,並接收onMesssageReceived方法。從FCM的RemoteMessage獲取onMessageReceived方法的值

Old GCM give bundle data onMessageReceiced method but in FCM there is RemoteMessage data. 

那麼請告訴我如何解析remotemessage以獲取所有通知值。

MY PAYROL

{ 
"collapse_key":"score_update", 
"priority":"high", 
"content_available":true, 
"time_to_live":108, 
"delay_while_idle":true, 
"data": 
{ 
    "message": "Message for new task", 
    "time": "6/27/2016 5:24:28 PM" 
}, 
"notification": { 
    "sound": "simpleSound.wav", 
    "badge": "6", 
    "title": "Test app", 
    "icon": "myicon", 
    "body": "hello 6 app", 
    "notification_id" : "1140", 
    "notification_type" : 1, 
    "notification_message" : "TEST MESSAGE", 
    "notification_title" : "APP" 
    }, 
"registration_ids": ["cRz9SJ-gGuo:APA91bFJPX7_d07AR7zY6m9khQro81GmSX-7iXPUaHqqcOT0xNTVsOZ4M1aPtoVloLNq71-aWrMCpIDmX4NhMeDIc08txi6Vc1mht56MItuVDdA4VWrnN2iDwCE8k69-V8eUVeK5ISer" 
] 
} 

回答

19

在FCM中,您收到了RemoteMessage而不是Bundle。

下面是我在我的應用程序使用的方式,其中的數據是我RemoteMessage

int questionId = Integer.parseInt(data.get("questionId").toString()); 
String questionTitle = data.get("questionTitle").toString(); 
String userDisplayName = data.get("userDisplayName").toString(); 
String commentText = data.get("latestComment").toString(); 

下面是我從服務器

{ 
    "registration_ids": "", 
    "data": { 
    "questionId": 1, 
    "userDisplayName": "Test", 
    "questionTitle": "Test", 
    "latestComment": "Test" 
    } 
} 

它發送我的通知數據所以,你必須分析每個並根據你的迴應每個領域。 由於我調試了代碼,您將在RemoteMessage中接收地圖,並將這些字段轉換爲適當的數據類型,因爲所有這些數據都以字符串的形式出現。

+0

感謝您的回答,但我的其他數據下的通知對象不在數據對象中,那麼我怎樣才能得到它 –

+0

正如你已經在評論中提到,你會從remoteMessage得到你的結果。得到通知()。你能試試嗎.. –

25

這裏是代碼片段,這是相當多的自我解釋。

你得到的數據在地圖的形式

public void onMessageReceived(RemoteMessage remoteMessage) 
     { 
      Log.e("dataChat",remoteMessage.getData().toString()); 
      try 
      { 
       Map<String, String> params = remoteMessage.getData(); 
       JSONObject object = new JSONObject(params); 
       Log.e("JSON_OBJECT", object.toString()); 
      } 
     } 

確保從您在「數據」鍵

這裏發送的正確格式,即數據服務器演示JSON文件

{ 
    "to": "registration_ids", 
    "data": { 
    "key": "value", 
    "key": "value", 
    "key": "value", 
    "key": "value" 
    } 
} 
+1

感謝您的回答,但我的其他數據下的通知對象不在數據對象,那麼我怎麼能得到它。 –

+3

'remoteMessage.getData()'返回空數組 –

相關問題