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

firebase-cloud-messaging-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 MainActivity.java:

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:
firebase-cloud-messaging-run-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:
firebase-cloud-messaging-compose-topic

More data:
firebase-cloud-messaging-compose-message-2

Click on SEND MESSAGE.

– Return to Android Studio:
firebase-cloud-messaging-result

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

III. Source code

FCM-TOPICmessages


Related Posts


Got Something To Say:

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

*