[英]Notification not showing when app is in foreground in Kotlin
I am using Firebase push notification in kotlin and below are the code snippet for showing push notification 我在Kotlin中使用Firebase推送通知,以下是用于显示推送通知的代码段
mNotifyManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
createChannel(mNotifyManager)
val mBuilder = NotificationCompat.Builder(this, "bks-channel")
.setLargeIcon(largeIcon)
.setContentTitle("Bks")
.setSmallIcon(R.drawable.app_icon)
.setContentText(message)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
mNotifyManager.notify(getRandomNumber(), mBuilder.build())
And the createChannel() function is : 并且createChannel()函数是:
@TargetApi(26)
private fun createChannel(notificationManager: NotificationManager)
{
val name = "bks"
val description = "bks"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val mChannel = NotificationChannel(name, name, importance)
mChannel.description = description
mChannel.enableLights(true)
mChannel.lightColor = Color.BLUE
notificationManager.createNotificationChannel(mChannel)
}
Below are the server logs: 以下是服务器日志:
array(1) {
[9]=>
array(2) {
["name"]=>
string(14) "ABL Staff USER"
["fcm_response"]=>
array(2) {
["fields"]=>
array(3) {
["data"]=>
array(4) {
["click_action"]=>
int(2)
["title"]=>
string(19) "Attendance Reminder"
["body"]=>
string(49) "Hi ABL Staff USER, Please mark your attendance ! "
["sound"]=>
string(7) "default"
}
["registration_ids"]=>
array(11) {
[0]=>
string(152) "device_token_1"
}
["notification"]=>
array(4) {
["click_action"]=>
int(2)
["title"]=>
string(19) "Attendance Reminder"
["body"]=>
string(49) "Hi ABL Staff USER, Please mark your attendance ! "
["sound"]=>
string(7) "default"
}
}
Same are working for below android o in foreground and background but not in android o or above 同样适用于android o以下的前景和背景,但不适用于android o或以上的环境
FCM has sent two type of notification FCM已发送两种通知
Send the data message to show a notification when the app in the background 发送数据消息以在后台运行应用程序时显示通知
For more details check the below link 有关更多详细信息,请检查以下链接
Are you creating channel by name "bks-channel" ? 您要通过名称“ bks-channel”创建频道吗? Looks like you are not creating bks-channel instead you are creating channel by name bks.
看起来您不是在创建bks-channel,而是在通过名称bks创建频道。
Update your create channel method. 更新您的创建频道方法。
@TargetApi(26)
private fun createChannel(notificationManager: NotificationManager) {
val name = "bks-channel"
val description = "bks"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val mChannel = NotificationChannel(name, name, importance)
mChannel.description = description
mChannel.enableLights(true)
mChannel.lightColor = Color.BLUE
notificationManager.createNotificationChannel(mChannel)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.