3

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);



    }
}
KENdi
  • 7,576
  • 2
  • 16
  • 31
Edson Reis
  • 131
  • 1
  • 9
  • You are not accessing the map directly, you are calling method that returns it. Why do you think that is wrong? – X3Btel May 21 '17 at 20:45
  • Refer this : https://stackoverflow.com/questions/42273699/how-to-stack-firebase-cloud-messaging-notifications-when-the-application-is-not/43914028#43914028 – Maddy May 22 '17 at 04:50

3 Answers3

5

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 :

mahmoud zaher
  • 544
  • 6
  • 9
0

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");

Priya
  • 672
  • 1
  • 5
  • 21
0
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.

PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35