【问题标题】:Updating Firebase push notification when app is in background当应用程序处于后台时更新 Firebase 推送通知
【发布时间】:2016-12-12 14:20:46
【问题描述】:

当应用处于后台时,如何更新包含数据负载的 Firebase 推送通知?有没有办法在 Firebase API 的通知中指定通知 ID?

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

当应用程序在 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());

完整项目在githubhttps://github.com/akshatashan/FirebaseCloudMessagingDemo

【问题讨论】:

  • 当你说应用时,它是安卓应用吗?
  • 是的一个安卓应用

标签: android firebase-cloud-messaging


【解决方案1】:

我根据链接中发布的答案找到了解决方案 How to handle notification when app in background in Firebase

总而言之,如果请求 json 没有通知标签,则无论应用程序是否在后台,都会调用 onMessageReceived。发送数据标签中的所有相关字段,并在 onMessageReceived 中解析。

【讨论】:

  • 可以,但关闭应用时不会收到仅发送“数据”通知。
【解决方案2】:

我想你要找的是Collapsible Messages:

可折叠消息是一条消息,如果它尚未传送到设备,则可以被包含相同折叠键的新消息替换。

对于message types(通知和数据),似乎它们都可以设置为可折叠,在您的情况下,您要求的是数据有效负载:

数据信息

  • 客户端应用程序负责处理数据消息。数据消息只有自定义键值对。
  • 使用您的应用服务器和 FCM 服务器 API:仅设置数据密钥。 可折叠或不可折叠

简单地说,你只需要相应地使用collapse_key

此参数标识可以折叠的一组消息(例如,使用 collapse_key: "Updates Available"),以便在可以恢复传递时仅发送最后一条消息。这是为了避免在设备重新联机或变为活动状态时发送太多相同的消息。

请注意,无法保证消息发送的顺序。

注意:在任何给定时间最多允许使用 4 个不同的折叠键。这意味着 FCM 连接服务器可以同时为每个客户端应用程序存储 4 条不同的发送同步消息。如果超过此数量,则无法保证 FCM 连接服务器将保留哪 4 个折叠键。

【讨论】:

  • 当消息尚未传送到设备时,折叠键很有用。前任。如果设备离线时发送多条消息,当设备在线并且指定了collapse_key时,只会发送一条消息。我的要求是在应用程序处于后台时更新或替换已发送的消息。当应用程序处于前台时,我通过在 onMessageReceived 函数中为 notificationManager 指定相同的 notifyId 来实现它。
  • @Akshata 看起来你理解得很好。对于您的用例,我还考虑建议使用delay_while_idle,但那些已被弃用。无论如何,当您的应用程序处于后台时,您是否以某种方式 存储 直到应用程序进入前台的时间?您能否展示代码并详细说明您的流程?
  • 我用一些代码 sn-ps 为问题添加了更多细节。当应用程序在 bg 中时,是否有一些密钥可以与请求一起发送以指定更新传递的消息而不是创建新消息?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多