繁体   English   中英

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

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

我的推送通知(使用 FCM)在前台和后台状态正常工作,但在终止状态下我只能从系统托盘接收通知但无法获得“数据有效负载”。 我知道 onMessageReceived() 没有在终止状态下调用(如果我错了,请纠正我),有什么方法可以在终止状态下调用此方法或以任何其他方式获取“数据有效负载”。 请帮忙!

如果您想在应用程序处于终止状态时读取数据有效负载,则不要发送通知有效负载仅使用数据有效负载管理所有操作。 如果应用程序处于终止状态并且您发送通知有效负载,则它不会调用 onRecive() 方法,因此在数据有效负载中发送通知的标题和消息并在 onRecive() 中生成通知

我为您提供完美的解决方案:

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

只需将您的整个数据放在一个 Map 中,然后通过从 map 中放入不同的键来获取值。

我还建议仅使用数据而不是通知,并使用http://www.androidbegin.com/tutorial/android-custom-notification-tutorial/构建您自己的自定义通知

希望这会有所帮助。

@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.

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