如何让推送通知显示在通知中心
How to get the Push Notifications displayed in the Notification center
所有这些都来自我目前正在开发的一个应用程序,我们称这个应用程序为 SampleApp
如何从我的 Phone 的托盘
中获取这些通知的列表
是的,我知道我可以通过此代码接收通知
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 推送通知服务器 (APNS) )
我的意思是离线创建,计划 UILocalNotification
s,因为 Push Notifications 不是 UILocalNotification
s 我不知道该怎么做
您可能会问的一些问题
这些通知来自我的应用程序吗?
- 是
我在 AppDelegate
上使用 didReceiveRemoteNotification
吗?
- 是的,但那是不同的。
我的远程通知 work/come 来自 APNS
- 是的。
我注册远程通知的代码是什么样的?
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 的评论部分解决了这个问题,因为现在我可以检索显示在 Notification 中的 all(推送和非推送)通知居中,但只针对iOS 10
,因为下面的代码只针对iOS 10
以上的设备,其他版本呢?
UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in
print(notifications)
}
所有这些都来自我目前正在开发的一个应用程序,我们称这个应用程序为 SampleApp
如何从我的 Phone 的托盘
中获取这些通知的列表是的,我知道我可以通过此代码接收通知
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 推送通知服务器 (APNS) )
我的意思是离线创建,计划 UILocalNotification
s,因为 Push Notifications 不是 UILocalNotification
s 我不知道该怎么做
您可能会问的一些问题
这些通知来自我的应用程序吗?
- 是
我在
AppDelegate
上使用didReceiveRemoteNotification
吗?- 是的,但那是不同的。
我的远程通知 work/come 来自 APNS
- 是的。
我注册远程通知的代码是什么样的?
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 的评论部分解决了这个问题,因为现在我可以检索显示在 Notification 中的 all(推送和非推送)通知居中,但只针对iOS 10
,因为下面的代码只针对iOS 10
以上的设备,其他版本呢?
UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in
print(notifications)
}