FCM Data Message vs Notification: Key Differences and Usage
notification message is handled by the system to display alerts automatically, while a data message delivers custom key-value pairs directly to the app for manual processing. Notification messages show UI by default, but data messages require app code to handle and display content.Quick Comparison
This table summarizes the main differences between FCM data messages and notification messages.
| Aspect | Notification Message | Data Message |
|---|---|---|
| Purpose | Display alerts automatically | Send custom data for app processing |
| Handled by | System UI (OS) | App code (developer) |
| Payload | Contains notification key | Contains only data key |
| App state behavior | Shown when app is background or foreground | Delivered only if app is in foreground or handled explicitly |
| Customization | Limited to predefined fields | Fully customizable key-value pairs |
| Use case | Simple alerts and messages | Complex logic or silent updates |
Key Differences
Notification messages are designed to show alerts automatically on the device. They include a notification key in the payload, which the operating system uses to display a message in the notification tray without needing app code. This makes them easy to use for simple notifications like reminders or promotions.
Data messages only include a data key with custom key-value pairs. They do not trigger any UI by themselves. Instead, the app receives the message and decides what to do, such as updating the UI, syncing data, or showing a custom notification. This requires the app to be running or handle background processing explicitly.
Another important difference is how messages behave depending on app state. Notification messages appear even if the app is closed or in the background, while data messages require the app to be active or have special handling to process them in the background. This makes data messages more flexible but also more complex to implement.
Code Comparison
Here is an example of sending a notification message using Firebase Cloud Messaging HTTP v1 API.
{
"message": {
"token": "device_token_here",
"notification": {
"title": "Hello!",
"body": "This is a notification message."
}
}
}Data Message Equivalent
Here is the equivalent data message example sending custom key-value pairs.
{
"message": {
"token": "device_token_here",
"data": {
"type": "chat",
"message": "You have a new message!"
}
}
}When to Use Which
Choose notification messages when you want simple alerts that appear automatically without extra app code, such as marketing messages or reminders. They are easy and reliable for showing notifications when the app is in the background or closed.
Choose data messages when you need full control over the message content and behavior, like syncing data silently, triggering custom UI updates, or handling complex logic. Data messages require your app to process them, so they are best for interactive or background tasks.