iOS11 swift silent push (background fetch, didReceiveRemoteNotification) is not working anymore
Asked Answered
V

2

10

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 ....
       }
   }
}
Volva answered 20/9, 2017 at 19:12 Comment(0)
V
5

Final UPDATE 2017-10-31

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

Apple Release Software Update 31st of October

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.

Volva answered 24/9, 2017 at 16:35 Comment(10)
"After reading the apple release notes" where did Apple mentions something about this?Neall
They didn't mention anything about, but I tried a lot and figured out, that this solution works.Volva
ok. Does that work for you when you launch the app in suspended mode? (Scheme -> check "Wait for executable to be launched" then send the push. Im my case, I'm still losing the silent pushes when the app is in this state. See my "UPDATE 13.09 - iOS 11 GM" here #44797113Neall
I just tried it -> if the checkbox "Wait for executable to be launched" is checked the app won't wake up, also if I combine the option with check the box "Launch due to background fetch event". In my case it only works if you check "Launch due to background fetch event" and Launch "Automatically".Volva
yes that's the same behaviour I have in my SO question and this is what differs to iOS 10 where the app was brought back from suspended to background mode and the delegate method called.Neall
Understood - in my case it was never called in foreground as well as in background. hope we get an answer soon on this suspended mode issueVolva
Let us continue this discussion in chat.Neall
If I were, I would put the latest update at the top of the answerDisputable
So if I have a terminated app (killed manually) and I received a Silent Push notification, will the delegate method: - application: didReceiveRemoteNotification:fetchCompletionHandler: be called? I tried this with iOS11.1 and appears not to be working. (nor with iOS10.3)Subliminal
@PauSenabre it works with iOS10xx and iOS11.1, only iOS11.0 is not working.Volva
N
0

What you experience is actually a well-documented behaviour rather than a bug.

Apple's documentation says: "The system treats silent notifications as low-priority. You can use them to refresh your app’s content, but the system doesn't guarantee their delivery. In addition, the delivery of silent notifications may be throttled if the total number becomes excessive. The actual number of silent notifications allowed by the system depends on current conditions, but don't try to send more than two or three silent notifications per hour."

In summary, silent notification is not guaranteed to wake up your app. Why? The more often the app in the background on the user's device is woken up, the higher the battery consumption is.

According to IBM's statement, it says "With the advent of iOS 11, Apple has introduced changes in the way they handle certain kinds of push messages. This was done to improve battery life, but comes at a cost of user experience that is likely to affect your app." You can find more details here.

Nalda answered 19/7, 2018 at 6:54 Comment(1)
It was definetily a bug on Apples side. They broke something within the ios11 operating system. there were several discussions last year about this time and apps didn‘t receive push notifications for almost 1 month. btw this question was answered a year ago you cannot reference to the current documentationVolva

© 2022 - 2024 — McMap. All rights reserved.