Get value from RemoteMessage from FCM onMessageReceived method

Jatin Patel picture Jatin Patel · Jul 7, 2016 · Viewed 48.7k times · Source

I have migrate gcm to fcm for push notification message. but how I Get bundle data from RemoteMessage received onMesssageReceived method.

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

So please tell me how I parse remotemessage for get all value of notification.

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"
]
}

Answer

Pritish Joshi picture Pritish Joshi · Jul 7, 2016

Here is the code snippet which is pretty much self Explanatory.

You get the data in the form of the Map

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());
          }
       }

Make Sure from server you are sending data in correct format i.e. in the "data" key

here is the demo Json file

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