android - startForeground() 总是在 Samsung 8.0 设备上显示弹出窗口

标签 android android-notifications android-8.0-oreo

每次我在前台服务中使用 startForeground() 以在其通知中更新状态时,我都遇到了 Samsung Galaxy S7 Edge 显示弹出通知的问题。

首先,这个问题出现在所有 Android 8.0 设备上,但通过将我的通知 channel 优先级设置为 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)

最佳答案

我一直在调查同样的事情。您能做的最好的事情就是创建没有 channel 的通知。服务从托盘中没有任何内容开始。似乎对我有用。

NotificationCompat.Builder(this)

但是,此构造函数在 26.1.0 中已弃用

关于android - startForeground() 总是在 Samsung 8.0 设备上显示弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51532532/

相关文章:

包 com.twilio.sdk 的 java.lang.ClassNotFoundException

android-8.0-oreo - 由于 bg 限制,不允许 Service.startForeground()

android - 如何使用 adb shell 命令验证 android 设备屏幕打开或关闭

java - 跨多个 Activity 的 Android 数据库

android - 在CursorLoader中通过 'selection'参数过滤ListView

java - 如何在Toast消息框中显示string[]值?

android - 警报管理器不显示通知 api 26

android - 如何查找通知 ID?

android - 通知访问安卓?如何使用它?

android - 对 SEND 操作 Intent 的权限拒绝