简体   繁体   English

有什么方法可以在后台使用FirebaseMessagingService从推送通知中获取数据?

[英]Is there any way how to get data from push notification using FirebaseMessagingService in background?

I'm using FirebaseMessagingService for my push notifications. 我将FirebaseMessagingService用于我的推送通知。 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 ,我可以使用LocalBroadcastManager获取通知标题和正文+数据有效负载,并根据有效负载数据向我的Activity发送响应。

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. 这只会在后台显示通知,但不会在onMessageReceived创建通知,因为如果应用程序在后台,则无法调用此函数。

According to documentation, data part of push notification is stored in extras and can be found when Activity is resumed. 根据文档,推送通知的数据部分存储在extras ,可以在恢复Activity时找到。 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. 但是问题是我没有标题和消息,因为它没有发送到onMessageReceived ,因此我无法捕获此信息。

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 : 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() 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. 如果推送通知的有效负载中包含通知部分,而您的应用程序在后台,则永远不会调用onMessageReceived。 The notification is automatically created and displayed by firebase. 通知是由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. 如前所述,当应用程序处于前台时,将始终调用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. 确保在所有情况下(前景,背景和应用程序被杀死)都调用onMessageReceived的唯一方法是从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. 也就是说,删除firebase有效内容主体的通知部分,然后将内容(标题,正文等)放入有效内容的数据部分。 And in your onMessageReceived assign this title, body etc to the notification you are creating. 并在您的onMessageReceived中将此标题,正文等分配给您正在创建的通知。

Here is how your payload from firebase will need to be: 这是您需要从firebase获得的有效负载的方式:

{
"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): 然后,在onMessageReceived函数中,您需要显式提取标题和正文并将其分配给通知(当然还有您要执行的任何其他操作):

@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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FirebaseMessagingService 推送通知创建 - 如何设置图标? - FirebaseMessagingService push notification creation - how to set icon? FirebaseMessagingService-如何从后台更新数据库或SharedPreferences? - FirebaseMessagingService - How to update Database or SharedPreferences from the background? 使用rxjava将数据从FirebaseMessagingService传输到Activity - Transfer data from FirebaseMessagingService to Activity using rxjava 两种类型的 FCM 推送通知有效负载(通知与数据)之间的区别以及它们何时到达 Android FirebaseMessagingService? - Difference between the two types of FCM push notification payloads (notification vs data) and when do they arrive to Android FirebaseMessagingService? 收到Android(Firebase)推送时如何从通知获取数据? - How to get data from notification when receiving push for android (firebase)? Android 7.1.1推送通知和FirebaseMessagingService不起作用 - Android 7.1.1 Push Notification and FirebaseMessagingService not work 当 android 应用程序处于终止状态时,如何从推送通知(使用 FCM)中获取“数据有效负载”? - How can I get “data payload” from push notification (using FCM) when android app is in killed state? 如何从Appium中的PUSH通知中获取JSON数据? - How can i get JSON data from a PUSH notification in Appium? FirebaseMessagingService仅在后台显示通知 - FirebaseMessagingService show notification only when background 如何从后台服务/线程传递数据并获取通知? - How to pass data from a background Service/thread and get notification for it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM