Apple Push Notification Collapse Key Equivalent

前端 未结 5 1693
梦如初夏
梦如初夏 2021-02-05 05:41

When using Google push notifications, I am allowed to specify a collapse_key value so a device will not receive multiple notifications of the same collapse_key. Does APNS have a

5条回答
  •  -上瘾入骨i
    2021-02-05 06:33

    If APNs attempts to deliver a notification but the device is offline, the notification is stored for a limited period of time, and delivered to the device when it becomes available.

    Only one recent notification for a particular app is stored. If multiple notifications are sent while the device is offline, each new notification causes the prior notification to be discarded. This behavior of keeping only the newest notification is referred to as coalescing notifications.

    https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html

    So there's no need for collapse_key in iOS.

    FYI, collapse_key is only useful when the device is offline/inactive:

    This parameter identifies a group of messages (e.g., with collapse_key: "Updates Available") that can be collapsed, so that only the last message gets sent when delivery can be resumed. This is intended to avoid sending too many of the same messages when the device comes back online or becomes active (see delay_while_idle).

    https://developers.google.com/cloud-messaging/server-ref#downstream

    UPDATE:

    For both iOS and Android (using collapse_key), if the device is offline (ie the Apple/Google push server cannot reach it), the push server overwrites any previous notification, and only keeps the last one.

    If the device is online, then I guess it's up to you to do whatever you want with the received notification. At least in Android you can decide whether you want to "pile things up", whether you want to overwrite any previous notification in the notification area, or whether you want to overwrite any previous notification of the same type.

    NotificationManager notificationManager = ...;
    String appName = ...;
    NotificationCompat.Builder builder = ...
    // Always use the same id, so only the last notification will be displayed in the notification area.
    int notId = 0;
    // Always use a different id, so all notifications will pile up in the notification area
    notId = new Random().nextInt(100000);
    // Uses the type of notification as id, so you'll only have up to one notification per type
    // in the notification area. It's like using collapse_key, but on the app itself.
    // That type should should be some additional data in the notification you sent.
    notId = notificationType;
    Notification notification = builder.build();
    notificationManager.notify(appName, notId, notification);
    

提交回复
热议问题