📖 Scenario: You are building a mobile app that sends notifications to users based on their interests. You want to use Firebase Cloud Messaging (FCM) to send messages to groups of users subscribed to specific topics.
🎯 Goal: Create a Firebase Cloud Messaging setup that subscribes users to a topic, configures a message payload, sends a message to the topic, and completes the messaging configuration.
📋 What You'll Learn
Create a topic subscription variable
Add a message configuration object
Send a message to the topic using Firebase Admin SDK
Complete the messaging setup with proper topic and message structure
💡 Why This Matters
🌍 Real World
Topic-based messaging allows apps to send notifications to groups of users interested in specific subjects, like news or sports updates.
💼 Career
Understanding Firebase topic messaging is useful for cloud engineers and mobile developers who build scalable notification systems.
Progress0 / 4 steps
1
Create a topic subscription variable
Create a variable called topic and set it to the string "news" to represent the topic users will subscribe to.
Firebase
Hint
Use const topic = "news"; to define the topic name.
2
Add a message configuration object
Create a constant called message that is an object with a notification property. The notification should have title set to "Breaking News" and body set to "New updates available.". Also add a topic property set to the variable topic.
Firebase
Hint
Define message as an object with notification and topic properties.
3
Send a message to the topic using Firebase Admin SDK
Use the Firebase Admin SDK's messaging().send() method to send the message object. Assign the result to a constant called response. Use await to wait for the send operation to complete.
Firebase
Hint
Use await admin.messaging().send(message); and assign it to response.
4
Complete the messaging setup with proper topic and message structure
Add a subscription call using admin.messaging().subscribeToTopic() to subscribe an array with a single token "user_token_123" to the topic. Assign the result to a constant called subscribeResponse using await.
Firebase
Hint
Use await admin.messaging().subscribeToTopic(["user_token_123"], topic); and assign it to subscribeResponse.
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
Step 1: Understand topic-based messaging concept
Topic-based messaging allows grouping devices under a topic to send messages to all subscribers at once.
Step 2: Identify the main purpose
The main purpose is to send messages to multiple devices subscribed to the same topic efficiently.
Final Answer:
To send messages to multiple devices subscribed to a topic -> Option A
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
Step 1: Identify subscription function
To add a device to a topic, Firebase provides the subscribeToTopic() function.
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
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'}.
Step 2: Determine the outcome
This correctly sends the notification to all devices subscribed to the 'news' topic.
Final Answer:
A notification with title 'Hello' and body 'World' is sent to all devices subscribed to 'news' -> Option A
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: