简体   繁体   English

FCM前台和后台通知布局视图和PendingIntent

[英]FCM Foreground and Background notification layout view and PendingIntent

I'm creating notification using Firebase and FCM我正在使用 Firebase 和 FCM 创建通知

class FcmServices: FirebaseMessagingService() {

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        sendNotification(remoteMessage)
    }

    override fun onNewToken(token: String) {
        super.onNewToken(token)
    }

    private fun sendNotification(remoteMessage: RemoteMessage) {

        val notifTitle = remoteMessage.notification?.title
        val notifBody = remoteMessage.notification?.body

        val builder: NotificationCompat.Builder?
        val notificationManager =
            this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        val channelId = "101"

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val notificationChannel = NotificationChannel(channelId, "notification", importance)
            notificationManager.createNotificationChannel(notificationChannel)
            builder = NotificationCompat.Builder(applicationContext, notificationChannel.id)
        } else {
            builder = NotificationCompat.Builder(applicationContext)
        }

        val intent = Intent(this, NotifikasiActivity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        builder.setSmallIcon(R.drawable.logo_bprmsa)
            .setLargeIcon(
                BitmapFactory.decodeResource(
                    this.resources,
                    R.drawable.logo_bprmsa
                )
            )
            .setContentIntent(pendingIntent)
            .setContentTitle(notifTitle)
            .setContentText(notifBody)
            .setDefaults(Notification.DEFAULT_VIBRATE)

        var alarmSound: Uri? = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        if (alarmSound == null) {
            alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
            if (alarmSound == null) {
                alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
            }
        }
        builder.setSound(alarmSound)
        builder.setAutoCancel(true)
        builder.setWhen(System.currentTimeMillis())
        builder.setDefaults(NotificationCompat.DEFAULT_VIBRATE)
        builder.priority = NotificationCompat.PRIORITY_HIGH
        notificationManager.notify(Random().nextInt(), builder.build())

    }

}

I need to open the NotificationActivity after click the notification received from Firebase, but the problem is the PendingIntent is not working while the app is in background state. Is there any different condition between foreground and background state code?单击从 Firebase 收到的通知后,我需要打开NotificationActivity ,但问题是PendingIntent在应用程序处于后台时不起作用 state。前台后台state 代码之间是否存在任何不同的情况?

And for the second, the layout of foreground and background is also different ( background state without the icon shown)其次,前景背景布局也不同(背景state 没有显示图标)

通知

https://firebase.google.com/docs/cloud-messaging/concept-options https://firebase.google.com/docs/cloud-messaging/concept-options

Based on the official document from Google, it is intentional.根据谷歌的官方文档,这是故意的。 The Icon and any other buttons or others you wish to display other than the title and the message won't display if your app is not running foreground.如果您的应用程序未在前台运行,则图标和任何其他按钮或您希望显示的除标题和消息以外的其他内容将不会显示。

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

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