Rakesh L
Rakesh L

Reputation: 1178

Android - Unable to parse Remote Message from FCM

I am developing an Android application in which I would like to receive push notification by FCM.

The onMessageReceived callback is triggered but the RemoteMessage.getData() has returned null value and when I evaluated the expression , the RemoteMessage.mBundle has the required data but not in JSON format

The following is the data I am getting from RemoteMessage.mBundle

Bundle[{google.sent_time=1519016906113, gcm.notification.categoryId=80302, google.ttl=2419200, gcm.notification.notificationId=127647, gcm.notification.e=1, gcm.notification.threadId=127093, gcm.notification.title=Moni Expert has commented on your post, from=612005318045, gcm.notification.roleId=115442, gcm.notification.userId=76284, gcm.notification.profileUrl=/image/user_male_portrait?img_id=122306&img_id_token=%2B99KTi28KTf7%2BehhVakxUyPqAuU%3D&t=1519016037919, google.message_id=0:1519016906118429%c05b1316c05b1316, gcm.notification.lastName=Expert, gcm.notification.notificationDate=2018-02-19 05:07:17.821, gcm.notification.timeStamp=Fri Feb 16 14:57:08 GMT 2018, gcm.notification.body=Moni Expert has commented on your post, gcm.notification.flag=0, gcm.notification.type=1, gcm.notification.firstName=Moni, gcm.notification.threadStatus=Published, gcm.notification.subject=posted, gcm.notification.roleName=D4E Admin, gcm.notification.userName=moniexpert, gcm.notification.postedUserId=76284, collapse_key=digital.engineers.club, gcm.notification.notification=Moni Expert has commented on your post}]

I have searched a lot to convert this data to Json format but nothing helped.

Update :

The code snippet has mentioned below

public class D4EPushService extends FirebaseMessagingService {

    private static final String TAG = "MyFMService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // Handle data payload of FCM messages.
        Log.d(TAG, "FCM Message Id: " + remoteMessage.getMessageId());
        Log.d(TAG, "FCM Notification Message: " +
                remoteMessage.getNotification());
        Log.d(TAG, "FCM Data Message: " + remoteMessage.getData());
        remoteMessage.mBundle;
    }

I am not able to even assign remoteMessage.mBundle to new bundle variable in my code snippet as it has privately declared in RemoteMessage class

Please anyone help me to find the solution.

Upvotes: 1

Views: 1675

Answers (1)

Maradiya Krupa
Maradiya Krupa

Reputation: 283

Try below solution it is work for me.

if (remoteMessage.getData().size() > 0) {


    Map<String, String> data = remoteMessage.getData();

    String message = data.get("your key");}

Upvotes: 1

Related Questions