0
0
FirebaseComparisonBeginner · 4 min read

FCM Data Message vs Notification: Key Differences and Usage

In Firebase Cloud Messaging, a 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.

AspectNotification MessageData Message
PurposeDisplay alerts automaticallySend custom data for app processing
Handled bySystem UI (OS)App code (developer)
PayloadContains notification keyContains only data key
App state behaviorShown when app is background or foregroundDelivered only if app is in foreground or handled explicitly
CustomizationLimited to predefined fieldsFully customizable key-value pairs
Use caseSimple alerts and messagesComplex 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.

json
{
  "message": {
    "token": "device_token_here",
    "notification": {
      "title": "Hello!",
      "body": "This is a notification message."
    }
  }
}
Output
A notification appears on the device with title 'Hello!' and body 'This is a notification message.'
↔️

Data Message Equivalent

Here is the equivalent data message example sending custom key-value pairs.

json
{
  "message": {
    "token": "device_token_here",
    "data": {
      "type": "chat",
      "message": "You have a new message!"
    }
  }
}
Output
The app receives the data payload {"type":"chat","message":"You have a new message!"} and must handle displaying or processing it.
🎯

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.

Key Takeaways

Notification messages show alerts automatically using system UI with minimal app code.
Data messages deliver custom data to the app for manual handling and flexible behavior.
Notification messages work well for simple alerts visible anytime, even if the app is closed.
Data messages require the app to be active or handle background processing to receive them.
Use notification messages for easy alerts and data messages for custom app logic.