Bird
Raised Fist0
Firebasecloud~5 mins

Topic-based messaging in Firebase - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of topic-based messaging in Firebase Cloud Messaging?
easy
A. To send messages to multiple devices subscribed to a topic
B. To send messages only to a single device
C. To store messages in a database
D. To authenticate users before sending messages

Solution

  1. Step 1: Understand topic-based messaging concept

    Topic-based messaging allows grouping devices under a topic to send messages to all subscribers at once.
  2. Step 2: Identify the main purpose

    The main purpose is to send messages to multiple devices subscribed to the same topic efficiently.
  3. Final Answer:

    To send messages to multiple devices subscribed to a topic -> Option A
  4. Quick Check:

    Topic-based messaging = send to many devices [OK]
Hint: Topic messaging targets groups, not single devices [OK]
Common Mistakes:
  • Confusing topic messaging with single device messaging
  • Thinking it stores messages in database
  • Assuming it handles user authentication
2. Which Firebase function is used to subscribe a device to a topic?
easy
A. unsubscribeFromTopic()
B. sendToTopic()
C. getToken()
D. subscribeToTopic()

Solution

  1. Step 1: Identify subscription function

    To add a device to a topic, Firebase provides the subscribeToTopic() function.
  2. Step 2: Differentiate from other functions

    sendToTopic() sends messages, getToken() gets device token, unsubscribeFromTopic() removes subscription.
  3. Final Answer:

    subscribeToTopic() -> Option D
  4. Quick Check:

    Subscribe device = subscribeToTopic() [OK]
Hint: Subscribe uses subscribeToTopic(), not sendToTopic() [OK]
Common Mistakes:
  • Confusing sendToTopic() with subscription
  • Using getToken() to subscribe
  • Mixing unsubscribe with subscribe
3. Given this code snippet, what will happen?
const message = {
  topic: 'news',
  notification: { title: 'Hello', body: 'World' }
};
admin.messaging().sendToTopic(message.topic, message.notification);
medium
A. A notification with title 'Hello' and body 'World' is sent to all devices subscribed to 'news'
B. An error occurs because sendToTopic requires a message object, not separate topic and notification
C. The message is sent only to one device
D. The message is stored but not sent

Solution

  1. Step 1: Analyze sendToTopic usage

    The sendToTopic method expects a topic string followed by a payload object. Here, message.topic provides the string 'news' and message.notification provides the payload {title: 'Hello', body: 'World'}.
  2. Step 2: Determine the outcome

    This correctly sends the notification to all devices subscribed to the 'news' topic.
  3. Final Answer:

    A notification with title 'Hello' and body 'World' is sent to all devices subscribed to 'news' -> Option A
  4. Quick Check:

    sendToTopic(topic, payload) sends to subscribers [OK]
Hint: sendToTopic(topic_string, payload_object) sends to topic subscribers [OK]
Common Mistakes:
  • Confusing sendToTopic(topic, payload) with send(full_message_object)
  • Thinking it sends only to one device
  • Believing the message is stored but not sent
4. You wrote this code to send a message to a topic:
const message = {
  topic: 'sports',
  notification: { title: 'Game', body: 'Tonight at 8' }
};
admin.messaging().sendToTopic(message);
But it throws an error. What is the likely cause?
medium
A. sendToTopic expects the message object, but you passed only the topic string
B. The topic field should be inside the notification object
C. sendToTopic expects a topic string, not a message object
D. sendToTopic expects the message object, but you passed the whole object correctly

Solution

  1. Step 1: Check sendToTopic parameter type

    In Firebase Admin SDK, sendToTopic expects two parameters: topic string and payload object, not a single message object.
  2. Step 2: Identify mismatch

    Passing the whole message object as one argument causes an error because the function signature is different.
  3. Final Answer:

    sendToTopic expects a topic string, not a message object -> Option C
  4. Quick Check:

    sendToTopic(topic, payload) not sendToTopic(message) [OK]
Hint: sendToTopic needs topic string first, then payload [OK]
Common Mistakes:
  • Passing message object instead of topic string
  • Putting topic inside notification
  • Assuming one argument is accepted
5. You want to send a message only to devices subscribed to both 'news' and 'sports' topics. How can you achieve this using Firebase topic messaging?
hard
A. Send two separate messages, one to 'news' and one to 'sports' topics
B. Use condition parameter with "'news' in topics && 'sports' in topics" in sendToTopic
C. Subscribe devices to a combined topic 'news_sports' and send to that topic
D. Send a message to 'news' topic and filter devices manually

Solution

  1. Step 1: Understand topic combination options

    Firebase supports sending messages to multiple topics using conditions with logical operators.
  2. Step 2: Use condition parameter for AND logic

    Using condition: "'news' in topics && 'sports' in topics" sends message only to devices subscribed to both topics.
  3. Step 3: Evaluate other options

    Sending two messages sends to each topic separately, subscribing to combined topic requires extra setup, filtering manually is inefficient.
  4. Final Answer:

    Use condition parameter with "'news' in topics && 'sports' in topics" in sendToTopic -> Option B
  5. Quick Check:

    Use condition for multiple topic targeting [OK]
Hint: Use condition with && for multiple topic targeting [OK]
Common Mistakes:
  • Sending separate messages instead of using condition
  • Creating combined topic unnecessarily
  • Filtering devices manually after sending
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard