简体   繁体   English

应用程序未运行时如何发送 fcm 通知

[英]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.我可以在应用程序处于活动状态时收到 FCM 通知,但在应用程序处于后台或被终止时无法收到通知。

you need to create a service class extending FirebaseMessagingService and override onMessageReceived method in that class to send notification您需要创建一个扩展 FirebaseMessagingService 的服务类并覆盖该类中的 onMessageReceived 方法以发送通知

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 .您现在正在从后端获取通知信息,并使用函数showNotification将其显示为通知。 of course, you must implement the function showNotification .当然,您必须实现函数showNotification it just a simple function for showing notifications in android它只是一个简单的功能,用于在 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:如果您想在应用程序处于后台或终止时收到通知,您的 json 对象必须是这样的:

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

You can catch notification onMessageReceived您可以在MessageReceived上捕捉通知

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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