Silent notification is not working when app in background in iOS 11.4.1

2.2k Views Asked by At

I'm trying to develop a messenger app, client needs to send only silent notification when app is in foreground or in background.

It works perfectly in IOS 10.2.1 Verizon iPhone.

After update to 11.4.1 notification works only in foreground but not in background.

Payload:

 {
     "to" : "/topics/channel_18",
     "data" : {
      "action" : "NOTIFY",
      "message" : "{"text":"test" }"
     },
     "content_available" : true
    }

code:

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

         Messaging.messaging().appDidReceiveMessage(userInfo)
        let action = userInfo["action"] as! String        

        // Configure the notification's payload.
        let content = UNMutableNotificationContent()
        content.title = NSString.localizedUserNotificationString(forKey: "Hello!", arguments: nil)
        content.body = NSString.localizedUserNotificationString(forKey: "testing", arguments: nil)
        content.sound = .none

        // Deliver the notification in five seconds.
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
        let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger) // Schedule the notification.
        let center = UNUserNotificationCenter.current()
        center.add(request) { (error : Error?) in
            if let theError = error {

            }
        }


        completionHandler(UIBackgroundFetchResult.newData)
    }

I have gone through the link Silent pushes not delivered to the app on iOS 11 but I could not find the solution.

1

There are 1 best solutions below

0
On

Silent pushes have two things working against them: opportunistic delivery and the background wakeup limiter. These two things conspire to make it look like silent push may not be working when indeed it is. iOS 11-11.2.x had issues where silent push delivery was delayed almost "forever" but those have been addressed.

Opportunistic Delivery

Opportunistic delivery tries to wait for the "best" time to wake up an app for background work. For example, it is very likely that background work will execute when the device is plugged into power and connected to WiFi - ideal conditions. In less than ideal conditions iOS will make a guess as to when it would be a good time to wake up an app.

Apple explained this well in the WWDC 2013 session What's New In Multitasking

Rate Limiting Background Execution

The background wakeup limiter attempts to prevent apps from abusing background execution. Many apps would like to do a lot of work in the background, or schedule background work so that the app is almost constantly running in the background. The rate limiter tries to prevent apps from abusing background execution. Apple doesn't document this behavior, but it (may) look something like this:

  • The rate limiter starts with a delay value, let's say 10 minutes. So 10 minutes after a silent push is received it is delivered to the device.

  • Each time your app is launched in the background to do work the rate limiter doubles the delay.

  • Each time your app takes more than 15 seconds to call the block passed to application(_:didReceiveRemoteNotification:fetchCompletionHandler:) double the delay again. More than 30 seconds, quadruple it.

  • Each time the user opens your app the delay is reset back to the original value.

Other factors such as memory and energy use probably come into play as well, but that should give you a good idea of what may be going on. Apple provides logging profiles that may allow you to see when the push is received, when it is delivered to your app, and when your app is actually launched to do background work. A jailbroken device may allow you to modify the delay values on the fly for troubleshooting.