有什么方法可以在后台使用 FirebaseMessagingService 从推送通知中获取数据? [英] Is there any way how to get data from push notification using FirebaseMessagingService in background?

本文介绍了有什么方法可以在后台使用 FirebaseMessagingService 从推送通知中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的推送通知使用 FirebaseMessagingService.它在应用程序中按预期工作.onMessageReceived 被调用,我可以获得通知标题和正文 + 数据负载,并使用 LocalBroadcastManager 将取决于负载数据的响应发送到我的 Activity.p>

但是后台推送通知有问题.这只会在后台显示通知,但它不是在 onMessageReceived 中创建的,因为如果应用程序在后台,则无法调用此函数.

根据文档,推送通知的数据部分存储在extras中,可以在Activity恢复时找到.我已经为此功能及其工作.但问题是我没有标题和消息,因为它没有发送到 onMessageReceived 并且我无法捕获此信息.

有什么方法可以获取吗?因为我需要在应用程序内的对话窗口中显示它.推送通知不仅仅是文本和标题,它不仅仅是用户的信息,它还会做一些动作.

FirebaseMessagingService中接收通知标题、正文和有效负载:

覆盖 fun onMessageReceived(msg: RemoteMessage?) {val pNotification = msg?.notificationval 有效载荷 = msg?.data如果(pNotification!= null){val ntitle = pNotification.titleval nMsg = pNotification.body}//使用有效负载 - 为 LocalBroadcastmanager 创建通知和 Intent}

onResume()

中的 extras 中捕获有效负载

私人乐趣可能FindBackgroundPayload(extras: Bundle?){如果(额外!= null){extras.keySet().forEach { key->val keyValue = extras[key]App.log("BackgroundKeyValues: $keyValue")if (key.equals("gcm.notification.body", ignoreCase = true) && keyValue != null){...//使用有效载荷数据键值 - 没有标题和正文}}}}

解决方案

不幸的是,它不可能按照您希望的方式工作.

如果推送通知在负载中包含通知部分并且您的应用程序在后台,则永远不会调用 onMessageReceived.通知由 firebase 自动创建和显示.没有办法拦截它并修改通知或为此执行任何其他操作.只有点击通知后,您的应用才会获得控制权.

当应用程序处于前台时,如您所述,将始终调用 onMessageReceived.

确保在所有情况下(前台、后台和应用被终止)都调用 onMessageReceived 的唯一方法是从 firebase 发送仅数据负载.也就是说,删除 firebase 有效负载正文的通知部分,并将内容(标题、正文等)放入有效负载的数据部分.并在您的 onMessageReceived 中将此标题、正文等分配给您正在创建的通知.

以下是来自 firebase 的有效负载的需要:

<代码>{注册ID":[firebase_token"],数据":{"body":"通知正文","title":"通知标题",其他数据键":其他数据值"}}

然后在您的 onMessageReceived 函数中,您需要显式提取标题和正文并将其分配给通知(当然还有您想要执行的任何其他操作):

@Override公共无效onMessageReceived(远程消息消息){映射<字符串,字符串>dataMap = message.getData();字符串标题 = dataMap.get("title");String body = dataMap.get("body");String otherdatavalue = dataMap.get("otherdatakey");NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setContentTitle(标题).setContentText(正文).setContentIntent(pendingIntent);//处理点击通知}

I'm using FirebaseMessagingService for my push notifications. It is working as intented in app. onMessageReceived is called, I can get notification title and body + data payload and send reaction depends on payload data to my Activity using LocalBroadcastManager.

But there is problem with background push notification. This will only show notification in background, but it is not created in onMessageReceived because this function cannot be called if app is in background.

According to documentation, data part of push notification is stored in extras and can be found when Activity is resumed. I have function for this already and its working. But problem is that I dont have title and message, because it was not send to onMessageReceived and I cannot catch this information.

Is there any way how to obtain it? Because I need to show it in my dialog window inside app. Push notification is not only text and title and it is not just information for user, but it will do some action.

Receiving notification title, body and payload in FirebaseMessagingService:

override fun onMessageReceived(msg: RemoteMessage?) {
        val pNotification = msg?.notification
        val payload = msg?.data
        if (pNotification != null){
            val ntitle = pNotification.title
            val nMsg = pNotification.body
        }
        //working with payload - creating notification and Intent for LocalBroadcastmanager
}

Catching payload from extras inside onResume()

private fun maybeFindBackgroundPayload(extras: Bundle?){
        if (extras != null){
            extras.keySet().forEach { key->
                val keyValue = extras[key]
                App.log("BackgroundKeyValues: $keyValue")
                if (key.equals("gcm.notification.body", ignoreCase = true) && keyValue != null){
                    ...
                    //Working with payload data key value - dont have title and body
                }
            }
        }
    }

解决方案

Unfortunately, it is not possible the way you want it to work.

If the push notification has a notification part in the payload and your app is in the background, onMessageReceived will never be called. The notification is automatically created and displayed by firebase. There is no way to intercept this and modify the notification or do any other operation for that matter. Your app will only get control once the notification is clicked.

When the app is in foreground, as you mentioned, onMessageReceived will always be called.

The only way to ensure that onMessageReceived is called in all situations(foreground, background and app killed) is to send a data-only payload from firebase. That is, remove the notification part of the firebase payload body and put the content(title,body etc) in the data part of the payload. And in your onMessageReceived assign this title, body etc to the notification you are creating.

Here is how your payload from firebase will need to be:

{
"registration_ids":[
  "firebase_token"
],
"data":{
    "body":"body of notification",
    "title":"title of notification",
    "otherdatakey":"otherdatavalue"
}
}

Then in your onMessageReceived function, you need to explicitly extract the title and body and assign it to the notification(along with any other operations you want to do of course):

@Override
public void onMessageReceived(RemoteMessage message) {
Map<String, String> dataMap = message.getData();
String title = dataMap.get("title");
String body = dataMap.get("body");
String otherdatavalue = dataMap.get("otherdatakey");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(title)
            .setContentText(body)
            .setContentIntent(pendingIntent);//To handle click of notification

}

这篇关于有什么方法可以在后台使用 FirebaseMessagingService 从推送通知中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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