20

I need to show firebase notifications when the app is on foreground by using local notification but it is not working.

  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin=new FlutterLocalNotificationsPlugin();

  static  FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  static StreamController<Map<String, dynamic>> _onMessageStreamController =
  StreamController.broadcast();
  static StreamController<Map<String, dynamic>> _streamController =
  StreamController.broadcast();
  static final Stream<Map<String, dynamic>> onFcmMessage =
      _streamController.stream;

  @override
  void initState() {
    super.initState();

    var android=AndroidInitializationSettings('mipmap/ic_launcher.png');
    var ios=IOSInitializationSettings();
    var platform=new  InitializationSettings(android,ios);
    flutterLocalNotificationsPlugin.initialize(platform);

    firebaseCloudMessaging_Listeners();
  }

Here is the Firebase Code

 void firebaseCloudMessaging_Listeners() {

if (Platform.isIOS) iOS_Permission();

_firebaseMessaging.getToken().then((token) {
  print("FCM TOKEN--" + token);
});
_firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async {
    print('on message $message');
    showNotification(message);
  },
  onResume: (Map<String, dynamic> message) async {
    print('on resume $message');
  },
  onLaunch: (Map<String, dynamic> message) async {
    print('on launch $message');
  },
);


}

This is showNotification method

 void showNotification(Map<String, dynamic> msg) async{
    print(msg);
    var android = new AndroidNotificationDetails(
        'my_package', 'my_organization', 'notification_channel', importance: Importance.Max, priority: Priority.High);
    var iOS = new IOSNotificationDetails();
    var platform=new NotificationDetails(android, iOS);
    await flutterLocalNotificationsPlugin.show(
      0,'My title', 'This is my custom Notification', platform,);
  }

and Firebase Response

{notification: {title: Test Title, body: Test Notification Text}, data: {orderid: 2, click_action: FLUTTER_NOTIFICATION_CLICK, order name: farhana}}

4
  • Have you solved this? Commented Feb 25, 2020 at 5:47
  • @Farhana Are you able to fix this? i am also struggling with the same. Commented Dec 9, 2020 at 11:22
  • @Rocx yes, I resolved this issue Commented Dec 9, 2020 at 11:25
  • can you share some of the working code. Also wants to understand if i am on different page then do i get the onmessage notification? Or need to define it everywhere. stackoverflow.com/questions/65166526/… Commented Dec 9, 2020 at 11:27

5 Answers 5

8

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

You just add to your code the following line

    FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(alert: true, badge: true, sound: true);
Sign up to request clarification or add additional context in comments.

2 Comments

Wow! THIS is the correct answer. THANK YOU. Note: Be sure to add this after you app is initialized, ie after await Firebase.initializeApp();
Anytime bro @lenz
4

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

8 Comments

I'm working for Android, later on, I will work for ios and thanks for the info
Are you getting any kind of error or exception on Android? Or the notification won't show up?
i check on debug mode but no exception i was getting
I just tested the above code on Android and it is working for me. Can you check where exactly your code is failing? Is the print(msg) statement inside showNotification() getting executed?
actually, I was testing in oreo devic;
|
4

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 UIKit
import Flutter
import 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.

2 Comments

i can see the notification in foreground but my onMessage is not getting called. Any solution for this?
If you set the delegate in didFinishLaunchingWithOptions then you will not be able to show a notification if you send notification object in the FCM payload and if you remove the user notification delegate method then show notification will not work
1

For not receiving the notification in foreground make sure the android drawable file contains the launcher_icon or the icon which you have set in shownotification function.

Comments

0

I also couldn't get firebase notifications with flutter_local_notifications working on iOS in foreground. Background notifications worked fine. Problem was that firebase notification data is different on iOS. Notification data is not ["notification"]["title"] as in android but ["aps"]["alert"]["title"].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.