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

4

22

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

Instigation answered 26/10, 2016 at 19:24 Comment(3)
Call that function on center, not on the class and provide a callback handler developer.apple.com/reference/usernotifications/…Broder
Can you mark my answer as accepted?Apure
First make sure you've authorization, then you can schedule notification.Pituri
A
47

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)
    }
})
Apure answered 29/10, 2016 at 4:57 Comment(6)
It's always giving me 0 in ios 13.. I am using the device not emulator.. any idea about this ?Duntson
@Duntson Are you sure they are being scheduled correctly? Do the notifications show properly?Instigation
Notifications are getting scheduled properly. But getting requests count as 0.Duntson
@Duntson I'm having the same issue. Have you figured it out already?Lidalidah
How do you print the title? it just shows redacted for all of themFroude
Make sure your app has requested the authorization to send notifications and you have accepted. That might be the reason for some of you to see 0.Euroclydon
C
4

Just in case anyone needs to access notifications that are already delivered (user can see them in notification center), this can be done in the following way:

    let center = UNUserNotificationCenter.current()
    center.getDeliveredNotifications { notifications in
            // use center.removeDeliveredNotifications(withIdentifiers:requestIdsToRemove)
            // if you want to cancel some of the notifications displayed to user
        }
    }
Captainship answered 6/10, 2020 at 19:10 Comment(0)
V
0

it's not actually now, but you should allow to use notifications

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
        if !granted {
            print("user has declined notifications")
        }
    }
Vernal answered 3/3, 2020 at 18:20 Comment(0)
U
-1

For ObjC:- [[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest*> *requests){ NSLog(@"requests: %@", requests); }];

Urbanize answered 21/3, 2021 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.