直到最近(我相信在 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)
}
}
该功能刚刚完成而不删除通知。在真实设备上调试时,它显示matchingNotifications
包含通知并且正确提供了要删除的通知 ID。
对于测试,调用removeAllDeliveredNotifications()
工作并删除所有通知。
上面的函数被调用override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void)
这里有什么问题?