从通知中心删除远程通知

问题描述 投票:0回答:2

我需要聚合相同类型的远程通知。

示例:如果用户收到推送通知,说“用户1对您的帖子发表评论”,然后收到“用户2对您的帖子发表评论”,则在收到第二次推送时,我应删除第一个通知并创建自定义通知“ 2位用户对您的帖子发表了评论“。


我在userInfo字典中获取计数器,我正在使用NotificationService Extension来修改通知的内容。

问题是我提出了2个通知:

  1. “用户1对您的帖子发表了评论”
  2. “用户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应用程序类似的行为。

有什么建议?

ios swift push-notification ios10 unusernotificationcenter
2个回答
0
投票

推送通知分组是Android应用程序中提供的一项功能,但在iOS上无法实现相同功能。因为它应该由操作系统处理(在其他情况下,当应用程序关闭或最小化时将无法实现)并且iOS不提供此支持。


0
投票

所以我在Apple的文档中读到,当在aps对象中设置content-available : 1时,它会以后台模式启动应用程序,并且可以处理收到的静音推送。避免在应用程序配置中设置mutable-content : 1并添加带远程通知的后台模式非常重要。

© www.soinside.com 2019 - 2024. All rights reserved.