iOS11快速无声推送(后台获取,didReceiveRemoteNotification)不再起作用 [英] iOS11 swift silent push (background fetch, didReceiveRemoteNotification) is not working anymore

查看:138
本文介绍了iOS11快速无声推送(后台获取,didReceiveRemoteNotification)不再起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望iOS11版本能够解决无声推送问题,该问题已在最新的Beta和GM版本的iOS中出现.

当前,我正在努力理解为什么我没有收到任何静默的推送消息,而这实际上应该唤醒我的应用程序以在后台执行一些所需的任务.

在iOS 10中,我仅使用后台获取功能,并在AppDelegate中实现了唤醒代码",如以下代码所示.

在iOS 11中,注册代码仍然可以正常工作,我的后端还向Apple DEV服务器(沙盒)和PROD服务器(生产版本)传递了推送通知.不幸的是,功能 func application(_ application:UIApplication,didReceiveRemoteNotification userInfo:[AnyHashable:Any],fetchCompletionHandler completeHandler:@escaping(UIBackgroundFetchResult)->无效)从未被静默推送通知调用./p>

我真的在这里错过了iOS 11的东西吗?


  @UIApplicationMain类AppDelegate:UIResponder,UIApplicationDelegate {//..这里的一些变量...func application(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?)->布尔{//在后端注册静默推送通知application.registerForRemoteNotifications()//...这里的一些代码...//为后台服务/终止的应用设置后台获取间隔UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)//...这里的一些代码...返回真}func应用程序(_应用程序:_UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:数据){让tokenParts = deviceToken.map {data->串入返回字符串(格式:%02.2hhx",数据)}让令牌= tokenParts.joined()logger.log.debug(设备令牌:\(令牌)")让realm = RealmController()让用户= realm.getLoggedInUserObject()//将推送令牌发送到服务器如果让用户=用户{让电子邮件= user.email!让serverController = ServerController.serverControllerserverController.sendPushToken(令牌:令牌,电子邮件:电子邮件){状态如果状态== 201 {//...这里的一些代码...} 别的 {//...这里的一些代码...}}}}func应用程序(_应用程序:_UIApplication,didFailToRegisterForRemoteNotificationsWithError错误:错误){logger.log.debug(错误)}func application(_ application:UIApplication,didReceiveRemoteNotification userInfo:[AnyHashable:Any],fetchCompletionHandler completeHandler:@escaping(UIBackgroundFetchResult)-> Void){logger.log.debug(userInfo)让aps = userInfo ["aps"]为![String:AnyObject]如果aps ["content-available"] as?整数== 1 {//....一些静默的推送任务在这里....}}} 

解决方案

最终更新2017-10-31

Apple刚刚发布了iOS 11.1的正式(万圣节)版本

更新2017-10-09

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

已解决的通知

静默推送通知的处理频率更高.(33278611)

我将再次测试此Beta 2版本,并更新此答案供您参考.

更新-测试->在不同情况下进行了一些测试之后,此错误似乎已在最新的iOS11.1 beta 2版本中修复.现在我们只能等待正式发布.在某些论坛中,他们认为Apple将于10月下旬发布iOS11.1.


旧帖子

上个星期我调查了很多时间,以寻找有关此问题的答案.阅读苹果发行说明(包括不推荐使用,更改和新功能)后,我测试了以下情况:

我在我的 AppDelegate 中添加了空函数,现在无声推送在前台和后台再次起作用:

  func application(_ application:UIApplication,performFetchWithCompletionHandlercompleteHandler:@转义(UIBackgroundFetchResult)->无效){logger.log.debug(使用完成处理程序TEST执行抓取")} 

我不确定此解决方法"是否与问题相关,以下功能应用程序(_应用程序:UIApplication,didReceiveRemoteNotification userInfo:[AnyHashable:Any],fetchCompletionHandlercompleteHandler:@escaping(UIBackgroundFetchResult)->无效)在iOS11中未调用.

尽管如此,如果您发现相同的行为,也可以尝试一下并给我反馈.

更新2017-09-25

在我的情况下,无声推送"现在可以在前台和后台模式下使用,但是如果该应用程序已从用户或操作系统挂起,则不会起作用.因此,此问题仍然悬而未决-感谢您提供任何帮助!有关更多信息,请参见以下线程:没有推送到iOS 11上的应用的静默推送


更新2017-10-05

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

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

一些开发人员说此Beta版本已解决该问题,其他开发人员则说在某些情况下该问题仍然存在.现在,当苹果公司为客户推出iOS11.1时会很有趣.

I was hoping that the iOS11 release will fix the silent push issue, which was in the latest betas and GM version of iOS.

Currently I'm struggling to understand, why I don't receive any silent push messages, which should actually wake up my app to perform some needed tasks in the background.

In iOS 10 I just use the background fetch capability and implemented the 'wake-up-code' in my AppDelegate like the code below.

In iOS 11 the registering code is still working fine and my backend is also delivering the push notification to Apples DEV servers (sandbox) and also to the PROD servers (production release). Unfortunately the function func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) is never called by the silent push notifications.

Did I actually miss something here for 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 ....
       }
   }
}

解决方案

Final UPDATE 2017-10-31

Apple just had its official (Halloween) release of iOS 11.1

UPDATE 2017-10-09

Apple released iOS11.1 beta 2 today. Again they mentioned in their Release Notes the following note:

Notifications Resolved Issues

Silent push notifications are processed more frequently. (33278611)

I'm going to test again this beta 2 version and update this answer for your information.

UPDATE - Testing -> After some testing with different scenarios this bug seems to be fixed within the latest iOS11.1 beta 2 version. Now we can only wait for the official release. In some forums they assume that Apple will release iOS11.1 in late October.


Older post

I investigated a lot of time the last week to look for an answer regarding this issue. After reading the apple release notes (incl. the deprecated, changed and new functions) I tested the following situation:

I added the empty function to my AppDelegate and now the silent push works again both in foreground and also in background:

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

I'm not sure if this 'workaround' is related to the issue, that the following function application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) is not called in iOS11.

Nevertheless you can give it a try and give me feedback, if you identify the same behavior.

UPDATE 2017-09-25

The 'silent push' works now in my case in foreground and background mode, but not, if the app is suspended either from the user or from the OS. So this issue is still open and not fixed - any help appreciated! For more information see also this thread: Silent pushes not delivered to the app on iOS 11


UPDATE 2017-10-05

Apple released iOS11.1 beta some days ago. They mentioned in their Release Notes the following:

Notifications Resolved Issues
Silent push notifications are processed more frequently. (33278611)

Some developers are saying that the issue is fixed with this beta, other developers say that the issue is still present within some circumstances. Now it would be interesting when Apple is launching iOS11.1 for the customers.

这篇关于iOS11快速无声推送(后台获取,didReceiveRemoteNotification)不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆