Bird
Raised Fist0
Firebasecloud~20 mins

Topic-based messaging in Firebase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Topic-based Messaging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
How does topic-based messaging deliver notifications?

In Firebase Cloud Messaging, when you send a message to a topic, how does the service deliver the message?

AThe message is delivered to all devices subscribed to the topic, regardless of their connection state.
BThe message is stored on the server and delivered only when the user opens the app.
CThe message is sent to a random device subscribed to the topic.
DThe message is sent only to devices currently connected to the server.
Attempts:
2 left
💡 Hint

Think about how topics help reach multiple devices at once.

❓ Architecture
intermediate
2:00remaining
Choosing topics for user groups

You want to send notifications to users grouped by their interests. Which topic naming strategy is best to organize these groups?

ACreate a topic per user and send messages to each topic individually.
BUse a single topic for all users and filter messages on the client side.
CCreate one topic per interest, like 'sports', 'music', and subscribe users accordingly.
DUse random topic names and assign users randomly.
Attempts:
2 left
💡 Hint

Think about how topics help target groups efficiently.

❓ security
advanced
2:30remaining
Securing topic subscriptions

Which method best prevents unauthorized users from subscribing to sensitive topics?

AUse public topic names and rely on obscurity to protect sensitive topics.
BAllow all users to subscribe to any topic without restrictions.
CUse Firebase Authentication and verify user identity before subscribing to topics on the client.
DSubscribe users to topics only on the server after verifying permissions.
Attempts:
2 left
💡 Hint

Consider where subscription control should happen for security.

❓ Configuration
advanced
2:30remaining
Correct topic subscription code snippet

Which code snippet correctly subscribes a device to a topic using Firebase Cloud Messaging in JavaScript?

Firebase
import { getMessaging, getToken, onMessage } from 'firebase/messaging';

const messaging = getMessaging();

// Which option correctly subscribes to 'news' topic?
Amessaging.subscribe('news');
BgetToken(messaging).then(token => fetch('/subscribe', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ token, topic: 'news' }) }));
Cmessaging.subscribeToTopic('news');
Dmessaging.subscribeTopic('news');
Attempts:
2 left
💡 Hint

Firebase client SDK does not directly subscribe to topics; it requires server interaction.

✅ Best Practice
expert
3:00remaining
Handling topic messaging at scale

Your app has millions of users subscribed to multiple topics. What is the best practice to avoid hitting Firebase Cloud Messaging limits when sending topic messages?

ABatch topic messages and send them with delays to respect Firebase rate limits.
BSend one message per topic for all topics at once, ignoring rate limits.
CSend messages to individual device tokens instead of topics to avoid limits.
DUse random topic names to distribute load evenly.
Attempts:
2 left
💡 Hint

Think about how to manage large scale messaging without errors.

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