每次我在前台服务中使用startForeground()在其通知中进行状态更新时,我遇到三星Galaxy S7 Edge的问题,显示弹出通知.

首先,此问题出现在所有Android 8.0设备上,但很容易修复,我的通知通道优先级设置为IMPORTANCE_LOW.但问题仍然存在于三星设备上.

所以,问题是,如何在Samsung 8.0设备上静默更新前台服务通知?

我的代码如下.

申请类:

override fun onCreate() {

super.onCreate()

//other code

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

notificationManager.createNotificationChannels(listOf(

//

NotificationChannel(MY_NOTIFICATION_CHANNEL,"My channel",NotificationManager.IMPORTANCE_LOW).apply {

setSound(null,null)

}

//

))

}

}

服务开始前景:

val notification = NotificationCompat.Builder(this,MY_NOTIFICATION_CHANNEL)

.setSmallIcon(android.R.drawable.stat_sys_warning)

.setColor(App.context().resources.getColor(R.color.primary_dark))

.setContentText(if (/*logic*/) "This" else "That")

.addAction(0,if (enable) "Disable" else "Enable",firstIntent)

.addAction(0,"Another action",secondIntent)

.build()

startForeground(MY_NOTIFICATION_ID,notification)

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐