【问题标题】:iOS handle multiple APN when App closedApp关闭时iOS处理多个APN
【发布时间】:2015-11-16 17:03:17
【问题描述】:

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

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

任何帮助将不胜感激。

【问题讨论】:

    标签: ios swift apn


    【解决方案1】:

    他们没有获取未交付到您的应用的 APN 的 API。

    一般而言,APN 不提供任何交付保证

    【讨论】:

    • 所以当我的设备已经收到通知并且我点击其中一个通知时,没有办法获取所有通知?如果是这样,对我来说唯一的解决方案是将每个推送通知保存在我的后端并在用户单击通知时下载它。这对我来说似乎有点无效.. 我很难相信这是不可能的:( 我一直认为交付保证意味着从 Apple 到设备,而不是从设备到应用程序?
    • 很抱歉,事情就是这样。您可以查看其他静默推送。也许这会奏效。但通知主要是针对用户的
    • 对不起@Daij-Djan,这不是 3k1 一开始就问的。我遇到了同样的情况,并且不是在寻找 APN api 来获取“未送达”通知。通知已经在那里了。我猜他只是为了礼貌而将答案标记为正确,但问题仍然没有解决
    • 这是他问的。对不起你的问题。多年来情况也没有改变
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-25
      • 2018-12-23
      • 1970-01-01
      • 2011-10-25
      • 2012-03-30
      • 2017-11-19
      • 2017-05-08
      • 1970-01-01
      相关资源
      最近更新 更多