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

问题描述

我正在尝试将 Firebase Cloud Messages 集成到我的应用中。我在 showNotification 函数中使用的代码来自 Android User Interface Samples。我在 Activity 中尝试了它并且它起作用了,但我不确定为什么它在服务中不起作用。 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)
}