Calling NUserNotificationCenter getDeliveredNotifications in iOS 11 always returns empty array

I am trying to retrieve all delivered notifications still showing in the notification center but

UNUserNotificationCenter getDeliveredNotifications(completionHandler:)

just doesn't work. I can get all the pending notifications with

UNUserNotificationCenter getPendingNotificationRequests(completionHandler:)

but

getDeliveredNotifications

will simply always return a 0 count even though there are notifications on the notification center.


I have noticed that this works in iOS 10 but not in iOS 11. Anyone has found a way to fix this?

Hi!


I did face the same problem with `getDeliveredNotification`, the array of notifications were always empty.

So, I realized that I was setting `[UIApplication sharedApplication].applicationBadgeNumber` to 0, what clear all remote notifications from notification center.

I created an empty project, with just scheduling notifications (nothing on setting badge). getDeliveredNotifications just keeps returning empty array. This started to happen since iOS 11.2

Looks like I'm facing the same problem here.

In addition to getDeliveredNotifications returning an empty array, removeDeliveredNotifications(withIdentifiers:) just doesn't seem to do anything at all when called with identifiers of delivered notifications that are still present in the notification center.


I can also confirm that both methods behave correctly on a device running iOS 10. The bug definitely exists on 11.2 (don't know about 11.0 and 11.1).

Hi,

I'm facing the same issue in iOS 13.3.1 calling those methods inside

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

of UNNotificationServiceExtension class.


Have you found any solutions / workaorunds?


Thanks.

--T

I found the solution and now it's working properly.

As described here:

https://developer.apple.com/documentation/usernotifications/unusernotificationcenter/1649520-getdeliverednotifications

The

completionHandler
is executed on background thread and the array of notifications cloud not be ready immediatelly.


I used a DispatchSemaphore in order to wait and get the notiification array.


let semaphore = DispatchSemaphore(value: 0)

let center = UNUserNotificationCenter.current()

let customID = data["custom-payload-id"] as? String ?? "-1";

center.getDeliveredNotifications { notifications in

// background Thread here

let matching = notifications.first(where: { notify in

let id = userInfo["custom-payload-id"] as? String

return id == customID

})

if let matchExists = matching {

center.removeDeliveredNotifications(

withIdentifiers: [matchExists.request.identifier])

}

semaphore.signal()

}


// main thread here

semaphore.wait()



--T

Calling NUserNotificationCenter getDeliveredNotifications in iOS 11 always returns empty array
 
 
Q