0
0
Firebasecloud~5 mins

Topic-based messaging in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
Topic-based messaging lets you send messages to many devices that subscribe to a common topic. This helps you reach groups of users easily without managing individual device addresses.
When you want to send a notification to all users interested in sports updates.
When you need to broadcast a message to all devices subscribed to a weather alert topic.
When you want to send promotional offers to users subscribed to a deals topic.
When you want to notify all app users about a new feature release.
When you want to send emergency alerts to users in a specific region subscribing to that topic.
Config File - firebase-messaging-config.json
firebase-messaging-config.json
{
  "project_info": {
    "project_id": "example-project-123",
    "project_number": "123456789012"
  },
  "messaging_sender_id": "123456789012",
  "api_key": "AIzaSyD-exampleAPIKey1234567890",
  "app_id": "1:123456789012:web:abcdef123456"
}

This JSON file contains the Firebase project configuration needed to initialize Firebase Cloud Messaging in your app. It includes your project ID, sender ID, API key, and app ID.

Commands
Log in to your Firebase account to access your projects and send messages.
Terminal
firebase login
Expected OutputExpected
✔ Success! Logged in as user@example.com
List all Firebase projects linked to your account to find the project ID you want to use.
Terminal
firebase projects:list
Expected OutputExpected
Project Display Name: example-project-123 Project ID: example-project-123 Project Number: 123456789012
Send a notification message to all devices subscribed to the 'news' topic.
Terminal
firebase messaging:send --topic news --message '{"notification": {"title": "Hello!", "body": "This is a topic message."}}'
Expected OutputExpected
Message sent successfully to topic: news
--topic - Specifies the topic to send the message to.
--message - Defines the message content in JSON format.
Subscribe a device with the given token to the 'news' topic so it receives messages sent to that topic.
Terminal
firebase messaging:subscribe --topic news --token eXampleDeviceToken123
Expected OutputExpected
Token eXampleDeviceToken123 subscribed to topic news
--topic - Specifies the topic to subscribe to.
--token - Specifies the device token to subscribe.
Unsubscribe a device from the 'news' topic to stop receiving messages sent to that topic.
Terminal
firebase messaging:unsubscribe --topic news --token eXampleDeviceToken123
Expected OutputExpected
Token eXampleDeviceToken123 unsubscribed from topic news
--topic - Specifies the topic to unsubscribe from.
--token - Specifies the device token to unsubscribe.
Key Concept

If you remember nothing else from this pattern, remember: topic-based messaging lets you send one message to many devices by grouping them under a shared topic.

Common Mistakes
Trying to send a topic message without devices subscribed to that topic.
No devices will receive the message because no one is subscribed.
Make sure devices subscribe to the topic before sending messages.
Using an invalid or expired device token when subscribing or unsubscribing.
The subscription or unsubscription will fail silently or cause errors.
Use valid, current device tokens obtained from the client app.
Sending messages with incorrect JSON format in the message content.
The message will be rejected or not delivered properly.
Validate JSON syntax and structure before sending.
Summary
Use the Firebase CLI to log in and list your projects.
Subscribe devices to topics using their device tokens.
Send messages to all devices subscribed to a topic with a single command.
Unsubscribe devices to stop receiving topic messages.