0
0
Firebasecloud~30 mins

Topic-based messaging in Firebase - Mini Project: Build & Apply

Choose your learning style9 modes available
Firebase Topic-Based Messaging Setup
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use await admin.messaging().subscribeToTopic(["user_token_123"], topic); and assign it to subscribeResponse.