Handle background notification in terminated status app

i have an issue when handling silent push in my app when the user close the app, here is my native code which works like charm when the app in the foreground, background or terminated status from a short time from closing the app, and after long time from closing it (+30min) it will not work anymore, it make me confuse why it does not work and other messaging app like viber, whatsapp and messanger you can still receive messages and calls even you swipe the app and close it !! is there any thing must i add !!

override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
            Messaging.messaging().appDidReceiveMessage(userInfo)
            if Auth.auth().canHandleNotification(userInfo) {
                completionHandler(.noData)
                return
            }
        
        // sample notification for testing
            let content = UNMutableNotificationContent()
            content.title = "Hi there"
            content.body = "Just test"
            content.sound = UNNotificationSound.default
            
            let request = UNNotificationRequest(identifier: "helloNotification", content: content, trigger: nil)
            
            UNUserNotificationCenter.current().add(request) { (error) in
                if let error = error {
                    print("Error adding notification request: \(error.localizedDescription)")
                }
            }
        // resent to dart side
        let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
        let notificationChannel = FlutterMethodChannel(name: "notificationHandler", binaryMessenger: controller.binaryMessenger)
        var dataToSend: [String: Any] = [:]

        if let text = userInfo["text"] as? String {
            dataToSend["text"] = text
        }
        
        // Convert the dictionary to NSDictionary
        let nsDataToSend = NSDictionary(dictionary: dataToSend)


        // Pass the NSDictionary to Dart
        notificationChannel.invokeMethod("handleRemoteMessage", arguments: nsDataToSend)
    }

i checked :

  • background capabilities : remote notifs, background fetching, voip, background processing

An app will not be woken in the background by a silent (aka content-available) push notification if the app had previously been force-quit. Force quit is considered a drastic choice by the user to say that they do not want the app to run, often because it misbehaved in some unrecoverable manner.

The WWDC 2020 video "Background execution demystified" (https://developer.apple.com/videos/play/wwdc2020/10063/) explains the factors that effect background runtime.

If the purpose of using silent notifications is to execute some code that is triggered remotely, you would want to look into using a Notification Service Extension as discussed at https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension. The Notification Service Extension will be executed for every visible push notification. So, it could serve your needs, as long as the user has not disabled the visibility of your notifications through various settings. The service extension will not be executed for push notifications that will not be presented visually.

Although using totally silent pushes will not be a feasible solution for most apps, you may also want to look at using the Interruption Levels for notifications and setting your notifications to "passive". This setting will allow you to send visible pushes, and execute the Notification Service Extension when the notification arrives, but will be less likely to interrupt the user, although these are not completely invisible like silent pushes. WWDC 2021 video "Send communication and Time Sensitive notifications" (https://developer.apple.com/videos/play/wwdc2021/10091/) explains the Interruption Levels introduced in iOS 15.

If you believe you may be eligible for a special entitlement that will allow you to silence the visible pushes by using a Notification Service Extension. You can read more about this here: https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_usernotifications_filtering

On this page you will also find a link to apply for the entitlement. You may want to double check that your use case will be eligible before applying and having to waste time waiting for an answer.

Handle background notification in terminated status app
 
 
Q