标签: unusernotification

为什么不使用 removeDeliveredNotifications 删除通知?

直到最近(我相信在 iOS 12 发布之前),使用removeDeliveredNotifications.

突然,通知服务扩展中没有任何代码更改,通知不再被删除。

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {

    self.contentHandler = contentHandler
    self.content = request.content.mutableCopy() as? UNMutableNotificationContent

    guard let content = content else {
        contentHandler(request.content)
        return
    }

    UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
        let matchingNotifications = notifications.filter({ $0.request.content.threadIdentifier == "myThread" && $0.request.content.categoryIdentifier == "myCategory" })
        UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: matchingNotifications.map({ $0.request.identifier }))
        contentHandler(content)
    }
}
Run Code Online (Sandbox Code Playgroud)

该功能仅完成而不删除通知。在真实设备上调试时,它显示matchingNotifications包含通知并且正确提供了要删除的通知 ID。

对于测试,调用removeAllDeliveredNotifications()有效并删除所有通知。

上面的函数被调用 override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void)

这里有什么问题?

ios unusernotificationcenter unusernotification unnotificationserviceextension

5
推荐指数
1
解决办法
1592
查看次数

UNUserNotificationCenter.current().requestAuthorization() 可以多次调用吗?

UNUserNotificationCenter.current().requestAuthorization() 可以多次调用吗?

  • 我可以在每次应用程序启动时调用吗?
  • 用户授予通知权限后可以再次调用吗?
  • 如果没有,为什么不呢?

apple-push-notifications ios unusernotificationcenter unusernotification

3
推荐指数
1
解决办法
1373
查看次数