fongfuse
fongfuse

Reputation: 129

How to get data in notification FCM android

"notification":{  
      "title":"[add title]",
      "body":"[add your message]",
      "badge":11
   }

In my FCM is sent badge in notification object

How to get it

ps. title and body I can get it but I can't get badge

thanks!

Upvotes: 2

Views: 9211

Answers (3)

Nick Bapu
Nick Bapu

Reputation: 463

You need to get "badge" as int like

jsonObject.getInt("badge")

Upvotes: 1

Zealous System
Zealous System

Reputation: 2324

You can retrieve data from below method

 @Override
 public void onMessageReceived(RemoteMessage remoteMessage) {

    Map<String, String> data = remoteMessage.getData();
    String title = data.get("title").toString();
    String message = data.get("body").toString();
    String message = data.get("badge").toString();
}

Upvotes: 3

Yamini Balakrishnan
Yamini Balakrishnan

Reputation: 2411

As per doc, You have to send notification as data type, to send/receive custom params. You have to send from server like,

"data":{  
      "title":"[add title]",
      "body":"[add your message]",
      "badge":"11"
   }

And On receiver side, you have parse that as follows:

 @Override
        public void onMessageReceived (RemoteMessage remoteMessage){
            super.onMessageReceived(remoteMessage);
            Log.d(TAG, "From: " + remoteMessage.getFrom());
            // Check if message contains a data payload.
            if (remoteMessage.getData().size() > 0) {
                Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

                try {
                    Map<String, String> map = remoteMessage.getData();

                    handleDataMessage(map);

                } catch (Exception e) {
                    Log.e(TAG, "Exception: " + e.getMessage());
                }
            }

        }

        private void handleDataMessage(Map<String,String> json){
            Log.e(TAG, "push json: " + json.toString());

            try {

                String title = json.get("title");
                String message = json.get("body");
                String badge = json.get("badge");

              // Display notication depends on received data


            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }

Upvotes: 0

Related Questions