How to use Firebase cloud messaging in kotlin with Android Studio.

Kumar Shubhankar
3 min readMay 13, 2021

We used a send notification to database from the app.

We will add the first dependency after creating a new project in firebase .It will connect the app to firebase and will do some code.

Let’s starts…

Steap 1 . It added a dependency build.gradle.

implementation 'com.google.firebase:firebase-messaging:21.0.1'
implementation 'com.google.firebase:firebase-analytics:18.0.2'

AndroidManifest.xml:

<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>

steap 2. we will create project in firebase.

Already created a project called MessageNotification. Now click on android icon and add a package name , nickName and SHA 1, click on register app after downloading SDK and copy it and paste android project SDK.

Here paste it. In this project I already pasted google-service.json file (SDK).

now click on tools in android project and click on Firebase .

  1. After clicking on firebase then open firebase fields and click on Cloud messaging after click on add FCM to your app after click on accept Changes button .

steap 3 . We will create new class file.

FirebaseService.kt :

package com.example.usenotification

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


class FirebaseService : FirebaseMessagingService() {

val TAG = "FCM Service"

override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.d(TAG, "From: " + remoteMessage!!.from)
Log.d(TAG, "Notification Message Body: " + remoteMessage.notification?.body!!)

val intent = Intent(this@FirebaseService,MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra("fcm_message", remoteMessage.notification?.body)
startActivity(intent)
}

}

In this file add a override fun onMessageReceived after adding log message.

ActivityMain.kt:

package com.example.usenotification

import android.content.DialogInterface
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.AlertDialog

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val intent = intent
val message = intent.getStringExtra("fcm_message")

if (!message.isNullOrEmpty()){
AlertDialog.Builder(this)
.setTitle("Firebase Alert!")
.setMessage(message)
.setPositiveButton("OK", DialogInterface.OnClickListener { dialog, which -> }).show()
}
}
}

In this file add if condition for check null safety and add alert dialog and title

Here GitLab project links UseNotification

https://gitlab.com/akashkumar.edugaon/usernotification.

Steap 4 : Again Go to the firebase Database.

Go to cloud messaging click on a new notification after adding a title, message and other fields after click on next and select an app package name after click on review and publish it.

After

After

Thats all…

thanks

--

--