如何在> = Android O中设置通知渠道默认值

时间:2019-07-18 10:21:09

标签: android android-notifications android-8.0-oreo notification-channel

我一直在苦苦挣扎着解决这个问题,却找不到合适的解决方法。

我想设置默认的频道设置(例如声音打开,灯光打开,振动,锁定屏幕通知等)

当我创建一个频道时(已经尝试使用不同的频道ID和不同的包名称)时,我总是在仅振动的情况下获得频道-其余的东西都关闭了。

我尝试使用此代码创建频道(更改重要性值不会更改新频道):

object DefaultNotificationChannel {

@RequiresApi(Build.VERSION_CODES.O)
fun createChannel(applicationContext: Context) {
    val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + applicationContext.packageName + "/" + R.raw.notification)

    createNotificationChannel(applicationContext, notificationManager, sound)
}

@TargetApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(applicationContext: Context, notificationManager: NotificationManager, sound: Uri) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = applicationContext.getString(R.string.notification_channel_name)
        val id = applicationContext.getString(R.string.default_notification_channel_id)

        val importance = NotificationManager.IMPORTANCE_HIGH
        val channel = NotificationChannel(id, name, importance)
        channel.enableLights(true)
        channel.lightColor = Color.RED
        channel.enableVibration(true)
        val attributes = AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build()
        channel.setSound(sound, attributes)
        channel.enableVibration(true)
        channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC

        notificationManager.createNotificationChannel(channel)
    }
}

}

我知道,一旦创建频道,应用程序就无法更改其设置-这就是为什么我已经尝试使用不同的ID和名称的原因。

我还尝试了(Google Codelabs Notification Channels and Badges)中的应用示例,但结果相同。

我已经注意到,在其他一些电话中,每一个都可以,使用Importance.HIGHT可以-所有开关都打开-但不能在我的设备上。 当我安装Whatsapp或Viber之类的应用程序时,它们的频道已经启用了所有设置-因此我想可以自动进行设置。

我知道我可以随时添加按钮以在我的应用程序中打开频道设置,但是最好在注册频道后自动进行设置。

提前谢谢! :)

Default channel settings on device

0 个答案:

没有答案
相关问题