Firebase Cloud Messaging – How to Subscribe TOPIC & Receive Messages | Android

FCM Topic Messaging helps us to send a message to multiple devices that have subscribed to a particular TOPIC. We compose topic messages, then FCM handles routing and delivering the messages to devices. In this tutorial, we’re gonna look at way to subscribe TOPIC, receive Messages, then unsubscribe in an Android App Client.

Related Post: Firebase Cloud Messaging – Spring Server to Push Notification Example | Spring Boot

I. Topic Messaging on Android

1. Usage

Topic messaging is suitable for publicly available information such as news, weather…
– Supports unlimited topics and subscriptions for each App.
– Payload is limited to 2KB.

If you want to:
– Deliver fast, secure message to single devices or small groups: use registration tokens instead of TOPICs.
– Send messages to multiple devices per user: consider device group messaging.

2. How to Subscribe TOPIC & Receive Messages
2.1 Add Firebase Config file

Follow this guide to generate google-services.json file and move it into your Android App root directory. You don’t need to get SHA-1 Key in this example.

2.2 Add dependencies

build.gradle (project-level):

build.gradle (App-level):

2.3 Subscribe/Unsubscribe TOPIC

– Client can subscribe to any existing TOPIC.
– If Client subscribes to a new TOPIC, that TOPIC is created automatically in FCM, then any of other Clients can subscribe to it.

2.4 Receive Message

Create class that extends FirebaseMessagingService class and overrides onMessageReceived() method:

2.5 Android Manifest

For Firebase Service:

II. Practice

1. Goal

We will build an Android App that can:
– subscribe/unsubsribe to a TOPIC
– receive Message of that TOPIC when running
– get Notification payload and Data payload

To send notification messages, we use Notifications Composer in the Firebase Console.

2. Technology

– Gradle 2.3.3
– Android Studio 2.x
– Firebase Android SDK 11.x

3. Project Structure


4. Step by step
4.1 Create Android Project

– Generate new Android Project with package com.javasampleapproach.fcm.
build.gradle (project-level):

build.gradle (App-level):

4.2 Create Firebase Project & Add Firebase Config file

– Follow this guide to generate google-services.json file and move it into your Android App root directory. You don’t need to have SHA-1 Key in this example, just leave it blank.

– Make sure that package_name in google-services.json has a correct value according to:
+ applicationId in build.gradle (App-level).
+ package in AndroidManifest.xml.
In this case, it is com.javasampleapproach.fcm.

4.3 Create Activity with Subscribe/Unsubscribe Button

– Modify activity_main.xml that has two Buttons:

– Handle Click Listener for each Button in

4.4 Create class for handling messages

Inside com.javasampleapproach.fcm package, create MyFCMClass:

4.5 Add Firebase Service to AndroidManifest

4.6 Run & Check result

– Use Android Studio, build and Run your Android App:

Click SUBSCRIBE button.

– Go to Firebase Console, click on the Project that you created above. Click on Notification in the left menu of the site.

– Compose Message:

More data:


– Return to Android Studio:

– Click UNSUBSCRIBE button, then go to Firebase Console to send another message. Android Logcat shows nothing else.

III. Source code


By grokonez | July 8, 2017.

Last updated on July 13, 2018.

Related Posts

9 thoughts on “Firebase Cloud Messaging – How to Subscribe TOPIC & Receive Messages | Android”

    1. Hi numanqmr,

      Firebase Message Composer currently doesn’t support to send message to multiple topics at the same time.


  1. Hi,
    First things first, I love the way you put your knowledge on the table for the people floating around the darkness sea :P,

    Last but not least, I’d like to know whether you’ve already figured out how to send notifications using a mechanism very much like the one you’ve explained above but using registration tokens instead topics.

    I appreciate a lot your thoughts !

    Regards !

  2. hi iam doing code both side properly i got message in my logcat bt i cant get notification in my mobile app please help me for getting notification in mobile screen

Got Something To Say:

Your email address will not be published. Required fields are marked *