[英]FCM Notification are not showing in foreground in android
通知在后台显示,但当应用程序在前台时,通知不显示。 我应用了许多解决方案,但它们对我不起作用。 谁能告诉我我的错误在哪里? 提前致谢
这是清单
<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" />
这是我的服务类
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())
}
}
您似乎缺少通知渠道。 android 通知需要它们。 文档链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.