从通知中心删除远程通知

时间:2018-02-27 16:58:27

标签: ios swift push-notification ios10 unusernotificationcenter

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

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

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

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

  1. "用户1对您的帖子"
  2. 发表了评论
  3. "用户3和其他2位评论了您的帖子"
  4. 而不仅是第二次通知。

    我尝试使用自定义标识符初始化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应用程序类似的行为。

    任何建议?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

相关问题