【问题标题】:iOS11 swift silent push (background fetch, didReceiveRemoteNotification) is not working anymoreiOS11 快速静默推送(后台获取,didReceiveRemoteNotification)不再起作用
【发布时间】:2018-03-01 23:41:18
【问题描述】:

我希望 iOS11 版本能够解决静默推送问题,该问题存在于最新的 iOS 测试版和 GM 版本中。

目前我很难理解,为什么我没有收到任何静默推送消息,这实际上应该唤醒我的应用程序以在后台执行一些需要的任务。

在 iOS 10 中,我只使用后台获取功能并在我的 AppDelegate 中实现了“唤醒代码”,如下面的代码。

在 iOS 11 中,注册代码仍然可以正常工作,我的后端也将推送通知发送到 Apple 的 DEV 服务器(沙箱)和 PROD 服务器(生产版本)。不幸的是,函数func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) 从未被静默推送通知调用。

我真的错过了 iOS 11 的内容吗?


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  // .. some variables here ...

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

       // register silent push notification in backend
       application.registerForRemoteNotifications()

       // ... some code here ... 


       // Set Background Fetch Intervall for background services / terminated app
       UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

       // ... some code here ... 

       return true
   }

   func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
       let tokenParts = deviceToken.map { data -> String in
           return String(format: "%02.2hhx", data)
       }
       let token = tokenParts.joined()
       logger.log.debug("Device Token: \(token)")

       let realm = RealmController()
       let user = realm.getLoggedInUserObject()

       // Send push token to server
       if let user = user {
           let email = user.email!

           let serverController = ServerController.serverController
           serverController.sendPushToken(token: token, email: email) { status in
               if status == 201 {
                // ... some code here ...
               } else {
               // ... some code here ...
               }
           }
       }
   }
   func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
       logger.log.debug(error)
   }
   func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
       logger.log.debug(userInfo)

       let aps = userInfo["aps"] as! [String: AnyObject]
       if aps["content-available"] as? Int == 1 {
          // .... some silent push tasks here ....
       }
   }
}

【问题讨论】:

    标签: swift ios10 ios11 background-fetch silentpush


    【解决方案1】:

    您所经历的实际上是有据可查的行为,而不是错误。

    Apple's documentation 说: “系统将静默通知视为低优先级。您可以使用它们来刷新您的应用程序的内容,但系统不保证它们的传递。此外,如果总数过多,静默通知的传递可能会受到限制。系统允许的实际静默通知数量取决于当前情况,但不要尝试每小时发送超过两到三个静默通知。”

    总之,静默通知并不能保证唤醒您的应用。为什么?用户设备后台应用被唤醒的次数越多,电池消耗就越高。

    根据 IBM 的声明,它说:“随着 iOS 11 的出现,Apple 已经改变了他们处理某些类型推送消息的方式。这样做是为了延长电池寿命,但会以用户体验为代价可能会影响您的应用程序。”您可以找到更多详细信息here

    【讨论】:

    • 这绝对是苹果方面的一个错误。他们破坏了 ios11 操作系统中的某些内容。去年有几次关于这个时间的讨论,应用程序近 1 个月没有收到推送通知。顺便说一句,这个问题在一年前得到了回答,您无法参考当前文档
    【解决方案2】:

    2017 年 10 月 31 日最终更新

    Apple 刚刚发布了 iOS 11.1 的官方(万圣节)版本

    2017 年 10 月 9 日更新

    Apple 今天发布了 iOS11.1 beta 2。他们在发行说明中再次提到了以下说明:

    通知已解决问题

    更频繁地处理静默推送通知。 (33278611)

    我将再次测试此 beta 2 版本并更新此答案以供您参考。

    更新 - 测试 -> 经过不同场景的一些测试后,这个错误似乎在最新的 iOS11.1 beta 2 版本中得到修复。现在我们只能等待正式发布了。在一些论坛中,他们认为苹果将在 10 月下旬发布 iOS11.1。


    旧帖

    上周我调查了很多时间来寻找有关此问题的答案。在阅读了苹果发行说明(包括已弃用、更改和新功能)后,我测试了以下情况:

    我将空函数添加到我的AppDelegate 现在静默推送在前台和后台都可以再次工作

    func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
            logger.log.debug("Perform Fetch with completion handler TEST")
        }
    

    我不确定此“解决方法”是否与问题有关,即 iOS11 中未调用以下 function application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

    不过,如果您发现相同的行为,您可以尝试并给我反馈。

    2017 年 9 月 25 日更新

    在我的情况下,“静默推送”现在可以在前台和后台模式下工作,但如果应用程序被用户或操作系统暂停,则不能。所以这个问题仍然存在并且没有解决 - 任何帮助表示赞赏! 有关更多信息,另请参阅此线程: Silent pushes not delivered to the app on iOS 11


    2017 年 10 月 5 日更新

    苹果几天前发布了 iOS11.1 测试版。他们在发行说明中提到了以下内容:

    通知已解决问题
    静默推送通知的处理频率更高。 (33278611)

    一些开发者说这个问题在这个测试版中得到了解决,其他开发者说这个问题在某些情况下仍然存在。现在,当 Apple 为客户推出 iOS11.1 时会很有趣。

    【讨论】:

    • “在阅读了苹果的发行说明之后”苹果在哪里提到了这件事?
    • 他们没有提及任何内容,但我尝试了很多并发现,这个解决方案有效。
    • 好的。当您以暂停模式启动应用程序时,这对您有用吗? (方案->选中“等待启动可执行文件”然后发送推送。我的情况是,当应用程序处于此状态时,我仍然会丢失静默推送。请参阅我的“更新 13.09 - iOS 11 GM”这里@ 987654323@
    • 我刚刚尝试过 -> 如果选中“等待启动可执行文件”复选框,则应用程序不会唤醒,如果我将该选项与复选框“由于背景而启动”结合使用获取事件”。在我的情况下,它仅在您选中“由于后台提取事件而启动”和“自动”启动时才有效。
    • 是的,这与我在 SO 问题中的行为相同,这与 iOS 10 不同,在 iOS 10 中,应用程序从挂起模式恢复到后台模式并调用了委托方法。
    猜你喜欢
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多