setup FCM in Android

Add Firebase to an existing android project

Click Tools -> Firebase

Screen Shot 2019-06-11 at 18.29.36.png

A new firebase sidebar will appear. From that click Set up Firebase Cloud Messaging

Screen Shot 2019-06-11 at 18.31.40.png

Next click on Connect to Firebase. It will ask to login to firebase console with gmail account. Then follow the instructions.

After that click on Add FCM to your app then follow the instructions.

Receiving message from FCM

Create a new kotlin class called MyFirebaseMessagingService.kt and add this following code:

class MyFirebaseMessagingService: FirebaseMessagingService() {

val TAG = "FirebaseMessagingService"

override fun onNewToken(token: String?) {
Log.d("TOKEN", token)

override fun onMessageReceived(remoteMessage: RemoteMessage) {

Log.d(TAG, remoteMessage.from)

if (remoteMessage.notification != null) {
showNotification(remoteMessage.notification?.title, remoteMessage.notification?.body)

// show notification
private fun showNotification(title: String?, body: String?) {
val intent = Intent(this,
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this)

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

Then from AndroidManifest.xml add this service:


<action android:name=""/>

Push notification from the console dashboard

In the first page, scroll down a little bit and choose Cloud Messaging


Inside the new page, click Send your first message


Inside Message Text, write the information you want the user to know (the long message). In Target choose User segment and in Select app choose the application package name. Next (optional).

After finish click Review then Publish

Trigger push notification

So by default, we will set FirebaseMessaging to automatically subscribe to topic GENERAL


Then when we want to turn off the notification, we just need to unsubscribe from this topic


Please refer to this Github link

Thank you!