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" ] }
A user tap on a notification opens the app launcher by default. Messages with both notification and data payload, when received in the background. In this case, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
Messages may have a RemoteMessage. Notification instance if they are received while the application is in the foreground, otherwise they will be automatically posted to the notification tray. Use the RemoteMessage. Builder class for building message instances to send via send(RemoteMessage) .
Open new activity on click of push notification. Display data coming from push notification of new activity. If the application is closed so after click on notification the app get started.
Notification messages When your app is in the foreground, the FCM notification message is delivered to the onMessageReceived() handler and you can handle it by posting a notification if needed or update the app content with the FCM payload data (Max 4KB) or fetch content from app server.
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" } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With