简体   繁体   中英

How can I get “data payload” from push notification (using FCM) when android app is in killed state?

My push notification(using FCM) is working correctly in foreground and background state, but in killed state I am able to receive only notification from the system tray but can't get "data payload". I am aware about the fact that onMessageReceived() is not called in killed state(correct me if I am wrong), is there any way to call this method in killed state as well or any other way to get "data payload". Please help!

if you want read data payload when when app in kill state then don`t send notification payload manage all operation using data payload only . if app in kill state and you send notification payload then it does not call onRecive() method so send tital and message of notification in data payload and generate notification in onRecive()

I have the perfect Solution for you :

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.e("FCM_MESSAGE", remoteMessage.getData().toString());
    Map<String, String> dataMap = remoteMessage.getData();
}

Just put your whole data in a Map and get values by putting in different keys from map.

I would also suggest to use only data instead of notification and build your own custom notification using http://www.androidbegin.com/tutorial/android-custom-notification-tutorial/

Hope this helps.

@Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); if (remoteMessage.getData().size() > 0) { Map<String, String> data = remoteMessage.getData(); ArrayList<String> ndata = new ArrayList<>(); for (Map.Entry<String, String> entry: data.entrySet()){ ndata.add(entry.getValue()); } if(ndata.size()>0){ showNotification(ndata.get(1), ndata.get(0)); } } if (remoteMessage.getNotification() != null) { showNotification( remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody()); } }

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