iOS 10 How to view a list of pending notifications using UNUserNotificationCenter?

aestusLabs picture aestusLabs · Oct 26, 2016 · Viewed 12.7k times · Source

Solution Code:

let center = UNUserNotificationCenter.current()
print(center.getPendingNotificationRequests(completionHandler: { error in
        // error handling here
    }))

My original post:

I am trying to get a list of pending notifications via UNUserNotificationCenter as UIApplication.shared.scheduledLocalNotifications was depreciated.

This is the code I'm using:

let center = UNUserNotificationCenter.current()
    print(UNUserNotificationCenter.getPendingNotificationRequests(center))

However this prints "(Function)". getPendingNotificationRequests requires a UNUserNotificationCenter parameter and I can't think of what else it could be.

Thanks

Answer

Jerry picture Jerry · Oct 29, 2016

The getPendingNotificationRequests call passes an array of requests to the completion closure. Try something like this:

let center = UNUserNotificationCenter.current()
center.getPendingNotificationRequests(completionHandler: { requests in
    for request in requests {
        print(request)
    }
})