Android 通知在活动中有效,但在服务中无效

Android 通知在活动中有效,但在服务中无效,android,firebase,firebase-cloud-messaging,android-notifications,firebase-notifications,Android,Firebase,Firebase Cloud Messaging,Android Notifications,Firebase Notifications,我正在尝试将Firebase云消息集成到我的应用程序中。我在showNotification函数中使用的代码来自。我在活动中尝试了它,但不确定为什么它在服务中不起作用。println显示函数正在被调用,并且值正在按预期到来。功能中是否缺少任何内容 class MessagingService : FirebaseMessagingService() { override fun onMessageReceived(remoteMessage: RemoteMessage) {

我正在尝试将Firebase云消息集成到我的应用程序中。我在
showNotification
函数中使用的代码来自。我在活动中尝试了它,但不确定为什么它在服务中不起作用。
println
显示函数正在被调用,并且值正在按预期到来。功能中是否缺少任何内容

class MessagingService : FirebaseMessagingService() {

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        if (remoteMessage.data.isNotEmpty()) {
            val message = Gson().fromJson(remoteMessage.data.toString(), Message.Res::class.java).payload!!
            showNotification(message.title, message.body, message.count)
        }
    }

    private fun showNotification(title: String?, body: String?, count: Int = 0) {
        
        println("_print::showNotification(title:$title, body:$body, count:$count)")

        val mainPendingIntent = PendingIntent.getActivity(
            applicationContext, BuildConfig.REQUEST_CODE,
            Intent(applicationContext, MainActivity::class.java),
            PendingIntent.FLAG_UPDATE_CURRENT
        )

        val builder = NotificationCompat.Builder(applicationContext, "channel_email_1")
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher_round))
            .setContentIntent(mainPendingIntent)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setColor(ContextCompat.getColor(applicationContext, R.color.primary))
            .setSubText(if (count > 1) "You have $count pending notifications" else title)
            .setCategory(Notification.CATEGORY_EMAIL)
            .setPriority(1)
            .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)

        NotificationManagerCompat.from(applicationContext)
            .notify(BuildConfig.NOTIFICATION_ID, builder.build())
    }
}
而不是

val mainPendingIntent = PendingIntent.getActivity(
        applicationContext, BuildConfig.REQUEST_CODE,
        Intent(applicationContext, MainActivity::class.java),
        PendingIntent.FLAG_UPDATE_CURRENT
    )

NotificationManagerCompat.from(applicationContext)
        .notify(BuildConfig.NOTIFICATION_ID, builder.build())
我使用:

    val notificationManager =
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    // Since android Oreo notification channel is needed.
    setupChannels(notificationManager, builder)

    // Create pending intent, mention the Activity which needs to be triggered
    // when user clicks on notification.
    val notificationId = Random.nextInt()
    navigateToScreen(builder, notificationId)

    val notification = builder.build()
    notificationManager.notify(notificationId, notification)
}

private fun setupChannels(notificationManager: NotificationManager,
                          builder: NotificationCompat.Builder) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channelId = "channel_email_1"
        val channel = NotificationChannel(channelId, "title",
            NotificationManager.IMPORTANCE_DEFAULT).apply {
            description = "body"
            // Add other settings.
            lockscreenVisibility = Notification.VISIBILITY_PUBLIC
            canShowBadge()
            setShowBadge(true)
        }
        notificationManager.createNotificationChannel(channel)
        builder.setChannelId(channelId)
    }
}

private fun navigateToScreen(builder: NotificationCompat.Builder,
                             notificationId: Int) {
    val intent = Intent(this, MainActivity::class.java)
    val pendingIntent = PendingIntent.getActivity(this, notificationId, intent,
        PendingIntent.FLAG_UPDATE_CURRENT)
    builder.setContentIntent(pendingIntent)
}

您是如何在服务中接收该卡的?哪些不起作用?
\u print::showNotification(标题:卡转移请求,正文:您将eToll卡转移至遗嘱认证的请求已收到。参考号BNZ160970,count1)