当应用程序在后台时处理数据有效负载 [英] Handling data payload when app is in the background

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

问题描述

我正在尝试让我的应用在后台处理DATA有效负载.

I am trying to get my app to process the DATA payload while it is in the background.

"data": {
        "aKey": "aValue",
        "customtitle": "my title",
        "custombody": "my body",
        "customimage": "my image"
    },
    "notification": {
        "title": "my title",
        "text": "some text",
        "icon": "icon to use",
        "sound": "soundtouse"
    }

根据部分下的文档 > 在后台应用中处理通知消息

Accroding to the documentation under the section Handle notification messages in a backgrounded app

..数据有效载荷是根据启动器活动的意图提供的.

..the data payload is delivered in the extras of the intent of your launcher Activity.

在我的应用程序中,onMessageReceived仅接收一个RemoteMessage对象.

In my application, the onMessageReceived only receives a RemoteMessage object.

我尝试覆盖onMessageReceived签名以接受Intent,但这不起作用.

I tried overriding the onMessageReceived signature to accept an Intent but that doesn't work.

除了文档中的最后一句话"外,我找不到任何教程或解释.

I can find no tutorial or explanation on this other than the the VERY LAST SENTENCE in the documentation.

有人可以告诉我在应用程序处于后台或关闭状态时如何处理数据有效负载的示例吗?

Can someone tell me where there are examples on how to handle the data payload when the app is in the background or closed?

如果该应用已打开,则可以通过我的onMessageReceived(RemoteMessage)`方法很好地处理数据有效载荷.

If the app is open, then it processes the Data payload fine, from my onMessageReceived(RemoteMessage)` method.

推荐答案

您只会遇到FCM的预期行为.在您链接的文档中,该表显示了在使用时data有效负载,它将仅在邮件有效负载仅包含数据的情况下触发onMessageReceived() :

You're only encountering the expected behavior for FCM. In the documentation you linked, the table shows that when using a data payload, it will only trigger onMessageReceived() if the message payload only has data:

"data": {
        "aKey": "aValue",
        "customtitle": "my title",
        "custombody": "my body",
        "customimage": "my image"
    }

但是您在消息有效负载中同时使用了notificationdata.这意味着当您的应用程序在后台运行时,有效负载将由Android系统本身处理,并在通知托盘中显示notification详细信息.

But you are using both, notification and data in your message payload. Which means that when your app is in background, the payload will be handled by the Android system itself, showing the notification details in the Notification Tray.

然后,当该Android上的用户轻按处理通知时,它将打开您的应用程序(通常是MainActivity或您设置为使用

Then when the user taps on that Android handled notification, it should open your app (usually the MainActivity or whichever activity you set to open by using click_action) and from there, you should handle the intent like so:

    Intent intent = getIntent();
    String pos = getIntent().getStringExtra("<YOUR KEY HERE>");
    if(pos !=null){
        selectDrawerItem(navigationView.getMenu().getItem(Integer.parseInt(pos)));
    }

这篇关于当应用程序在后台时处理数据有效负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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