Sub topic 7
Sending Your First Message
This chapter assumes that Firebase is connected to your Flutter app, firebase_messaging is initialized, and you can obtain an FCM registration token. A registration token identifies one app installation. You also need a Firebase project with Cloud Messaging enabled.
Choose a target
FCM can send to one registration token, a topic, or several tokens. Start with one token while testing. It gives you a clear answer about which device should receive the message.
| Target | Use it for | Example |
|---|---|---|
| Registration token | One app installation | A message about one user's order |
| Topic | A group of subscribed installations | News alerts for users subscribed to `news` |
| Multiple tokens | A controlled group of installations | A batch of selected devices |
The message travels through FCM before reaching Android or Apple's Push Notification service (APNs) for iOS.
Send from the Firebase console
The Firebase console is useful for a manual test:
- Open your Firebase project and select Messaging.
- Create a new campaign or notification.
- Enter a notification title and body.
- In the target section, select a specific app or device if the console offers that option.
- Add custom data such as
routewith a value like/orders/42when you need the app to open a particular screen. - Send the test message to the device.
The console is convenient, but production systems normally send from a trusted backend. Do not put service-account credentials or server access tokens in the Flutter app.
Send with the HTTP v1 API
The HTTP v1 API requires an OAuth 2.0 access token created with a Firebase service account. Keep that credential on your server. Replace the placeholders in this request with values from your server environment.
curl -X POST \
"https://fcm.googleapis.com/v1/projects/PROJECT_ID/messages:send" \
-H "Authorization: Bearer OAUTH_ACCESS_TOKEN" \
-H "Content-Type: application/json; UTF-8" \
-d '{
"message": {
"token": "DEVICE_REGISTRATION_TOKEN",
"notification": {
"title": "Order shipped",
"body": "Your order #42 is on its way."
},
"data": {
"route": "/orders/42"
}
}
}'The notification object contains display text. The data object contains application values. FCM data values must be strings, so encode numbers or objects as strings before sending them.
To target a topic, replace token with a topic field:
{
"message": {
"topic": "news",
"notification": {
"title": "Daily update",
"body": "A new story is available."
}
}
}The device must subscribe to that topic before it can receive the message. Topic names should represent broad groups, not private user data.
Check the response
A successful request returns a message name similar to projects/PROJECT_ID/messages/0:.... An HTTP 400 response usually indicates invalid JSON, a missing field, or an invalid token format. An HTTP 401 or 403 response usually indicates an invalid or expired OAuth token or insufficient service-account permissions.
Exercise
Which target should you use to test delivery to exactly one Flutter installation, and where should the HTTP v1 credential live?
Show answer
Use that installation's FCM registration token. Store the HTTP v1 credential on a trusted server, never in the Flutter application.