App关闭时iOS处理多个APN

时间:2015-11-16 17:03:17

标签: ios swift apn

当我的应用程序关闭并且我获得多个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
        }
    }
}

另外:如果我通过点击应用徽标而不是点击通知来接收通知并打开应用,则所有通知似乎也会消失。

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:0)

他们无法获得未送到您应用的APN。

一般来说,APN不提供任何交付保证

答案 1 :(得分:0)

使用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将本地通知命名为“待处理”,而将远程通知“分配给”,因此您可以独立处理这两个通知。