更新已发送的通知 ios 10

Updating a delivered notifications ios 10

我正在尝试更新 iOS 10 上已发送的通知,这是一项新功能。我正在使用 api 调用通过 firebase 发送推送通知。

如果在线通知应该是 "a is online" 如果 b 也上线通知消息应更新为 "a and b are online" 如果 c 也上线通知的消息应该更新为 "a,b and c are online" 等等..

我使用了这个问题的答案How can I removed previously delivered notifications when a new notification arrives with UNUserNotificationCenterDelegate in iOS 10?。 每当我的后端发送通知时,我都会在 willPresentNotification 中编写一个逻辑来获取所需的确切消息并更新现有的通知消息。

但它仅在应用程序处于前台或后台时才有效。如果应用程序终止,则前端中没有任何内容可以执行以更新通知消息。所以每次后端发送一个通知

我收到 "a is online"、"b is online" 和 "c is online" 所有单独的通知。

那么,如何从后端获得所需的行为??我是否必须将我推送的通知的标识符保存在数据库中并使用它们远程更新消息? iOS 甚至允许后端服务器从远程设备的通知中心获取通知详细信息吗??如果我们使用来自后端的标识符,它会相应地更新消息吗??

谢谢

每次创建 更新 通知...只需使用 相同 identifier。有关更多信息,请参阅此 moment 的 WWDC 视频。

以上答案适用于本地通知,但您正在寻找远程通知...

仍然看到相同的时刻来理解想法...唯一的区别是对于本地通知,您有 identifier...对于远程通知,您有 apns-collapse-id header :

所以只需给 apns-collapse-id 一个值,每次发送一个新值时它都会更新前一个值。显然,如果用户在应用程序中,那么你就不走运了,因为他们已经收到了通知。这仅在用户尚未打开通知(也不在应用程序中)时才有效。如果他们打开了它,则会发送 new 通知。

apns-collapse-id

Multiple notifications with the same collapse identifier are displayed to the user as a single notification. The value of this key must not exceed 64 bytes. For more information, see Quality of Service, Store-and-Forward, and Coalesced Notifications.

From Apple docs:

To allow the coalescing* of similar notifications, you can include a collapse identifier within a notification request. Normally, when a device is online, each notification request that you send to APNs results in a notification delivered to the device. However, when the apns-collapse-id key is present in your HTTP/2 request header, APNs coalesces requests whose value for that key is the same. For example, a news service that sends the same headline twice could use the same collapse identifier value for both requests. APNs would then coalesce the two requests into a single notification for delivery to the device. For details on the apns-collapse-id key.


*: 聚在一起形成一个整体