关于 android:如何使用 Firebase 将多个通知折叠成一个通知?

How to collapse multiple notifications into a single one using Firebase?

我在 Firebase Cloud Functions 中有一个函数,用于向我的应用程序中的特定用户发送通知,并且具有作为 notificationContent 的以下代码:

1
2
3
4
5
6
7
8
const notificationContent = {
    notification: {
        title:"My Notification Title",
        body:"My Notification Body",
        icon:"default",
        sound :"default"
    }
};

我尝试使用 collapse_key:"unique_key" 但它没有效果。我读到只有在设备离线时才有效。我也使用了 tag:"unique" 但每次有新通知到达时,它都会覆盖最旧的通知。

我可以通过 Firebase 实现这一目标吗?如果我收到多于一个通知,要归为一个?

enter


如果您想使用更多可定制和高级的通知功能。
您应该只发送带有 data 负载的 FCM,并在 android 客户端创建通知。
请记住,如果您使用 notification 有效载荷或 notification data 有效载荷发送 FCM,则通知将由 android 核心系统创建,并且 BroadcastReceiveronReceive 方法不会被调用,如果您的应用程序是在背景上。
如果您发送带有 data 负载的 FCM,它会一直调用 onReceive,因此您可以在 android 客户端手动生成自定义通知。 (大多数应用程序使用后一种方法。)

我希望这个链接会有所帮助。


我也有同样的困惑,并意识到我误解了 collapseKey 和 tag 的用途。

collapseKey 将限制客户端离线时收到的通知数量,标签是将通知堆叠在抽屉中的东西。

所以对于一个典型的云函数,它应该是这样的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  const notification = {
    notification: {
      'title': 'Interesting title',
      'body': 'Hello, world'
    },
    'data': {
      'whatever': whatever,
    },
    'android':{
      'collapseKey': collapseKey,
      'priority': 'high',
      'notification': {
        'tag': tag,
      }
    },
    'token': fcmToken
  };

  admin.messaging().send(notification)

请注意,"tag" 参数位于 android 通知内,而不是顶级通知内。


最简单、最灵活的解决方案是扩展 FirebaseMessagingService 并自己处理通知。但首先不是在您的云函数中的 notificationContent 上使用 notification,您必须将其更改为 data 以便您发送数据消息而不是通知消息。不同的是,通知消息会有一个隐式的折叠键(应用程序的包名),而数据消息不会有。但是数据报文需要在客户端处理,否则不显示。

这是您的 FirebaseMessagingService 所需的示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class MyFCMService extends FirebaseMessagingService {

  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getNotification() != null) {
      //this notification was sent from the Firebase console, because in our cloud function, we are using the DATA tag, not the notification tag
      //so here we have to handle the notification that was sent from the console
      ...
    } else if (remoteMessage.getData().get(KEY) != null) {
      //this data message was sent from our cloud function
      //KEY is one of the keys that you are using on the cloud function
      //in your example, you are using the keys: title, body, icon, sound

      //display the notification to the user
      ...
      notificationManager.notify(TAG, ID, notificationBuilder.build());
      //you have to use the same TAG and the same ID on each notification if you want your 2nd notification to simply update the text of the first one, instead of showing as a new notification
    }
  }
}

附注。当你从你的云函数发送通知时(如果你使用 data 标签,它实际上是一条数据消息,而不是通知消息),那么这个方法将被调用,无论应用程序是否在后台或在前台。但是,当您从 firebase 控制台发送通知时,仅当应用程序在前台时才会调用此方法。如果应用程序在后台,Firebase SDK 将处理通知并将其显示给用户。在某些情况下,仅当用户未运行应用程序时才显示通知是有意义的,例如,如果您想宣传应用程序的某些新功能。在这种情况下,您可以做的是在通知控制台上使用唯一标签(例如 "display_in_foreground")并在客户端上检查该标签。如果您将其设置为 true,您甚至可以向当前正在运行该应用程序的用户显示通知,或者如果设置为 false,您可以选择不显示通知。仅当应用程序在前台时才会进行此检查。如果它在后台,则根本不会调用它,SDK 将处理以显示通知。