【问题标题】:Removing Remote Notifications From Notification Center从通知中心删除远程通知
【发布时间】:2018-08-07 09:58:45
【问题描述】:

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

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


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

问题是我要显示 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 应用程序类似的行为。

有什么建议吗?

【问题讨论】:

  • 应该是mutable-content: 1
  • @JustinM 是的,我知道为了调用通知服务扩展我需要设置mutable-content: 1,问题是通知出现了两次,就像我说的那样(第一次是原始的,第二次是修改)

标签: ios swift push-notification ios10 unusernotificationcenter


【解决方案1】:

推送通知分组是 Android 应用程序中提供的一项功能,但在 iOS 上无法实现。因为它应该由操作系统来处理(否则在应用关闭或最小化时将无法实现)而iOS不提供此支持。

【讨论】:

  • 我不同意,你可以看看facebook的实现。您可以在您的 iOS 设备上接收多条消息,一旦您在计算机上打开消息,所有来自手机的通知都会被清除,所以我知道这是可能的。顺便说一句,您可以使用Thread-Identifier 有效负载参数对通知进行分组,尽管这不是我的愿望。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多