Sub topic 11
Displaying Foreground Notifications with a Local Notifications Plugin
This chapter assumes you know basic Flutter widgets, async/await, streams, and Firebase Cloud Messaging (FCM). It also assumes that firebase_messaging is already connected to your Firebase project and that the app can receive messages.
Why foreground notifications need extra work
When an FCM notification arrives while your app is in the background, Android or iOS usually displays it in the system notification tray. When the app is visible, the operating system generally gives the message to your Dart code without displaying a notification.
This behavior prevents duplicate alerts while a user is actively using the app. If your product should still alert the user, display a local notification from the FirebaseMessaging.onMessage stream.
A local notification is created directly by the app on the device. The flutter_local_notifications package provides the Dart interface for creating one.
Add and configure the plugin
Add flutter_local_notifications to pubspec.yaml, then run flutter pub get. Create an Android notification channel before showing a notification. A channel groups notifications with the same behavior, such as sound and importance.
The following example initializes the plugin, creates an Android channel, and displays a local notification whenever an FCM message arrives in the foreground.
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
final notifications = FlutterLocalNotificationsPlugin();
const channel = AndroidNotificationChannel(
'high_importance',
'Important notifications',
description: 'Notifications that need immediate attention.',
importance: Importance.high,
);
Future<void> setUpForegroundNotifications() async {
const android = AndroidInitializationSettings('@mipmap/ic_launcher');
const ios = DarwinInitializationSettings();
await notifications.initialize(
const InitializationSettings(android: android, iOS: ios),
);
await notifications
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
FirebaseMessaging.onMessage.listen((message) async {
final notification = message.notification;
if (notification == null) return;
await notifications.show(
message.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description,
importance: Importance.high,
priority: Priority.high,
),
iOS: const DarwinNotificationDetails(),
),
payload: message.data['route'],
);
});
}Call setUpForegroundNotifications() after Firebase.initializeApp() and before the screen that needs to receive messages is displayed. In a real app, call it once, such as during application startup. Registering the listener repeatedly can cause one incoming message to produce several notifications.
Platform permission and behavior
On iOS, request notification permission with FirebaseMessaging.requestPermission(). The DarwinInitializationSettings value configures the local notification plugin, but it does not replace the user's permission decision.
On Android 13 and newer, request the POST_NOTIFICATIONS runtime permission. The channel's importance also affects whether Android makes sound, vibrates, or shows the notification prominently.
Use the message's data map for a route or identifier. Use notification.title and notification.body for display text. Check for missing values because data-only messages may not contain a notification object.
Exercise
Why might an FCM message appear in the background but not while the Flutter app is open?
Show answer
The operating system often displays notification messages automatically in the background, but foreground messages are delivered to FirebaseMessaging.onMessage for the app to handle. The app must create a local notification if it wants a visible alert.