【问题标题】:How to get the Push Notifications displayed in the Notification center如何在通知中心显示推送通知
【发布时间】:2017-08-17 10:04:13
【问题描述】:

所有这些都来自我目前正在开发的一个应用程序,我们称这个应用程序为 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)")
}

但是这段代码的问题是我只能获得我离线创建的通知,而不是来自Apple Push Notification Server (APNS)的通知p>

我的意思是离线创建,预定UILocalNotifications,由于推送通知不是UILocalNotifications,我不知道该怎么做

您可能会问的一些问题

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

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

    • 是的,但不一样。
  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()
    

【问题讨论】:

  • 你为什么不用UNUserNotificationCenter.current.getDeliveredNotifications()?此外,iOS 中没有“托盘”之类的东西,您所说的就是通知中心。
  • 更新答案并接受自己。
  • @user3589771 我还不能,因为我仍然无法为iOS 10以下的设备获取这些通知
  • 如果没有其他方式获取通知,请使用小于 iOS 10 的 NSUserDefault

标签: swift notifications system-tray


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2019-01-07
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    相关资源
    最近更新 更多