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

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

问题描述

我将 FirebaseMessagingService 用于我的推送通知。它正在按应用程序的意图运行。 onMessageReceived 被调用,我可以获得通知标题和正文+数据有效负载,并根据有效负载数据向我的 Activity 发送响应使用 LocalBroadcastManager

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.

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

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.

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

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.

在<$ c中接收通知标题,正文和有效内容$ c> 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
}

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
                }
            }
        }
    }


推荐答案

不幸的是,这不可能

如果推送通知的有效内容中包含通知部分,而您的应用程序在后台,则onMessageReceived将永远不会被调用。通知是由Firebase自动创建和显示的。没有办法拦截此事件并修改通知或对此做任何其他操作。

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.

如前所述,当应用程序处于前台状态时,始终会调用onMessageReceived。

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

确保onMessageReceived在所有情况下(前景,背景和应用程序被杀死)都被调用的唯一方法是从Firebase发送仅数据有效负载。也就是说,删除firebase有效内容主体的通知部分,然后将内容(标题,正文等)放入有效内容的数据部分。并在您的onMessageReceived中将此标题,正文等分配给您正在创建的通知。

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.

这是您从firebase获得的有效载荷的方式:

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"
}
}

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

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