I have a flutter app that sends notifications to the users according to some actions. In the android part, all works fine, but in iOS part I only can receive messages if the app is opened. If I send a message with the app in background it did not receive, will only receive when I open the app again.
To do it I'm using FirebaseMessaging 7.0.3
Things I tried until now:
And in AppDelegate.swift, I already tried this two codes:
1 (from FirebaseMessaging documentation)
and 2
The second one arrives the messages with the app opened, with the first one it don't arrives at all.
I tried too create a new identifier from 0 in xcode and still i'm having the same result.
This is my dart sendNotification method:
What I'm missing here? How could I handle this messages in background? If you need more info, I Could add
To do it I'm using FirebaseMessaging 7.0.3
Things I tried until now:
Create a key in developers.apple and apply it within firebase
Feed my project with googleservices.plist
Turn on Push Notifications and Background Modes, and enable Background fetch and Remote notifications under Background Modes.
Added this code in my info.plist:
Code Block language <key>FirebaseScreenReportingEnabled</key> <true/> <key>FirebaseAppDelegateProxyEnabled</key> <false/>
And in AppDelegate.swift, I already tried this two codes:
1 (from FirebaseMessaging documentation)
Code Block swift import UIKit import Flutter @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { if #available(iOS 10.0, *) { UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate } GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } }
and 2
Code Block swift import UIKit import Flutter import Firebase @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { if #available(iOS 10.0, *) { 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() FirebaseApp.configure() GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } }
The second one arrives the messages with the app opened, with the first one it don't arrives at all.
I tried too create a new identifier from 0 in xcode and still i'm having the same result.
This is my dart sendNotification method:
Code Block dart Future sendNotification(String body, String title, bool isAdm) async { final String url = 'https://fcm.googleapis.com/fcm/send'; var notification; notification = '{"notification": {"body": "${body}", "title": "${title}", "content_available": "true", "click_action": "FLUTTER_NOTIFICATION_CLICK"}, "priority": "high", "to": "MYTOPIC"}'; final response = await http.post( url, headers: <String, String>{ "Content-Type": "application/json", "Keep-Alive": "timeout=5", "Authorization": "key=MYKEY" }, body: notification ); print(response.body); }
What I'm missing here? How could I handle this messages in background? If you need more info, I Could add
It looks like you are sending background (aka Silent) push notifications to your app and is expecting them to activate the app.
The issue you are seeing is expected, and it is due to content-available (aka "background" or "silent") push notifications being throttled when being delivered to apps that are in the background.
Background push notifications are never guaranteed to be delivered to the app every single time.
The WWDC 2020 video "Background execution demystified" (https://developer.apple.com/videos/play/wwdc2020/10063/) explains the factors that effect background runtime.
If you need your code executed for every send push notification, I suggest looking 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 longs 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.
The issue you are seeing is expected, and it is due to content-available (aka "background" or "silent") push notifications being throttled when being delivered to apps that are in the background.
Background push notifications are never guaranteed to be delivered to the app every single time.
The WWDC 2020 video "Background execution demystified" (https://developer.apple.com/videos/play/wwdc2020/10063/) explains the factors that effect background runtime.
If you need your code executed for every send push notification, I suggest looking 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 longs 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.