从活动通知中检索 FCM 数据负载

Retrieve FCM data payload from active notifications

我们有一个后端服务器,可以将带有数据负载的 FCM 推送通知发送到 Android 和 iOS 设备。我正在开发 Android 应用程序。当应用程序在前台时,将调用 onMessageReceived 并且可以从 RemoteMessage 检索数据。当应用程序在后台时,系统通知 posted。用户单击此按钮,可以从 Intent extras 中检索数据。一切顺利。

我正在尝试弄清楚如何处理在应用程序处于后台运行或死机时收到多个通知的情况。所需的行为是 "suck in" 任何活动通知并能够检索其数据有效负载。我可以使用 NotificationManager (API >= 23) 或实现 NotificationListenerService 来获取和清除(取消)活动通知。我一直无法找到一种方法来获取与它们相关的数据有效负载。 StatusBarNotification.getNotification().extras 看起来很有希望,但不包含数据。我什至使用 Android Studio 调试器检查从 getActiveNotifications() 返回的通知,但没有找到任何东西。

我认为根据我的研究,"correct" 的答案是发送 "notification" 部分用于 iOS 推送,但忽略它用于 Android 推送(即。仅数据),然后 post 当我的应用程序不在前台时,我自己在设备上本地通知。然后 onMessageReceived 应该总是被调用,我可以保存数据有效负载并拥有完全控制权。但是,我不确定此时我们是否可以更改后端服务器以支持拆分 Android/iOS 路径。根据我的阅读,我担心如果应用程序被终止,某些设备上不会调用该服务。

那么,有谁知道检索其他活动通知的数据负载的方法吗?或者有其他方法可以在不需要更改后端服务器的情况下解决这个问题?

谢谢!

I think the "correct" answer, from my research, is to send the "notification" portion for iOS pushes, but omit it for Android pushes (ie. data only), and then post my own notifications locally on the device when my app is not in the foreground. Then onMessageReceived should always get called and I can save away the data payloads and have complete control.

这是正确的做法。 (目前唯一)

However, I am not sure we can change the back-end server to support a split Android/iOS path at this point.

我们正在努力改进 API 以更好地支持您的用例。
不幸的是,我无法分享这项工作的预计到达时间。

And from I've read, I have concerns about the service not being called on certain devices if the app is killed.

这些设备 notification-messages 也不会收到 :( .
这些设备甚至不接收 AlarmManager 事件,也不接收系统广播。
如果您遇到此类设备,请联系制造商并让他们知道他们的行为不符合标准并且正在破坏您的应用程序。