【问题标题】:Stack push notifications with Android Firebase使用 Android Firebase 堆栈推送通知
【发布时间】:2017-03-02 17:11:05
【问题描述】:

我开发了一个使用 Firebase 接收推送通知的 Android 应用。我的代码基于 Firebase/Google 官方文档(https://firebase.google.com/docs/cloud-messaging/android/clienthttps://firebase.google.com/docs/cloud-messaging/android/receive),并没有什么特别之处,它具有扩展 FirebaseMessagingService 的服务和扩展 FirebaseInstanceIdService 的服务。一切正常。

但是这个应用收到的通知比预期的要多,我的客户希望应用堆叠它收到的通知。 我已经看过有关解决旧 GCM 机制的解决方案的教程,但没有使用 FCM。所以我的疑问是: 是否可以使用 FCM 堆叠收到的推送通知?还是必须在后端以某种方式对其进行编码?

【问题讨论】:

  • 这也是我的问题。真的很期待 Firebase 团队的人来澄清这个话题

标签: android firebase push-notification firebase-cloud-messaging


【解决方案1】:
 NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID);
    Notification mNotification = builder
            .setLargeIcon(image)/*Notification icon image*/
            .setContentText(messageBody)
            .setSmallIcon(R.drawable.dhlone)
            .setContentTitle("DHL")
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(image).bigLargeIcon(image))/*Notification with Image*/
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
           .setBadgeIconType(R.drawable.dhlone)
            .setAutoCancel(true)

//使用set setAutoCancel(true)。它会工作的

【讨论】:

    【解决方案2】:

    堆栈通知可以使用 FCM 完成,它也需要在服务器端进行一些更改。所有这些都在这里详细解释:-

    https://stackoverflow.com/a/43913156/6157185

    【讨论】:

      【解决方案3】:

      您只需将.setGroup(GROUP_KEY_XPTO) 添加到您的通知生成器。具有相同组 id 的所有通知将被堆叠。

      final static String GROUP_KEY_EMAILS = "group_key_emails";
      
      // Build the notification, setting the group appropriately
      Notification notif = new NotificationCompat.Builder(mContext)
               .setContentTitle("New mail from " + sender1)
               .setContentText(subject1)
               .setSmallIcon(R.drawable.new_mail)
               .setGroup(GROUP_KEY_EMAILS)
               .build();
      
      // Issue the notification
      NotificationManagerCompat notificationManager =
              NotificationManagerCompat.from(this);
      notificationManager.notify(notificationId1, notif);
      

      在下一个通知上:

      Notification notif2 = new NotificationCompat.Builder(mContext)
               .setContentTitle("New mail from " + sender2)
               .setContentText(subject2)
               .setSmallIcon(R.drawable.new_mail)
               .setGroup(GROUP_KEY_EMAILS)
               .build();
      
      notificationManager.notify(notificationId2, notif2);
      

      在此处查看更多信息:https://developer.android.com/training/wearables/notifications/stacks.html

      【讨论】:

      • 这是我迄今为止找到的解决方案,但我不确定它是否对 Firebase 有效。在我的代码中,我没有调用 NotificationCompat.Builder。按照 Firebase 教程,您不需要拥有它。
      • 覆盖onMessageReceived。你需要遵循这个:firebase.google.com/docs/cloud-messaging/android/receive
      • 但只有当应用程序在前台时才会调用此方法(在这种情况下,我不需要堆叠通知)。使用 Firebase,如果应用程序已关闭或在后台,通知会发送到设备的系统托盘,并且通知是否在其中一一显示,如果我的应用程序未运行,我如何将它们堆叠在那里?
      • 为此我认为您需要使用数据消息:firebase.google.com/docs/cloud-messaging/…
      • 但是,这意味着必须在后端代码中进行一些更改,不是吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多