【发布时间】:2018-07-26 06:43:14
【问题描述】:
每次我在前台服务中使用startForeground() 在通知中更新状态时,我都遇到了三星 Galaxy S7 Edge 显示弹出通知的问题。
首先,所有 Android 8.0 设备都存在此问题,但通过将通知通道优先级设置为 IMPORTANCE_LOW 很容易解决。但是三星设备的问题仍然存在。
那么,问题是,如何在三星 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)
【问题讨论】:
-
为什么要多次使用startForeground? android 多次显示您的通知是有意义的。您可以在尚未启动时显示 (start)。 startForeground 将始终显示通知。
-
@AdibFaramarzi 我已经更改了代码以检查是否已经有正在进行的通知并使用
NotificationManager来更新通知而不是另一个startForeground()。不过没有区别。
标签: android android-notifications android-8.0-oreo