Inan Mahmud
Inan Mahmud

Reputation: 244

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");
    });

Upvotes: 3

Views: 2715

Answers (1)

Jim
Jim

Reputation: 7601

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

Upvotes: 1

Related Questions