苹果推送通知崩溃关键等效

在使用Google推送通知时,我可以指定一个collapse_key值,以便设备不会收到同一个collapse_key的多个通知。 APNS是否具有类似的function,或者是否有人知道如何模拟此function?

iOS中没有这样的function。 但是,由于推送通知是由您控制的服务器发送的,因此您可以跟踪发送给特定设备的通知,并决定是否发送新通知。 换句话说,您将逻辑放在您的服务器代码中,而不是您的iOS应用程序代码。

从iOS 10开始,使用HTTP / 2.0 APNS API,您可以指定apns-collapse-id标头并处理应用程序中的折叠逻辑。

折叠的通知将作为一个单一的通知,在设备上保持与新的数据更新。 每次更新通知时,都会将其推送到未读通知的顶部。

https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html获取的apns-collapse-id描述:

具有相同折叠标识符的多个通知作为单个通知显示给用户。 该值不应超过64个字节。 有关更多信息,请参阅服务质量,存储转发和合并通知。

并从https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW1

当设备在线时,您发送的所有通知都会发送给用户。 但是,您可以通过跨多个相同的通知使用折叠标识来避免显示重复的通知。 崩溃标识符的APN请求标头键是apns-collapse-id,在表6-2中定义。

例如,连续两次发送相同标题的新闻服务可以为两个推送通知请求使用相同的折叠标识符。 然后,APN将负责将这些请求合并为单个通知,以便交付给设备。

在iOS 10中,有一个新的“apns-collapse-id”,它看起来像处理这种需求。 如果您有Apple开发者帐户,则可以查看WWDC 2016通知会话video(707介绍videohttps://developer.apple.com/videos/play/wwdc2016/707/ )。

如果APN尝试传递通知但设备处于脱机状态,则通知将存储一段有限的时间,并在设备可用时传递给设备。

只有一个特定的应用程序最近通知被存储 。 如果在设备处于脱机状态时发送了多个通知,每个新通知都会导致事先通知被丢弃。 只保留最新通知的行为称为合并通知。

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

所以在iOS中不需要使用collapse_key

仅供参考, collapse_key仅在设备处于脱机/非活动状态时才有用:

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

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

更新:

对于iOS和Android(使用collapse_key), 如果设备处于脱机状态 (即Apple / Google推送服务器无法访问它), 则推送服务器将覆盖以前的任何通知 ,并且只保留最后一个通知

如果设备在线 ,那么我想你可以根据收到的通知做任何事情 。 至less在Android中,您可以决定是否要“堆叠起来”,是否要覆盖通知区域中的任何以前的通知,或者是否要覆盖以前的任何相同types的通知。

 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); 

在我们的情况下,我们需要我们的用户login才能够接收特定的推送通知,这对于常规推送来说是非常棘手的。 相反,我们发送后台更新通知 ,如果用户login,我们然后安排他们作为本地通知。 不幸的是,在这种情况下,似乎没有apns-collapse-id通过。

但是,对于本地通知还是有一个等价的:创buildUNNotificationRequest时,可以将identifier参数设置为您以apns-collapse-id传递的相同效果。

本次WWDC演讲中有pipe理交付通知的方便代码示例和演示 – 关键部分是18:00 – 21:00。