iOS 应用关闭时处理多个 APN
iOS handle multiple APN when App closed
当我的应用程序关闭并且我得到多个 APN 并单击其中一个时,我只会得到我单击的 APN 的数据。所有其他通知消失。
如何获取未点击的通知数据?
我目前正在这样处理我的通知:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let type: UIUserNotificationType = [UIUserNotificationType.Badge, UIUserNotificationType.Alert, UIUserNotificationType.Sound]
let setting = UIUserNotificationSettings(forTypes: type, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(setting)
UIApplication.sharedApplication().registerForRemoteNotifications()
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary{
self.application(application, didReceiveRemoteNotification: remoteNotification as [NSObject : AnyObject])
}
return true
}
和:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){
if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {
if let message = alert["message"] as? NSString {
//handle push message
}
} else if let alert = aps["alert"] as? NSString {
//handle push message
}
}
}
另外:如果我收到通知并通过单击应用程序徽标而不是单击通知来打开应用程序,所有通知似乎也会消失。
非常感谢任何帮助。
他们无法 api 获取未传送到您的应用程序的 APN。
一般来说,APNs 不提供投递保证
与 Swift 4 (iOS 11) 一起工作
您必须注意您的应用何时激活:
(将此添加到您的 AppDelegate):
func applicationDidBecomeActive(_ application: UIApplication) {
// whenever the app gets active state, check for unprocessed notifications
let center = UNUserNotificationCenter.current()
center.getDeliveredNotifications(completionHandler: { (notifications: [UNNotification]) in
print("count", notifications.count)
for notification in notifications {
print(notification.request.content.body)
}
// after working out all the notifications, you can decide to get rid of all or some with this two methods:
// removeDeliveredNotifications(withIdentifiers:)
// removeAllDeliveredNotifications
})
}
- 出于某种原因,Apple 将本地通知命名为 "Pending",将远程通知命名为 "Delivered",因此您可以独立处理两者。
当我的应用程序关闭并且我得到多个 APN 并单击其中一个时,我只会得到我单击的 APN 的数据。所有其他通知消失。
如何获取未点击的通知数据?
我目前正在这样处理我的通知:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let type: UIUserNotificationType = [UIUserNotificationType.Badge, UIUserNotificationType.Alert, UIUserNotificationType.Sound]
let setting = UIUserNotificationSettings(forTypes: type, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(setting)
UIApplication.sharedApplication().registerForRemoteNotifications()
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary{
self.application(application, didReceiveRemoteNotification: remoteNotification as [NSObject : AnyObject])
}
return true
}
和:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){
if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {
if let message = alert["message"] as? NSString {
//handle push message
}
} else if let alert = aps["alert"] as? NSString {
//handle push message
}
}
}
另外:如果我收到通知并通过单击应用程序徽标而不是单击通知来打开应用程序,所有通知似乎也会消失。
非常感谢任何帮助。
他们无法 api 获取未传送到您的应用程序的 APN。
一般来说,APNs 不提供投递保证
与 Swift 4 (iOS 11) 一起工作
您必须注意您的应用何时激活:
(将此添加到您的 AppDelegate):
func applicationDidBecomeActive(_ application: UIApplication) {
// whenever the app gets active state, check for unprocessed notifications
let center = UNUserNotificationCenter.current()
center.getDeliveredNotifications(completionHandler: { (notifications: [UNNotification]) in
print("count", notifications.count)
for notification in notifications {
print(notification.request.content.body)
}
// after working out all the notifications, you can decide to get rid of all or some with this two methods:
// removeDeliveredNotifications(withIdentifiers:)
// removeAllDeliveredNotifications
})
}
- 出于某种原因,Apple 将本地通知命名为 "Pending",将远程通知命名为 "Delivered",因此您可以独立处理两者。