当应用程序在后台时,不会调用 Firebase onMessageReceived [英] Firebase onMessageReceived not called when app is in the background

查看:39
本文介绍了当应用程序在后台时,不会调用 Firebase onMessageReceived的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我们的 Android 应用中使用 Firebase 作为我们的消息传递服务.我对 Firebase 进行了相当多的研究,我了解 应用程序是在前台运行还是现在会改变在前台收到的数据类型onMessageReceived 方法.

I am using Firebase for our messaging service in our Android app. I have researched Firebase quite a bit and I understand that 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 消息并编写自定义消息.

What I am trying to accomplish is to parse the incoming data from the Remotemessage and do something different with it depending on custom tag. IE, if the Data Map contains a field called, "My_Custom_Tag", I want to completely override the standard firebase message that pops up and write a custom one.

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

The problem is, when the app is running in the background, any code I put into the onMessageReceived never gets triggered. It works just fine when the app is running in the foreground, but will not detect/ receive anything if the app is in the background.

下面是一些示例代码:

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

My question is, how do I go about editing MyFirebaseMessagingService so that I can check the incoming data, determine tag info, and decide to either pass it on to firebase for so it can use the standard handling or not pass it to firebase and write my own custom display notification all while my app is running in the background?

谢谢大家!

PS,Firebase-Dungeon-Master Frank van Puffelen 有没有想过?>

PS, any chance the Firebase-Dungeon-Master Frank van Puffelen has a thought?

推荐答案

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

Remove the notification payload from your FCM messages in order to have the data payload delivered to the onMessageReceived method.

阅读this这个 仔细.

当您的应用程序在后台时,数据负载传送到 onMessageReceived 方法仅当没有通知负载时.(标记单词)

When your app is in the background, data payload delivered to the onMessageReceived method only if there is no notification payload. (Mark the words)

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

In case both payloads exist then system automatically handles the notification part (system tray) and your app gets the data payload in the extras of the intent of launcher Activity (after the user tap on the notification).

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

In order to be able to serve both platforms successfully, Android and iOS, you may have to send different FCM messages according to client's OS.

这篇关于当应用程序在后台时,不会调用 Firebase onMessageReceived的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆