Push Notifications in Android App using Kotlin & Firebase

Initial Steps

  1. Start a new project in Android Studio (my version 3.1.4)
  2. Provide the appication name as you wish. Company Domain: for example smredlabs.com.
  3. Check Include Kotlin Support
  4. Click Next and select Empty Activity
  5. Click Finish after providing the activity name
For detailed steps refer: Create New Project

Add Google Account to Android Studio

  1. Sign in with your Google account to start adding cloud functionalities. Click the person icon on the top right corner of the Android Studio as shown below in the image.
  2. Clicking on sign in will take to you a browser where you need to fill your credentials to your Google account.
  3. Allow access to Android Studio to access your Google Account

Add Firebase support to your project

  1. From menu: click Tools > Firebase
  2. Assistant window will open up from right hand side
  3. Click on Cloud Messaging
  4. Click Setup Firebase Cloud Messaging
  5. Click Connect to Firebase
  6. You will be prompted with a window where you can create a new Firebase project or you can select any of the existing one. Here we create new with the name of our application as default.
  7. Select your country and then click Connect to Firebase
  8. If status shows failed repeat the process - from step 5
  9. The assistant window will now show that the Firebase is connected.
  10. Click on Add FCM to your app
  11. The above step will open a window. Click Accept Changes
Note: The above is now set to receive messages when your app is in the background. In this case, the notification is delivered to the device's system tray. A user tap on a notification opens the app launcher by default.

Run your app. To send the notification go to console.firebase.google.com. Login with your credentials. Click on the project you created for the app. From menu click Cloud Messaging, under Grow section of the menu. Create a new message by filling the Message text and selecting your app as shown in the image below. Then click on Send Message button. You can see that the message is displayed in the notification bar of your device. Clicking on the same will open your app.

Handling messages

In order to handle the messages received we need to create a service class that extends FirebaseMessagingService.

  1. In the project panel right click on the com.smredlabs.myapplication (app > java). Select New > Kotlin File/Class.
  2. Provide the class name as MyFirebaseMessagingService and click Finish
  3. Update the created file with the following code.
  4. If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback. (For all messages where onMessageReceived is provided, your service should handle any message within 10 seconds of receipt.)
  5. With the help of the created service, whenever a new message arrives to our app the onMessageReceived will be triggered.

package com.smredlabs.myapplication

import android.util.Log
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

class MyFirebaseMessagingService : FirebaseMessagingService() {
    val LOGTAG = "FirebaseService"
    override fun onMessageReceived(remoteMessage: RemoteMessage?) {
        // We handle FCM based messages here. This service is necessary to recieve notifications in foregrounded apps
        // Here I am just logging the message recieved. You can implement your requirement here, for example
        // you can display the message received in the alert dialog, or save it in database etc.
        Log.d(LOGTAG, "Notification Message Body: " + remoteMessage.notification!!.body)

Now in your AndroidManifest.xml (app > manifests) update the following service tag

<?xml version="1.0" encoding="utf-8"?>
    <activity android:name=".MainActivity">

            <action android:name="com.google.firebase.MESSAGING_EVENT"/>

Now run your app and let it be in the foreground. Visit console.firebase.google.com and send a message. Once the message is sent, you should see the the log entries displayed in your Android Studio Logcat.

Email your queries to solutiomagister@gmail.com