【发布时间】:2018-08-07 09:58:45
【问题描述】:
我需要聚合相同类型的远程通知。
示例: 如果用户收到推送通知说:“用户 1 评论了您的帖子”,然后收到“用户 2 评论了您的帖子”,当收到第二次推送时,我应该删除第一个通知并创建一个自定义通知说“2 个用户评论了你的帖子”。
我正在 userInfo 字典中获取计数器,并且我正在使用 NotificationService 扩展来修改通知的内容。
问题是我要显示 2 个通知:
- “用户 1 评论了您的帖子”
- “用户 3 和其他 2 人评论了您的帖子”
而不仅仅是第二个通知。
我尝试使用自定义标识符初始化 UNNotificationRequest,但我仍然收到双重通知(原始通知,然后是自定义通知)。
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["push.comment"])
if let username = bestAttemptContent.userInfo["sender"] as? String,
let count = bestAttemptContent.userInfo["views_counter"] as? Int {
bestAttemptContent.title = "\(username) and \(count) others have commented on your post"
}
bestAttemptContent.body = "tap to see"
let request = UNNotificationRequest(identifier: "push.comment", content: bestAttemptContent, trigger: nil)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
我尝试在通知的负载中使用available-content : 1,但当应用程序终止时(不在后台/前台),我无法修改通知。
基本上我想要与 facebook 的 Messenger 应用程序类似的行为。
有什么建议吗?
【问题讨论】:
-
应该是
mutable-content: 1 -
@JustinM 是的,我知道为了调用通知服务扩展我需要设置
mutable-content: 1,问题是通知出现了两次,就像我说的那样(第一次是原始的,第二次是修改)
标签: ios swift push-notification ios10 unusernotificationcenter