Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter FCM push notification for iOS when app is in foreground

I'm using fcm in my flutter project. I want to show an alertdialog when a notification is received from fcm, when the app is in foreground. It's working for android but not on iOS. Can anyone help me with that please?

here's my code snippet:

FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("FCM: onMessage: " + message['notification']['body']);
        //showNotification(message['notification']['title'], message['notification']['body']);
        print("FCM: onMessage: $message");
        setState(() {
          return notification = true;
        });

        AwesomeDialog(
          context: scaffoldKey.currentContext,
          title: message['notification']['title'],
          desc: message['notification']['body'],
          btnOkOnPress: () {
            Navigator.pop(scaffoldKey.currentState.context);
          },
        ).show();

        //notification sound
        playSound();
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("FCM: onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("FCM: onResume: $message");
      },
    );

    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, badge: true, alert: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });
like image 926
Inan Mahmud Avatar asked Dec 22 '25 18:12

Inan Mahmud


1 Answers

FCM will not work well in foreground, you need to use others like, flutter_local_notifications

like image 109
Jim Avatar answered Dec 24 '25 08:12

Jim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!