/firebase-tutorials

How to subscribe user to FCM topic?

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

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to subscribe user to FCM topic?

 

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:

  • Add Firebase to your Android project by including the google-services.json file in the app directory.
  • Make sure your 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.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022