FCM data message not coming when app is closed in ios

Viewed 744

I have integrated FCM in my flutter project with flutter_local_notification. Everything works as expected in Android. However, ios also works almost except when app is closed from history. When app is cleared from history messages are not triggering. I am using only data message in push notification. Please have a look below:

{
   "to": "/topics/global",
   "priority": "high",
   "content_available": true,
   "data": {
      "title": "Demo Title",
      "message": "Demo Message",
      "payload": "basic"
   }
}

As you can see there is no notification object in the json. This configuration is working fine on Android (foreground & background) and for ios only foreground. However, if i add the notification object in this json surprisingly it works even the app is closed.

But as i need to handle the notification manually that option doesn't fit for my use case. So i researched further and disabled Method Swizzling

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

But that didn't help either. Now i'm kinda lost and can't find a way to achieve expected result. My AppDelegate.swift is given below

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
   override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    if #available(iOS 10.0, *) {
      UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
    }
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

NOTE: The ios app is published in TestFlight and testing on a real device

2 Answers

After a long research i couldn't find an absolute fix for this issue. However, i have implemented a workaround for the issue.

I have 3 scenario of sending notification which are

1. Sending notification to specific user
2. Sending notification to all user
3. Sending notification to a topic

When user opens the app i check which platform the user is using. If it's Android then i store the device token as android otherwise ios. At the same time i also register the user to a topic global_android if platform is Android and global_ios if platform is ios.

Now when i send notification from admin panel i match the condition with above 3 scenario. For android i use following json configuration to send notification

{
  "to": "topic || token",
  "priority": "high",
  "content_available": true,
  "data": {
     "title": "Demo Title",
     "message": "Demo Message",
     "payload": "basic"
  }
}

And for ios i use following configuration

{
  "to": "/topics/global",
  "priority": "high",
  "content_available": true,
  "notification": {
     "title": "Demo Title",
     "body": "Demo Message"
  },
  "data":{
      "key1":"value",
      "key2":"value"
  }
}

And now when i receive the notification i check the platform again. If it's Android then i show a custom notification using 'flutter_local_notification' plugin and if it's ios then i do nothing because the notification is shown already by firebase.

This is not an absolute solve but guess what, it's working for me.

Same problem with me. It is error from firebase_messaging library. This issue was discussed more than 1 year ago but still no solution. Details see here FCM Notification IOS.

Related