简体   繁体   中英

How to send fcm notification when app is not running

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM