1

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

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

任何帮助将不胜感激。

4

2 回答 2

0

他们不是获取未交付到您的应用程序的 APN 的 api。

一般来说,APNs 不做任何交付保证

于 2015-11-16T21:27:30.523 回答
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 将本地通知命名为“待处理”,并将“已传递”到远程通知,因此您可以独立处理两者。
于 2018-08-10T18:44:00.293 回答