Edson Reis
Edson Reis

Reputation: 131

FCM onReceivedMessage() get Bundle

link of structure

I'm trying to get the message from my ArrayMap but I can not access the ReceiveMessage bundle. I tried to access Map directly, but it is very wrong

My code

public class FbService extends FirebaseMessagingService {
    public FbService() {
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO(developer): Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.

        Map<String, String> params = remoteMessage.getData();
        String message =  params.get(3);

        Log.d("FbService", "Notification Message Body: " + message);



    }
}

Upvotes: 3

Views: 4724

Answers (3)

mahmoud zaher
mahmoud zaher

Reputation: 554

thanks to @Pritish Joshi

I have found this answer and helped me a lot , Here is the code snippet

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

reference :

Upvotes: 5

PRATEEK BHARDWAJ
PRATEEK BHARDWAJ

Reputation: 2442

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";
    String title = "";


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        send_Notification(remoteMessage);

    }


    private void send_Notification(RemoteMessage remoteMessage) {

        String notifications_text = "";


        String title = remoteMessage.getNotification().getTitle();
notifications_text =remoteMessage.getNotification().getBody();
        Intent resultIntent = new Intent(this, SplashActivity.class);
        TaskStackBuilder TSB = TaskStackBuilder.create(this);
        TSB.addParentStack(SplashActivity.class);
        TSB.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = TSB.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
        nb.setSmallIcon(R.drawable.logo);
        nb.setContentIntent(resultPendingIntent);
        nb.setAutoCancel(false);
        nb.setLargeIcon(BitmapFactory.decodeResource(getResources(),
                R.drawable.logo2));
        nb.setContentTitle(getString(R.string.app_name) + " " + title + " " + getString(R.string.str_notification));
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(random(), nb.build());

    }


    int random() {
        Random rand = new Random();
        int randomNum = 1 + rand.nextInt((100000 - 1) + 1);
        return randomNum;
    }




}

Try this one. it may help you.

Upvotes: 0

Priya
Priya

Reputation: 692

Get the bundle value using getJson, Now you will the constructed value as String in Json format,

 private String message = getJson(bundle);

Convert the string json to JSONObject,

 JSONObject object = new JSONObject(message );

From object get the value like object.optInt("NAME");

Upvotes: 0

Related Questions