0
0
Firebasecloud~5 mins

Notification messages vs data messages in Firebase - CLI Comparison

Choose your learning style9 modes available
Introduction
When sending messages to mobile apps using Firebase Cloud Messaging, you can send two types: notification messages and data messages. Notification messages show alerts to users automatically, while data messages carry custom information for the app to handle silently.
When you want to show a simple alert or notification to the user without extra coding.
When you need to send hidden data to the app for processing without disturbing the user.
When you want to customize how the app reacts to a message by handling data yourself.
When you want to update app content silently in the background.
When you want to combine both alert and custom data in one message.
Commands
This command sends a notification message to all devices subscribed to the 'news' topic. The message will automatically show a notification with title and body.
Terminal
curl -X POST -H "Authorization: key=AAAA1234examplekey" -H "Content-Type: application/json" -d '{"to":"/topics/news","notification":{"title":"Hello","body":"World"}}' https://fcm.googleapis.com/fcm/send
Expected OutputExpected
{"message_id":1234567890123456}
-X POST - Send a POST request to the Firebase server
-H "Authorization: key=..." - Authenticate the request with your server key
This command sends a data message to the 'news' topic. The app receives the data silently and can decide how to use it without showing a notification automatically.
Terminal
curl -X POST -H "Authorization: key=AAAA1234examplekey" -H "Content-Type: application/json" -d '{"to":"/topics/news","data":{"score":"5x1","time":"15:10"}}' https://fcm.googleapis.com/fcm/send
Expected OutputExpected
{"message_id":1234567890123457}
-d - Send the JSON payload with data fields
This command sends a combined message with both notification and data. The user sees the notification, and the app also receives extra data to handle.
Terminal
curl -X POST -H "Authorization: key=AAAA1234examplekey" -H "Content-Type: application/json" -d '{"to":"/topics/news","notification":{"title":"Game Update"},"data":{"level":"5","score":"9000"}}' https://fcm.googleapis.com/fcm/send
Expected OutputExpected
{"message_id":1234567890123458}
Key Concept

Notification messages automatically show alerts to users, while data messages deliver information silently for the app to handle.

Common Mistakes
Sending data messages expecting automatic notifications.
Data messages do not show notifications by themselves; the app must handle them to display alerts.
Use notification messages or combine notification and data fields to show alerts automatically.
Using notification messages to send complex data for app logic.
Notification messages have limited data fields and are mainly for alerts, not complex data handling.
Use data messages for sending custom data that the app processes.
Summary
Use notification messages to show alerts automatically on user devices.
Use data messages to send silent information the app can process in the background.
Combine notification and data fields to send alerts with extra custom data.