android – How to send fcm notification when app is not running-ThrowExceptions

Exception or error:

I have tried many solutions but none of them are worked. I am able receive FCM notification when app is active, but not getting notification when app is background or killed.

How to solve:

you need to create a service class extending FirebaseMessagingService and override onMessageReceived method in that class to send notification

class MyFirebaseMessagingService : FirebaseMessagingService() {

    override fun onMessageReceived(message: RemoteMessage) {
        super.onMessageReceived(message)
        try {
            message.notification?.let {
                showNotification(
                    it.title ?: "",
                    it.body ?: ""
                )

            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

you are now getting the notification info from backend and showing it as a notification by using the function showNotification. of course, you must implement the function showNotification. it just a simple function for showing notifications in android

Edit: this the implementation of the function, add this to your class

class MyFirebaseMessagingService : FirebaseMessagingService() {

    companion object {
        const val channelId = "Channel"
        const val channelName = "MyChannel"
        const val smallIcon: Int = R.drawable.ic_logo
        const val notificationId = 1
    }

        fun showNotification(myTitle: String, myBody: String) {

        val notificationBuilder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Notification.Builder(applicationContext, channelId)
        } else {
            Notification.Builder(applicationContext)
        }

        val intent = Intent(applicationContext, HomeActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(
            applicationContext,
            0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
        )
        val notificationManager =
            applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        when {
            Build.VERSION.SDK_INT >= 26 -> {
                val channel = NotificationChannel(
                    channelId,
                    channelName,
                    NotificationManager.IMPORTANCE_DEFAULT
                )
                notificationManager.createNotificationChannel(channel)
                notificationBuilder
                    .setContentIntent(pendingIntent)
                    .setContentText(myBody)
                    .setSmallIcon(smallIcon)
                    .setContentTitle(myTitle)
            }

            Build.VERSION.SDK_INT >= 24 -> notificationBuilder
                .setContentText(myBody)
                .setContentTitle(myTitle)
                .setSmallIcon(smallIcon)
                .setContentIntent(pendingIntent)

            else -> notificationBuilder
                .setContentText(myBody)
                .setContentTitle(myTitle)
                .setSmallIcon(smallIcon)
                .setContentIntent(pendingIntent)
        }
        notificationManager.notify(notificationId, notificationBuilder.build())
    }


}

###

If you want to get notification when app is background or killed your json object has to be this like:

{
“data”:{
“title” : “your_title”,
“body” : “your_body”
},
“to”: “device_token”,
“priority”: “high”
}

You can catch notification onMessageReceived

Leave a Reply

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