Subscribe users to an FCM topic with our step-by-step guide. Learn Firebase integration, permission setup, subscription, and handling notifications in your app.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set up Firebase in Your Project
First, ensure you have a Firebase project set up. You can create a Firebase project in the Firebase console if you haven't already. Then, integrate Firebase into your app by following these sub-steps:
google-services.json file in the app directory.build.gradle files are properly configured:// In project-level build.gradle
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.8'
}
}
// In app-level build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
dependencies {
implementation platform('com.google.firebase:firebase-bom:31.0.0')
implementation 'com.google.firebase:firebase-messaging'
}
Step 2: Request Required Permissions
Make sure to request the necessary permissions in your AndroidManifest.xml file. Add the following permissions if they're not already present:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE\_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
Step 3: Initialize Firebase in Your Application
Initialize Firebase in your application class to ensure it's set up when your app starts:
import android.app.Application;
import com.google.firebase.FirebaseApp;
import com.google.firebase.messaging.FirebaseMessaging;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
FirebaseApp.initializeApp(this);
}
}
Step 4: Subscribe to an FCM Topic
To subscribe a user to a specific FCM topic, use the FirebaseMessaging instance in your code as shown below:
FirebaseMessaging.getInstance().subscribeToTopic("your_topic_name")
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
// Subscription was successful
} else {
// Handle failure
}
});
Step 5: Handle Topic Messages in Your App
Ensure your app can handle incoming messages for the subscribed topic. Implement a FirebaseMessagingService to handle FCM messages:
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import android.util.Log;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Handle the FCM message
Log.d("FCM", "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d("FCM", "Message data payload: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d("FCM", "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
}
Step 6: Update AndroidManifest.xml for the Service
Finally, register the FirebaseMessagingService in your AndroidManifest.xml:
<service android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING\_EVENT"/>
</intent-filter>
</service>
With this guide, your app is now set up to subscribe users to an FCM topic and handle topic-based messages.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.