刘世都
刘世都

Reputation: 51

how get message body when FCM notification message is tap?

When app is in background, the notification message is deliver by Notification, when the notification is tap, app is launch, how can i get the message body?

the intent is this:

    Bundle[{google.sent_time=1470813025421, from=568540028909, 
google.message_id=0:1470813025865549%31bd1c9631bd1c96,
 collapse_key=com.google.firebase.quickstart.fcm}]

no message body in intent, only message id! Thanks!

Upvotes: 5

Views: 5280

Answers (3)

Jhordy Abonia
Jhordy Abonia

Reputation: 1

You must previously put the information in the data field, of the data load you send to the server.

For example, I send like this:

...
     CURLOPT_POSTFIELDS =>' {
                '.$to.',
                "notification": {
                    "title": "'.$subject.'",
                    "body": "'.$msg.'",
                    "color":"#ff0000",
                    "tag":"'.$tag.'"
                    '.$action.'
                    '.$image.'
                },
                 "data": {
                    "title": "'.$subject.'",
                    "body": "'.$msg.'",
                    "color":"#ff0000",
                    "tag":"'.$tag.'"
                    '.$action.'
                    '.$image.'
                }
            }'
    ...

Then on Android you recover the data with:

...
Object [] keys = intent.getExtras().keySet().toArray();
        String keysString = "";
        for( int i=0;i<keys.length;i++){
            Log.d("PUSH++", keys[i]+": "+intent.getExtras().get((String)keys[i]).toString()+" i"+i); 
    }
...
Log.d("PUSH++", "title: "+intent.getStringExtra("title"); 

Upvotes: 0

Diego Giorgini
Diego Giorgini

Reputation: 12717

I am sorry but what you are trying to do is not possible.

Currently it's not possible to access the (body, title, icon ...) information of a
notification-message from the activity that is launched when the notification is opened.

You can instead access the data component of the notification message.

One possible alternative is to use data-message messages and create your own custom notification and custom logic.

see notification-message vs data-message here:
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

PS: if would be useful if you could report a Feature Request through the firebase support page.
In this way the team can correctly prioritize future features. https://firebase.google.com/support/contact/bugs-features/

Upvotes: 4

Jagjit Singh
Jagjit Singh

Reputation: 1969

try this

  @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.
            Log.d(TAG, "From: " + remoteMessage.getFrom());
            Log.d(TAG, "From: " + remoteMessage.getData().get("message"));
    //        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
// if you sending data with custom key
 Log.d(TAG, "Notification Message of custom key: " +remoteMessage.getData().get("your key"));
            sendNotification(remoteMessage.getData().get("message"));
        }

Upvotes: 4

Related Questions