简体   繁体   中英

Can't extract data from Intent - Firebase Push Notification

I'm using Firebase Notifications. For test, I send notifications from the Firebase Console. But in my MainActivity I can't get data, which I set in the console.

Method onMessageReceived(RemoteMessage msg) in MyFirebaseService doesn't get called (if the method didn't get called - data transfer to MainActivity in Intent ). But I don't see this data in Intent .

For example: 在此处输入图片说明 And in debug: 在此处输入图片说明

If you use the console to send notification, it is not considered as payload, so if the application is running in background, the method onMessageReceived() is not called. But if the app run in foreground it does.

Here is a curl command to send a notification via curl as payload, you have to use a shell to use it.

curl -X POST --header "Authorization: key=<APIKEY>" --Header "Content-Type:
application/json" https://fcm.googleapis.com/fcm/send -d '{"to":"<YourToken>","data":{"type":"notification","message":"Hello, it works"}}'                     

**Note: remove the <>.

Your onMessageReceived() method looks like this to get the data.

 @Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
 try {
       String remote_type = remoteMessage.getData().get("type");
        if (remote_type.equals("notification")){
            String message = remoteMessage.getData().get("message");
            Log.d("ReceivedData", message);
        }
    }catch(NullPointerException e){ 
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM