Sub topic 6
Notification messages vs data messages
This chapter covers the two payload shapes FCM accepts, how each behaves in the foreground, background and terminated states, and how to choose between them. It assumes you have added firebase_core and firebase_messaging to a Flutter project, can read an FCM registration token, and understand Dart streams and async/await.
Why the message type matters
Every FCM message you send has a payload. The payload decides who displays the message: the operating system or your Dart code. That decision controls whether the user sees something when your app is closed, and whether your code runs at all.
FCM has two payload fields that matter here:
notificationis a predefined set of keys, mainlytitleandbody. FCM and the OS understand these keys and can show a notification for you.datais a map of your own keys and values. FCM delivers it to your app and does nothing else with it.
A message can contain either field or both. The combination gives three practical shapes.
| Shape | Contains | Common name |
|---|---|---|
| Display only | `notification` | Notification message |
| Custom only | `data` | Data message |
| Both | `notification` and `data` | Notification message with data payload |
Notification messages
A notification message carries a notification block. When the app is in the background or terminated, the OS (via the Android system tray or APNs on iOS) shows it automatically. Your Dart code does not need to run for the user to see it.
{
"message": {
"token": "DEVICE_REGISTRATION_TOKEN",
"notification": {
"title": "Order shipped",
"body": "Your order #42 is on its way."
}
}
}Use this shape when you only need the user to see text. It is the most reliable way to get something on screen, because it does not depend on your app being allowed to start in the background.
The trade-off is control. You cannot run logic before the notification is displayed, such as hiding it if the user is already viewing that conversation.
Data messages
A data message has only a data block. FCM never displays it. It hands the key-value pairs to your app, and your app decides what to do.
{
"message": {
"token": "DEVICE_REGISTRATION_TOKEN",
"data": {
"type": "chat",
"chatId": "17",
"route": "/chats/17"
}
}
}Two rules apply to the data map in the HTTP v1 API:
- Every value must be a string. Send
"count": "3", not"count": 3. Nested objects are rejected, so serialize them to a JSON string if you need them. - Some key names are reserved. Avoid keys starting with
googleorgcm, and avoid names such asfrom,message_typeandnotification.
Use data messages for silent updates, such as syncing content, or when your app builds its own notification, for example with a local notifications plugin.
Notification messages with a data payload
You can combine both blocks. The notification part lets the OS display the message, and the data part travels with it so your app can read it when the user taps.
{
"message": {
"token": "DEVICE_REGISTRATION_TOKEN",
"notification": {
"title": "Order shipped",
"body": "Your order #42 is on its way."
},
"data": {
"route": "/orders/42"
}
}
}This is the most common production shape. The OS handles display, and the data map tells your app where to navigate after a tap. Later chapters cover the tap and navigation handling.
What the app receives
In Dart, every incoming message arrives as a RemoteMessage. The two payload types appear in different properties.
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
void describe(RemoteMessage message) {
final notification = message.notification;
if (notification != null) {
debugPrint('Title: ${notification.title}');
debugPrint('Body: ${notification.body}');
}
debugPrint('Data: ${message.data}');
}message.notification is null for a pure data message. message.data is an empty map when no data was sent. Checking notification for null is the simplest way to tell the shapes apart in code.
Behavior by app state
The same payload behaves differently depending on the app's state. This is the part that most often surprises developers.
| App state | Notification message | Data message | Both |
|---|---|---|---|
| Foreground | Delivered to `onMessage`. No system notification is shown by default. | Delivered to `onMessage`. | Delivered to `onMessage`. No system notification is shown by default. |
| Background | OS shows the notification. Tapping it triggers `onMessageOpenedApp`. | Delivered to the background handler. Nothing is shown. | OS shows the notification. The data is available on tap. |
| Terminated | OS shows the notification. Tapping it makes `getInitialMessage` return the message. | Delivery depends on platform and device (see below). | OS shows the notification. The data is available through `getInitialMessage`. |
The foreground row deserves attention. When the app is open and visible, FCM does not draw a banner for you on Android. Your onMessage listener fires, and you must show something yourself if you want the user to see it. iOS can show foreground banners, but only after you opt in with setForegroundNotificationPresentationOptions. Both are covered in the foreground chapter.
Data messages when the app is not running
Data messages are less dependable than notification messages when the app is not in the foreground.
On Android, a data message can wake the app and start your background handler. Battery optimization, "background restrictions" set by the user, and manufacturer-specific task killers can block this. If the user force-stops the app from system settings, Android will not deliver messages until the user opens the app again.
On iOS, a data-only message is delivered as a silent notification. To receive it, you need the Background Modes capability with Remote notifications enabled. The message should also set content-available in the APNs payload. iOS treats silent notifications as low priority. It may throttle or drop them, and it does not deliver them to an app the user force-quit from the app switcher.
Do not rely on data messages for anything the user must see. Use them for work that can be skipped safely, such as a cache refresh.
Setting `content-available` for iOS silent messages
If you send a data message to an iOS device, add APNs configuration to the request. The HTTP v1 API lets you set platform-specific options with apns.
{
"message": {
"token": "DEVICE_REGISTRATION_TOKEN",
"data": {
"type": "sync"
},
"apns": {
"headers": {
"apns-priority": "5",
"apns-push-type": "background"
},
"payload": {
"aps": {
"content-available": 1
}
}
}
}
}Priority 5 is required for background pushes on iOS. A higher priority with content-available and no visible alert can be rejected by APNs.
Choosing a type
Use these criteria to pick a shape:
- The user must see it. Send a notification message with an optional data payload.
- The user should see it, and your code decides the content or whether to show it. Send a data message and build the notification yourself. Accept that delivery in the background is best effort.
- Nothing should be shown. Send a data message and treat it as a hint to sync, not a guarantee.
- You need a route or ID on tap. Send both blocks and keep the data values as strings.
Common mistakes
- Sending numbers or booleans in
data. The request fails validation. Convert to strings. - Expecting the Dart background handler to run for notification messages. When the OS displays the notification, your handler is not the display path. Do not put required work there.
- Testing data messages on an iOS simulator. Use a physical device for reliable results with APNs.
- Forgetting that the console always sends notification messages. The Firebase console composer produces a
notificationblock, so it cannot test a pure data message. Use the HTTP v1 API for that.
Exercise
Your app sends {"message": {"token": "...", "data": {"title": "Sale", "body": "50% off"}}}. A tester reports that nothing appears when the app is in the background. Why, and what is the smallest change that makes the OS show it?
Show answer
The message has only a data block, so FCM does not display it, and the title and body keys inside data mean nothing to the OS. Either move them into a notification block ("notification": {"title": "Sale", "body": "50% off"}), or keep the data message and show a notification yourself from the background handler with a local notifications plugin. The first is the smallest change.
Exercise
A request contains "data": {"count": 3}. What happens, and how do you fix it?
Show answer
The HTTP v1 API rejects it because data values must be strings. Change it to "count": "3", and parse it with int.parse in Dart.