gpt4 book ai didi

android - 应用程序在后台时不会调用 Firebase onMessageReceived

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:36 28 4
gpt4 key购买 nike

我在 Android 应用程序中使用 Firebase 作为消息服务。我对 Firebase 进行了相当多的研究,我明白 whether the app if running in the foreground or now will change the type of data received in the onMessageReceived method .

我想要完成的是解析来自 Remotemessage 的传入数据,并根据自定义标记对其执行不同的操作。 IE,如果数据映射包含一个名为“My_Custom_Tag”的字段,我想完全覆盖弹出的标准 firebase 消息并编写自定义消息。

问题是,当应用程序在后台运行时,我放入 onMessageReceived never 中的任何代码都会被触发。当应用程序在前台运行时它工作得很好,但如果应用程序在后台运行则不会检测/接收任何东西。

下面是一些示例代码:

@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并在我的应用程序在后台运行时编写我自己的自定义显示通知?

谢谢大家!

PS,有机会Firebase-Dungeon-Master Frank van Puffelen有想法吗?

最佳答案

从您的 FCM 消息中删除通知负载,以便将数据负载传送到 onMessageReceived 方法。

阅读thisthis仔细。

当您的应用程序处于后台时,数据有效载荷传送到 onMessageReceived 方法仅当没有通知有效载荷时。 (标记单词)

如果两个有效负载都存在,则系统会自动处理通知部分(系统托盘),您的应用程序会在启动器 Activity 的额外内容中获取数据有效负载(在用户点击通知后)。

为了能够成功地为 Android 和 iOS 这两个平台提供服务,您可能必须根据客户端的操作系统发送不同的 FCM 消息。

关于android - 应用程序在后台时不会调用 Firebase onMessageReceived,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40311279/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com