Notification background handler is not always triggered on iOS

Viewed 334

My background notification message handler works like a charm on Android. But sometimes it does not work on iOS without respecting any circumstances. APNS-tokens-certificates signing capabilities (push notif & background fetch etc.) all are set properly. But, background handler does not work often only on iOS and this issue persists so randomly. I have looked for any solution and also there is many topics related to this problem but many of them has no answers / not resolved. I thought maybe there is an up-to-date solution found by someone who suffer from same problem nowadays.

My background handler (used outside of classes) :

Future<void> backgroundHandler(RemoteMessage message) async {
  print("bg handler - bg handler");
  print("bg handler: " + message.data.toString()); 
  // await posting payload data to my db to get it back on lifecycle-foreground
}

This is notification payload from my server :

function CreateNotificationPayload(token,title,body,data){
    return {
        "data": data,
        "token": token,
        "notification": {
            "title": title,
            "body": body,
        },
        "android": {
            "priority" : 'high',
            "notification": {
                "body": body,
                "title": title,
                "sound": "default",
                "channelId" : "high_importance_channel"
            },
            "data" : {
            }
        },
        apns:{
            headers : {
                'apns-priority' : '10'
            },
            payload : {
                aps:{
                    sound : 'default'
                }
            }
        }
    };
}

In info.plist I tried different variations of FirebaseAppDelegateProxyEnabled :

<key>FirebaseAppDelegateProxyEnabled</key>
    <string>NO</string>

<key>FirebaseAppDelegateProxyEnabled</key>
    <false/>

<key>FirebaseAppDelegateProxyEnabled</key>
    <string>false</string>

In addition Appdelegate.swift :

import UIKit
import Flutter
import GoogleMaps
import Firebase

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, MessagingDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GMSServices.provideAPIKey("-----------HERE-is-My-google-APIkey------------")
    FirebaseApp.configure()
    Messaging.messaging().delegate = self
    GeneratedPluginRegistrant.register(with: self)

    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
    } else {
        let settings: UIUserNotificationSettings =
        UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }
    application.registerForRemoteNotifications()

    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

Note: Payload always reaches to my app. The issue is only about background handler. It works on both platforms (android & ios) but sometimes it stops working on iOS somehow.

I can not figure out why it stops working and then starts working again by doing many tests. Any suggestion?

0 Answers
Related