How to get the Push Notifications displayed in the Notification center

时光怂恿深爱的人放手 提交于 2019-12-13 14:32:32

问题


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

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()
    

Update:

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)
}

来源:https://stackoverflow.com/questions/45732146/how-to-get-the-push-notifications-displayed-in-the-notification-center

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!