Kotlin Firebase Cloud Messaging – How to Receive Messages | Android

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that help us deliver messages reliably at no cost. In this tutorial, we’re gonna look at way to receive Messages in an Android App using FCM.

More Practice: Kotlin Firebase Cloud Messaging – How to Subscribe TOPIC & Receive Messages | Android

I. Firebase Cloud Messaging

1. Usage

With FCM, we can:
– notify a client that new data is available to sync.
– send notification messages to drive user re-engagement and retention.
– create instant messaging with a payload of up to 4KB to a client app.

2. How to receive Messages
2.1 Add Firebase to Android Project

– Create new Android App including Kotlin support.
– From menu bar, Tool -> Firebase:

– Assistant window will be shown. Choose Cloud Messaging, then click on Setup Firebase Cloud Messaging:

– At the step 1, click on Connect to Firebase:

– In new Window, choose Create new Project and fill in the name of Firebase Project:

– Click on Connect to Firebase, the Browser will show new Firebase Project that has just been created:

– Come back to Android Studio, at the step 2, click on Add FCM to your app:

– You will see a new window that shows the code will be add to build.gradle files. Click on Accept Changes:

– Now view your Project under Project type, you can see google-services.json file:

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 Extend FirebaseMessagingService

Create class that inherits FirebaseMessagingService class and override onMessageReceived() method:

2.4 Android Manifest

For Firebase Service:

II. Practice

1. Goal

We will build an Android App that can:
– receive Message 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


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 the instruction above to add Firebase to Android Project.

4.2 Create class for handling messages

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

4.3 Add Firebase Service to AndroidManifest

4.4 Run & Check result

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

– 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:

III. Source code


By JavaSampleApproach | December 11, 2017.

Related Posts

Got Something To Say:

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