29

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"
]
}
Jatinkumar Patel
  • 1,094
  • 2
  • 17
  • 34

5 Answers5

46

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"
  }
}
Pritish Joshi
  • 2,025
  • 3
  • 18
  • 33
36

In FCM you received RemoteMessage instead of Bundle.

Below is the way I used in my application where data is my RemoteMessage

Map<String, String> data = remoteMessage.getData()
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();

Below is my notification data which I am sending it from server

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

So you have to parse each and every field as per your response. As I have debugged the code you will receive map in your RemoteMessage and cast those fields in appropriate data types as all those data comes as string.

Drup Desai
  • 912
  • 6
  • 18
  • 1
    Thanks for your answer but My other data under notification object not in data object then how can I get it – Jatinkumar Patel Jul 07 '16 at 12:29
  • As you have already mentioned in comment that you will get your result from remoteMessage.getNotification(). Can you try that.. – Drup Desai Jul 08 '16 at 09:33
  • there is no message.get function – Dean Blakely Jun 27 '18 at 20:06
  • get method is of map. You need to call RemoteMessage.getData() first. – Drup Desai Sep 10 '18 at 10:15
  • And don't forget to call toString() method on remoteMessage.getData.get(). – mia Sep 20 '18 at 05:34
  • I actually don't have the get method. I have remoteMessage.zzds and in there there is an ArrayMap with the actual values. Any idea how to get it from there? – f.trajkovski May 19 '20 at 22:57
  • @f.trajkovski you can get it with the help of remoteMessage.getData() method, Which actually returns Map – Drup Desai May 20 '20 at 13:10
  • I wasn't able to get the data like that but my issue was on the server side. I was sending the message in a wrong format and that is the reason why I had empty map. – f.trajkovski May 24 '20 at 10:04
  • @DrupDesai How can we use the getData() while using the notification keyword behalf of data. if I am going to use getData then not getting data if the application in kill from background also. – Peter Aug 18 '21 at 10:29
2

For your data that looks like:

"data":{ 
    "message": "Message for new task",
    "time": "6/27/2016 5:24:28 PM"
}

you should get them through

Log.d(TAG, "Key Data : " +  remoteMessage.getData().get("message").toString());
Log.d(TAG, "Key Data : " +  remoteMessage.getData().get("time").toString());

Wrap them in try catch to be sure

Al-Punk
  • 3,531
  • 6
  • 38
  • 56
2

The type of fun getData() is Map<String, String>. So you can fetch data object values like we usually fetch with Map<String, String>.

"data": {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
    "key4": "value4"
  }

then you have to fetch like this in kotlin

val data = remoteMessage.data
val key1 = data[key1]
val key2  = data[key2]// you can fetch like this

For getNotification() type is Notification object

val notification: Notification =  remoteMessage.notification
val message = notification.body
val title = notification.title // you can fetch by accessing Notification class member

Here are the details about the Notification object

Krishna Sony
  • 1,286
  • 13
  • 27
1

RemoteMessage.Notification notification = remoteMessage.getNotification();
        Map<String, String> data = remoteMessage.getData();
        sendNotification(notification, data);
String urlparam=data.get("url_param").toString();
String urlparam=data.get("dp_name").toString();

"data": {
    "notification_id": 11,
    "title_url": "service_id=1;service_category=Installation;",
    "destination": "Category_page"
  }