Up until recently (I believe prior to iOS 12 release), removing remote push notifications from the Notification Center worked as expected using removeDeliveredNotifications
.
Suddenly, without any code change in the Notification Service Extension, notifications are not removed anymore.
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)
}
}
The function just completes without removing the notification. When debugging on a real device, it shows that matchingNotifications
contains notifications and the notification IDs to remove are correctly provided.
For testing, calling removeAllDeliveredNotifications()
works and removes all notifications.
The function above is called in override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void)
What is the problem here?
didReceive
implementation? I believe it's related to when you call thecontentHandler
completion. – GorgecontentHandler
unloads/destroys your extension (I assume) which may stop the asynchronous deletion. Can you try delay callingcontentHandler
for testing purposes? (delay for a couple of seconds for example) – Gorge