how get message body when FCM notification message is tap?
Asked Answered
C

3

5

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!

Congou answered 10/8, 2016 at 7:46 Comment(2)
Read this post, this may helpfull #34094710Enrichetta
@Congou , have you solved this problem. have you get message body in intent? I am fetching same problem.Dishevel
W
4

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"));
        }
Widdershins answered 10/8, 2016 at 7:51 Comment(3)
This will work for data messages and for notification messages when the app is in the foreground. It will not work for notifications messages when the app is in the background, since onMessageReceived() will not be invoked in that case. Instead a notification will be displayed in the system notification area and clicking it opens the app.Gerstner
@FrankvanPuffelen Can u please provide a solution what to do when app is in Background?Widdershins
See Diego's answer.Gerstner
J
4

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/

Joeljoela answered 10/8, 2016 at 10:9 Comment(0)
B
0

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"); 
Bacolod answered 12/9, 2023 at 21:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.