简体   繁体   English

当应用程序在后台时更新Firebase推送通知

[英]Updating Firebase push notification when app is in background

How to update a Firebase push notification containing data payload when the app is in background? 当应用程序在后台运行时,如何更新包含数据有效负载的Firebase推送通知? Is there a way to specify the notification id in the notification to the Firebase API? 是否可以在Firebase API的通知中指定通知ID?

My request json to the firebase api. 我对Firebase API的请求json。

{
"registration_ids": ["device id"], 
"collapse_key": "Updates Available"
"notification": {
                    "title": "title", 
                    "desc": "description",
                    "body": "Message received", 
                    "sound": "TYPE_NOTIFICATION",
                    "click_action": "sometargetAction"
                },
"data":         {
                    "user": 
                    {
                        "id": 2
                        "name":"leapingwolf", 
                        "occupation": "passionate coder"
                    }
                }
}

I use the id of the "user" to append to a delivered push notification when the app is in foreground in the onMessageReceived function like so 当应用程序位于onMessageReceived函数的前台时,我使用“用户”的ID附加到传递的推送通知中,如下所示

        User user = remoteMessage.getData().get("user");
        Gson gson = new GsonBuilder().create();
        User userModel =  gson.fromJson(user, User.class);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("FCM Message With Payload")
                .setContentText(messageBody)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(userModel.getId(), notificationBuilder.build());

The full project is in github https://github.com/akshatashan/FirebaseCloudMessagingDemo 完整项目位于github https://github.com/akshatashan/FirebaseCloudMessagingDemo

I figured out the solution based on the answers posted in the link How to handle notification when app in background in Firebase 我根据链接中发布的答案找出了解决方案, 当应用程序在Firebase中后台运行时如何处理通知

To summarise, if the request json does not have the notification tag, then onMessageReceived is called irrespective of whether or not the app is in background. 总而言之,如果请求json没有通知标签,则无论该应用程序是否在后台,都将调用onMessageReceived。 Send all relevant field in the data tag and parse it in the onMessageReceived. 发送数据标签中的所有相关字段,并在onMessageReceived中对其进行解析。

I think what you are looking for is Collapsible Messages : 我认为您正在寻找的是可折叠消息

A collapsible message is a message that may be replaced by a new message containing the same collapse key if it has yet to be delivered to the device. 可折叠消息是一条消息,如果尚未传递到设备,则可以用包含相同折叠键的新消息替换。

For both message types (notification and data), it seems they can be both set as collapsible , in your case, you were asking for the data payload: 对于这两种消息类型 (通知和数据),似乎可以将它们都设置为可折叠 ,在您的情况下,您是在要求数据有效载荷:

Data message 数据信息

  • Client app is responsible for processing data messages. 客户端应用负责处理数据消息。 Data messages have only custom key-value pairs. 数据消息仅具有自定义键值对。
  • Use your app server and FCM server API: Set the data key only. 使用您的应用服务器和FCM服务器API:仅设置数据密钥。 Can be either collapsible or non-collapsible . 可以是可折叠的或不可折叠的

So to put it simply, you just have to make use of the collapse_key accordingly: 简而言之,您只需要相应地使用crash_key即可

This parameter identifies a group of messages (eg, with collapse_key: "Updates Available" ) that can be collapsed, so that only the last message gets sent when delivery can be resumed. 此参数标识可以折叠的一组消息(例如,带有falling_key:“可用更新” ),以便在可以恢复传递时仅发送最后一条消息。 This is intended to avoid sending too many of the same messages when the device comes back online or becomes active. 这是为了避免在设备重新联机或变为活动状态时发送太多相同的消息。

Note that there is no guarantee of the order in which messages get sent. 请注意,不能保证发送消息的顺序。

Note: A maximum of 4 different collapse keys is allowed at any given time. 注意:在任何给定时间最多允许使用4个不同的折叠键。 This means a FCM connection server can simultaneously store 4 different send-to-sync messages per client app. 这意味着FCM连接服务器可以为每个客户端应用程序同时存储4种不同的发送同步消息。 If you exceed this number, there is no guarantee which 4 collapse keys the FCM connection server will keep. 如果超过此数字,则无法保证FCM连接服务器将保留4个折叠键。

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

相关问题 Android:当应用未运行或在后台运行时,将Firebase推送通知保存到磁盘 - Android: saving a Firebase push notification to disk when the app is not running or in background 应用程序在后台时,Android Firebase推送通知 - Android-Firebase Push Notification when app is in background 当应用在后台时,Firebase推送通知不会播放声音 - Firebase Push notification is not playing sound when app is in background Firebase 在 android 中从后台删除应用程序时未收到推送通知 - Firebase push notification not received when app remove from background in android 当应用在后台时推送通知 - Push notification when app is in background Flutter 应用程序 Firebase 推送通知不在后台出现 - Flutter App Firebase push notification not coming in background 为什么当应用程序处于后台时声音不响(Android应用程序中的Firebase推送通知)? - Why Sound is not ringing when app is in background(Firebase Push Notification in Android app)? 仅当应用程序在后台时发送推送通知 - Sending a push notification only when app is in background 当应用程序在后台时,Cordova推送通知不起作用 - Cordova push notification not working when app is in background 当应用程序在Android TV上处于后台时推送通知 - Push notification when app is in the background on Android TV
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM