如何获取通知中心显示的推送通知

Zon*_*ame 6 notifications system-tray swift

所有这些都来自我目前正在开发的一个应用程序,我们称这个应用程序为SampleApp

如何从我的手机托盘中获取这些通知的列表

在此处输入图片说明

是的,我知道我可以通过此代码获取通知

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)")
}
Run Code Online (Sandbox Code Playgroud)

但此代码的问题在于我只能获取我离线创建的通知,而不能获取来自Apple 推送通知服务器 (APNS) 的通知

通过离线创建我的意思是,scheduled UILocalNotifications,并且由于Push Notifications不是UILocalNotifications 我不知道该怎么做

你可能会问的一些问题

  1. 这些通知是来自我的应用程序吗?

    • 是的
  2. 我使用的didReceiveRemoteNotificationAppDelegate

    • 是的,但那是不同的。
  3. 我的远程通知是否有效/来自 APNS

    • 是的。
  4. 我注册远程通知的代码是什么样的?

    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()
    
    Run Code Online (Sandbox Code Playgroud)

Zon*_*ame 5

D\xc3\xa1vid P\xc3\xa1sztor \ 的评论部分解决了问题,因为现在我可以检索通知中心中显示的所有(推送和非推送)通知,但仅限于因为下面的代码仅适用于iOS 10iOS 10设备和以上,其他版本呢?

\n\n
UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in\n    print(notifications)\n}\n
Run Code Online (Sandbox Code Playgroud)\n