Flutter: Foreground notification is not working in firebase messaging Flutter: Foreground notification is not working in firebase messaging dart dart

Flutter: Foreground notification is not working in firebase messaging


There is an active issue logged on GitHub repository for the package regarding the same. Firebase messaging and local notifications won't work together on iOS since you can register only a single delegate for notifications.

Check out: https://github.com/MaikuB/flutter_local_notifications/issues/111

There's also an active flutter issue for the same:https://github.com/flutter/flutter/issues/22099


Problem: See the Push Notification while application is in foreground.

Solution:I was using firebase_message plugin and I was able to see the Push Notification while application is in foreground by making these few changes in my flutter project's iOS AppDelegate.swift file in flutter project.

import UIKitimport Flutterimport UserNotifications@UIApplicationMain@objc class AppDelegate: FlutterAppDelegate, UNUserNotificationCenterDelegate {  override func application(    _ application: UIApplication,    didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?  ) -> Bool {    GeneratedPluginRegistrant.register(with: self)    // set the delegate in didFinishLaunchingWithOptions    UNUserNotificationCenter.current().delegate = self    return super.application(application, didFinishLaunchingWithOptions: launchOptions)  }    // This method will be called when app received push notifications in foreground    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)    {        completionHandler([.alert, .badge, .sound])    }}

Note:This also works while using with flutter_local_notification plugin but with an issue that onSelectNotification is not working due to changes done above.


You can find the answer in FlutterFire documentationhttps://firebase.flutter.dev/docs/migration/#messaging

You just add to your code the following line

    FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(alert: true, badge: true, sound: true);