1

我在这个问题上苦苦挣扎了好几天,找不到合适的方法来解决这个问题。

我想设置默认频道设置(如声音开、灯开、振动、锁屏通知等)

当我创建一个频道时(已经尝试过使用不同的频道 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 和 pacakge 名称。

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

我已经注意到在其他一些手机中一切正常,Importance.HIGHT - 所有开关都打开 - 但在我的设备上没有。当我安装诸如 Whatsapp 或 Viber 之类的应用程序时,他们的频道已经开启了所有设置 - 所以我想它可以自动完成。

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

提前致谢!:)

设备上的默认频道设置

4

0 回答 0