如何获取通知中心显示的推送通知 [英] How to get the Push Notifications displayed in the Notification center

查看:45
本文介绍了如何获取通知中心显示的推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

if #available(iOS 10.0, *) {UNUserNotificationCenter.current().getPendingNotificationRequests { (requests) inprint("这里是 iOS 10 通知 \(requests)")}} 别的 {让请求 = (UIApplication.shared.scheduledLocalNotifications ?? [])print("这里是非 iOS 10 通知 \(requests)")}

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

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

您可能会问的一些问题

  1. 这些通知是否来自我的应用?

    • 是的
  2. 我是否在 AppDelegate 上使用了 didReceiveRemoteNotification?

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

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

    if #available(iOS 10.0, *) {让 authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]让中心 = UNUserNotificationCenter.current()center.delegate = 自己center.requestAuthorization(options: authOptions, completionHandler: { (_, _) in })} 别的 {让设置 = UIUserNotificationSettings(类型:[.alert,.badge,.sound],类别:nil)application.registerUserNotificationSettings(settings)}application.registerForRemoteNotifications()

解决方案

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

UNUserNotificationCenter.current().getDeliveredNotifications {(通知)在打印(通知)}

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

解决方案

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

这篇关于如何获取通知中心显示的推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆