I am struggling with this problem for a banch days and cannot find proper way to do it.
I would like to set default channel settings (like sound on, lights on, vibration, lock screen notification etc.)
When I create a channel (already tried with different channel id and different package names) I always get channel with only vibration on - rest of stuff is off.
I try to create channel with this code (changing importance value makes no change in new channels):
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)
}
}
}
I know that once channel is created the app cannot change its settings - thats why I already have tried with different ids and pacakge names.
I also have tried with application example from (Google Codelabs Notification Channels and Badges) but with the same results.
I already noticed that in some others phones everythig is ok, with Importance.HIGHT - all of switch are turned on - but not on my device. When I install apps like Whatsapp or Viber, they channels have all settings already on - so I guess it is possible to do automatically.
I know I can always add button to open channel settings in my app, but it will be better to do it automatically when channel is registered.
Thanks in advance! :)