Sub topic 6
Notification Messages vs Data Messages
This chapter assumes you know basic Dart, async/await, streams, and Flutter app states. You should also have a Flutter app connected to Firebase Cloud Messaging (FCM).
FCM supports two main message payloads:
- A notification message contains display fields such as
titleandbody. - A data message contains key-value information for your application.
- A combined message contains both notification and data fields.
The difference matters because Android and iOS may handle these messages for you when your app is not visible.
How message types behave
A notification message is designed for user-visible alerts. When the app is in the background, the operating system usually displays the notification automatically. Your Dart code runs when the user taps it. When the app is in the foreground, FirebaseMessaging.onMessage receives the message, but FCM does not automatically show a system notification.
A data message is designed for application logic. Its fields are delivered to your code when the platform allows background execution. You decide what the data means and whether to show a notification. Background delivery is subject to platform restrictions, battery settings, message priority, and the user’s permissions.
A combined message gives you both behaviors. The operating system can display the notification in the background, while your app can read the data after the user taps the notification. In the foreground, your onMessage listener receives both parts.
| Payload | Background behavior | Foreground behavior | Common use |
|---|---|---|---|
| Notification | System displays it | `onMessage` receives it | Chat or news alert |
| Data | App may process it, subject to restrictions | `onMessage` receives it | Sync or custom routing |
| Notification and data | System displays it; data is available after tap | `onMessage` receives both | Alert that opens a detail page |
Inspecting both parts
The following listener prints the notification and data portions of a message:
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
void listenForMessages() {
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
final notification = message.notification;
debugPrint('Title: ${notification?.title}');
debugPrint('Body: ${notification?.body}');
debugPrint('Data: ${message.data}');
final route = message.data['route'];
if (route is String && route.isNotEmpty) {
debugPrint('The message targets route: $route');
}
});
}Data values should be strings. For example, send "orderId": "42" rather than a numeric value. Convert the string in Dart when necessary.
{
"message": {
"token": "DEVICE_REGISTRATION_TOKEN",
"notification": {
"title": "Order shipped",
"body": "Your order #42 is on its way."
},
"data": {
"route": "/orders/42",
"orderId": "42"
}
}
}Choosing a payload
Use a notification message when the main goal is a visible alert and the default platform presentation is acceptable. Use a data message when your application must control processing, navigation, or notification appearance.
Do not assume that a data-only message will always run immediately in the background. Android may delay low-priority work, and iOS may limit background execution. A force-quit application can have additional restrictions. A notification message is usually more reliable for a user-visible alert, but it gives your app less control before the user taps.
Message flow by app state
Exercise
You need an alert that opens /orders/42 when tapped, and you want the operating system to display it while the app is in the background. Which payload should you send, and where should the route be stored?
Show answer
Send a notification-and-data payload. Put /orders/42 in the data field, such as "route": "/orders/42", then handle the tap with onMessageOpenedApp and getInitialMessage.