Basically, I'm simply trying to print the notifications that my app has delivered, but doing something like:
UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in
print(notifications)
}
just displays an empty array in the Xcode console, even when the notification center contains several delivered notifications. (print(notifications.count)
returns 0)
I can use getPendingNotificationRequests
to successfully grab the notifications that are scheduled yet undelivered, update them, and even remove them with removePendingNotificationRequests(withIdentifiers:)
. But any attempt to access/modify/remove anything that's been delivered does not work.
Am I missing something simple here? Did something change recently with the way iOS 10+ accesses the delivered notifications?
It was empty when I used it on a UNNotificationServiceExtension
, it turned out that I needed to use a Semaphore in order to prevent the extension to return 0 results:
let semaphore = DispatchSemaphore(value: 0)
let center = UNUserNotificationCenter.current()
center.getDeliveredNotifications { notifications in
defer {
semaphore.signal()
}
let relatedNotificationIdentifiers = notifications.filter { notification in
notification.request.content.userInfo["callId"] as? String == callId
&& notification.request.identifier != request.identifier
}.map(\.request.identifier)
// This call is async
center.removeDeliveredNotifications(withIdentifiers: relatedNotificationIdentifiers)
// ...so this call needs to be done with a slight delay because otherwise
// it will be killed before it is done removing the notifications
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
contentHandler(bestAttemptContent)
}
}
semaphore.wait()
Also, in the end it needed some wait to execute removeDeliveredNotifications
which is async under the hood as well.
apns-collapse-id
can help reducing incoming APNS messages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With