简体   繁体   English

当 android 应用程序处于终止状态时,如何从推送通知(使用 FCM)中获取“数据有效负载”?

[英]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.

相关问题 如果设备收到来自fcm的推送通知,如何显示/通信/广播消息到处于终止状态的android应用 - how to show/communicate/broadcast messages to android app that is in killed state if device receives push notification from fcm 当应用不在任务中或被杀死时,如何获取FCM通知数据? - How to get FCM notification data when app is not in task or killed? 如何在应用程序被终止时处理 fcm 推送通知,而不使用 WakefulBroadcastReceiver,因为它在 26.1.0 中已弃用? - How to hanlde fcm push notification when app is killed, without using WakefulBroadcastReceiver as its decprecated in 26.1.0? 当接收方设备未在Android中使用FCM处于前台状态时,如何从设备发送推送通知? - How to send Push Notification from device when the receiver device is not in foreground state using FCM in android? 使用 FCM 杀死应用程序时,Android 无法接收通知 - Android unable to receive notification when app is killed using FCM Android - 当收到推送通知 FCM 时,如何更新我的活动中的数据或 UI? - Android - How can I update data or UI in my Activity when Push Notification FCM is received? 在一加3t移动设备上杀死应用程序或从最近使用的应用程序中刷出应用程序时,Android FCM推送通知不起作用 - Android FCM Push Notification Not Working When App is being Killed or Swipe Out from Recent Apps on One plus 3t Mobile 应用程序被杀/刷出时未收到FCM推送通知 - FCM push notification not received when app is killed/swiped out 应用程序被杀死/刷出时的 FCM 推送通知 - FCM push notification when app is killed/swiped out 用户单击通知启动后台应用程序时如何获取Android推送通知有效负载 - How to get Android push notification payload when background App is launched by user click on notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM