Kotlin 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 with Kotlin langugage.

Related Post: Kotlin Firebase Cloud Messaging – How to Receive Messages | Android

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 to Android Project

Follow these steps.

2.2 Check dependencies

Open Gradle file to check what happens, we can see that some code has been added:
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 3.0.1
– Android Studio 3.x
– Firebase Android SDK 11.x

3. Project Structure

kotlin-fcm-subscribe-topic-receive-message-structure

4. Step by step
4.1 Create Android Project & Add Firebase Cloud Messaging

– Generate new Android Project with package com.javasampleapproach.kotlin.firebase.fcm.
– Follow these steps to add Firebase to Android Project.

4.2 Create Activity with Subscribe/Unsubscribe Button

– Modify activity_main.xml that has two Buttons:

– Handle Click Listener for each Button in MainActivity.kt:

4.3 Create class for handling messages

Inside com.javasampleapproach.kotlin.firebase.fcm package, create MyFCMClass:

4.4 Add Firebase Service to AndroidManifest

4.5 Run & Check result

– Use Android Studio, build and Run your Android App:
kotlin-fcm-subscribe-topic-receive-message-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:
kotlin-firebase-cloud-messaging-test-console

More data:
kotlin-firebase-cloud-messaging-test-console-options

Click on SEND MESSAGE.

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

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

III. Source code

Kotlin-FCM-subscribe-topic

By JavaSampleApproach | December 12, 2017.

Last updated on July 13, 2018.


Related Posts


Got Something To Say:

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

*