Search code examples
swiftnotificationssystem-tray

How to get the Push Notifications displayed in the Notification center


All of these came from just one app I'm currently developing, let's call this app SampleApp

How do I get a list of these notifications from my Phone's Tray

enter image description here

Yes I do know that I may be able to get the notifications via this code

if #available(iOS 10.0, *) {           
    UNUserNotificationCenter.current().getPendingNotificationRequests { (requests) in
        print("here are the iOS 10 notifs \(requests)")
    }         
} else {
    let requests = (UIApplication.shared.scheduledLocalNotifications ?? [])
    print("here are the NON iOS 10 notifs \(requests)")
}

But the problem with this code is that I can only get notifications which I created offline, and not those coming from the Apple Push Notification Server (APNS)

By created offline I mean, scheduled UILocalNotifications, and since Push Notifications aren't UILocalNotifications I have no idea what to do

Some Questions You May ask

  1. Are these notifications from my app?

    • Yes
  2. Am I using the didReceiveRemoteNotification on the AppDelegate?

    • Yes, but that's different.
  3. Does my remote notifications work/come from APNS

    • Yes.
  4. What does my code for registering remote notifications look like?

    if #available(iOS 10.0, *) {
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        let center = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: authOptions, completionHandler: { (_, _) in })
    } else {
        let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }
    
    application.registerForRemoteNotifications()
    

Solution

  • Dávid Pásztor's comment partially solved the issue because now I can retrieve the all (push and non push) Notifications displayed in the Notification Center, but only for iOS 10 because the code below is only for iOS 10 devices and above, what about other versions?

    UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in
        print(notifications)
    }