简体   繁体   English

FCM 通知未在 android 的前台显示

[英]FCM Notification are not showing in foreground in android

Notifications are showing in the background but when the app is in the foreground notifications are not showing.通知在后台显示,但当应用程序在前台时,通知不显示。 I applied many solutions but they do not work for me.我应用了许多解决方案,但它们对我不起作用。 Can anyone tell me where is my mistake?谁能告诉我我的错误在哪里? thanks in advance提前致谢

Here is Manifest这是清单

        <service
        android:exported="false"
        android:name=".services.MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/cute" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/design_default_color_on_primary" />

Here is My ServicesClass这是我的服务类

class MyFirebaseMessagingService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {

        sendNotification(remoteMessage.notification?.title, remoteMessage.notification?.body)
        //just for testing
        Toast.makeText(applicationContext, "${remoteMessage.notification?.title}", Toast.LENGTH_LONG).show()
    }
    private fun sendNotification(messageTitle: String?, messageBody: String?) {
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent = PendingIntent.getActivity(
            this,
            0 /* request code */,
            intent,
            PendingIntent.FLAG_ONE_SHOT
        )
        val pattern = longArrayOf(500, 500, 500, 500, 500)
        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationBuilder:NotificationCompat.Builder = NotificationCompat.Builder(applicationContext)
            .setSmallIcon(com.dextrologix.dham.rfms.resident.R.drawable.cute)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setVibrate(pattern)
            .setLights(Color.BLUE, 1, 1)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent) as NotificationCompat.Builder
        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(0, notificationBuilder.build())
    }
}

It seems like you are missing notification channel.您似乎缺少通知渠道。 They are required for android notifications. android 通知需要它们。 docs link 文档链接

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

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