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

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

问题描述

在我们的Android应用中,我正在使用Firebase进行消息服务。我已经对Firebase进行了相当多的研究,并且我知道应用程序是在前台运行还是现在将改变在onMessageReceived方法

我试图完成的是解析来自Remotemessage的传入数据,并根据自定义标记做不同的事情。 IE,如果数据映射包含一个名为My_Custom_Tag的字段,我想完全覆盖弹出的标准Firebase消息并写入一个自定义消息。
$ b

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



以下是一些示例代码:

  @Override 
public void onMessageReceived(RemoteMessage remoteMessage){
super.onMessageReceived(remoteMessage);

地图< String,String> aMap = remoteMessage.getData();
//定制对象在这里更容易解析
PushNotificationsPojo pojo = this.convertResponseToPojo(aMap);

if(pojo == null){
passToFirebase(remoteMessage);
return;

$ b $ 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);
$ b $ else if(pojo.getData()。getCustomTag()。equals(PushNotificationsPojo.ID_TAG_STUFF_AND_WHATNOT)){
createCustomNotification3(pojo);

} else {
passToFirebase(remoteMessage);




$ b $ p
$ b我的问题是,如何编辑MyFirebaseMessagingService以便我可以检查传入的数据,确定标签信息,并决定将其传递给firebase,以便使用标准处理或不传递给firebase,并在我的应用程序运行时编写自己的自定义显示通知背景?

感谢所有!

有一个想法吗?

解决方案

/ b>删除FCM消息中的通知有效载荷,以便将数据有效载荷传递给onMessageReceived方法。

阅读仔细。



当你的ap p在后台,只有在没有通知有效载荷的情况下,数据有效载荷传递给onMessageReceived方法。

如果存在两个有效载荷,则系统自动处理通知部分(系统托盘),您的应用程序获取数据有效载荷的发射器活动的意图(在用户点击通知后)额外的。



为了能够服务Android和iOS都成功,您可能需要根据客户端的操作系统发送不同的FCM消息。


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.

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.

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.

Here is some sample code below:

@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);
    }
}

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?

Thanks all!

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

解决方案

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

Read this and this carefully.

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

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

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天全站免登陆