2

Following is the json that I am trying to send from the firebase console

{
    "message": {
        "token": "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
        "data": {
            "Nick": "Mario",
            "body": "great match!",
            "Room": "PortugalVSDenmark"
        }
    }
}

My code in onMessageReceived is as follows

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(TAG, "From: " + remoteMessage.getFrom());

        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

            String name = remoteMessage.getData().get("Nick").toString();
            String body = remoteMessage.getData().get("body").toString();
            String room = remoteMessage.getData().get("Room").toString();

            String messageBody = name
                    + " "
                    + body
                    + " "
                    + room;

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setContentTitle("Zone")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    /*Notification with Image*/;
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        }
    }

Every time I send the above json from firebase console it does not even enter the if condition because the remoteMessage.getData().size() is not greater than zero.

Why is this happening though I am sending a data payload?

Also when the notification is received I want to create a sentence from the raw json and display it . E.g "Nick liked a picture"

When the notification comes through only the json is displayed.

Been struggling with this for a long time .

Any help will be appreciated. Thank you

A.S
  • 798
  • 1
  • 10
  • 32

1 Answers1

0

Try this.

String nick,body,room, fullString;

if(!remoteMessage.getData().isEmpty()){
        Log.d(TAG, "Notification Message Data: " + remoteMessage.getData());
        nick=remoteMessage.getData().get("Nick");
        body=remoteMessage.getData().get("body");
        room=remoteMessage.getData().get("Room");
        Log.d(TAG, "All data: " + nick+","+body+","+room);
        fullString = "Anything you want"+nick+"anything";
        //then use the string anywhere
    }