我在我们的 Android 应用程序中使用 Firebase 作为我们的消息服务。我对 Firebase 进行了相当多的研究,并且我了解 应用程序在前台运行还是现在运行是否会更改 onMessageReceived 方法中接收到的数据类型。
我想要完成的是解析来自 Remotemessage 的传入数据,并根据自定义标记对其执行不同的操作。 IE,如果数据映射包含一个名为“My_Custom_Tag”的字段,我想完全覆盖弹出的标准 firebase 消息并编写自定义消息。
问题是,当应用程序在后台运行时,我放入 onMessageReceived 的任何代码都 不会 被触发。当应用程序在前台运行时它工作得很好,但如果应用程序在后台运行则不会检测/接收任何东西。
下面是一些示例代码:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Map<String, String> aMap = remoteMessage.getData();
//Custom object here for easier parsing
PushNotificationsPojo pojo = this.convertResponseToPojo(aMap);
if(pojo == null){
passToFirebase(remoteMessage);
return;
}
if(pojo.getData().getCustomTag().equals(PushNotificationsPojo.ID_TAG_CHAT_MESSAGE)) {
createCustomNotification1(pojo);
} else if (pojo.getData().getCustomTag().equals(PushNotificationsPojo.ID_TAG_SOME_OTHER_MESSAGE)){
createCustomNotification2(pojo);
} else if (pojo.getData().getCustomTag().equals(PushNotificationsPojo.ID_TAG_STUFF_AND_WHATNOT)) {
createCustomNotification3(pojo);
} else {
passToFirebase(remoteMessage);
}
}
我的问题是,我该如何编辑 MyFirebaseMessagingService 以便我可以检查传入数据、确定标签信息并决定将其传递给 firebase 以便它可以使用标准处理或不将其传递给 firebase 并编写我的当我的应用程序在后台运行时拥有自定义显示通知?
谢谢大家!
附言, Firebase-Dungeon-Master Frank van Puffelen 是否有任何想法?
原文由 PGMacDesign 发布,翻译遵循 CC BY-SA 4.0 许可协议
从您的 FCM 消息中移除通知负载,以便将数据负载传送到 onMessageReceived 方法。
仔细阅读 这个 和 这个。
当您的应用程序处于后台时, 只有在没有通知有效载荷 的情况下,数据有效载荷才会传送到 onMessageReceived 方法。 ( 标记的话)
如果两个有效载荷都存在,则系统会自动处理通知部分(系统托盘),并且您的应用程序会在启动器活动的意图的附加部分中获取数据有效载荷(在用户点击通知后)。
为了能够成功地为 Android 和 iOS 这两个平台提供服务,您可能必须根据客户端的操作系统发送不同的 FCM 消息。