[英]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".我的推送通知(使用 FCM)在前台和后台状态正常工作,但在终止状态下我只能从系统托盘接收通知但无法获得“数据有效负载”。 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".
我知道 onMessageReceived() 没有在终止状态下调用(如果我错了,请纠正我),有什么方法可以在终止状态下调用此方法或以任何其他方式获取“数据有效负载”。 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()
如果应用程序处于终止状态并且您发送通知有效负载,则它不会调用 onRecive() 方法,因此在数据有效负载中发送通知的标题和消息并在 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.只需将您的整个数据放在一个 Map 中,然后通过从 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/我还建议仅使用数据而不是通知,并使用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()); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.