Search code examples
flutterfirebase-cloud-messaging

How do I show foreground notifications?


I am sending notifications from Firebase cloud messaging. The background notification works fine, but I can't find something that worked in the foreground for me.

I want to display the notification when the app is open same as when the app is in background: with the system notification sound and the same banner display at the top of screen.


Solution

  • You can control this on iOS

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

    package:firebase_messaging/firebase_messaging.dart

    Sets the presentation options for Apple notifications when received in the foreground.

    By default, on Apple devices notification messages are only shown when the application is in the background or terminated. Calling this method updates these options to allow customizing notification presentation behaviour whilst the application is in the foreground.

    Important: The requested permissions and those set by the user take priority over these settings.

    [alert] Causes a notification message to display in the foreground, overlaying the current application (heads up mode). [badge] The application badge count will be updated if the application is in the foreground. [sound] The device will trigger a sound if the application is in the foreground. If all arguments are false or are omitted, a notification will not be displayed in the foreground, however you will still receive events relating to the notification.