Не меняется NotificationalChannel Android
Мне нужно сделать возможность отключения звука / вибро у пушей в приложении.
Но как я понял, что как только ты создал канал, ты его уже не можешь редактировать.
Есть варианты изменять канал?
private fun generateNotification(
context: Context,
title: String?,
message: String?,
type: String?,
sound: Boolean,
data: String?,
order: Boolean,
vibro: Boolean
){
val random = Random()
var m = random.nextInt(9999 - 1000) + 1000
if (order) {
data?.let {
m = it.toInt()
}
}
val alarmSound = when(type){
null -> RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
"4" -> Uri.parse("android.resource://" + context.packageName + "/" + R.raw.info)
"3" -> Uri.parse("android.resource://" + context.packageName + "/" + R.raw.coin)
else -> RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
}
if(type == null)
Log.d("Type:", "null")
else
Log.d("Type", type)
val notificationChannelId = context.getString(R.string.default_notification_channel_id) + "107"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
var mChannel = NotificationManagerCompat.from(this).getNotificationChannel(notificationChannelId)
if(mChannel != null) {
Log.d("Channel", "удален")
NotificationManagerCompat.from(this).deleteNotificationChannel(notificationChannelId)
}
else{
Log.d("Channel", "создан")
}
mChannel = NotificationChannel(notificationChannelId, notificationChannelName, importance)
val att = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build()
if(sound) {
mChannel.setSound(alarmSound, att)
Log.d("Sound", "true")
}
else {
mChannel.setSound(null, null)
Log.d("Sound", "false")
}
if(vibro) {
Log.d("Vibro", "true")
mChannel.enableVibration(true)
mChannel.vibrationPattern = longArrayOf(0, 400, 300, 400)
}
else {
Log.d("Vibro", "false")
mChannel.enableVibration(false)
mChannel.vibrationPattern = longArrayOf(0)
}
NotificationManagerCompat.from(this).createNotificationChannel(mChannel)
}
val appCtx = applicationContext
val notificationIntent = Intent(context, MainActivity::class.java)
// notificationIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
notificationIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
notificationIntent.putExtra("message", data)
notificationIntent.putExtra("action", type)
appCtx.sendBroadcast(notificationIntent)
val intent = PendingIntent.getActivity(
context,
m,
notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT
)
val builder: NotificationCompat.Builder = NotificationCompat.Builder(context, notificationChannelId)
.setContentTitle(title)
.setContentText(message)
.setStyle(NotificationCompat.BigTextStyle().bigText(message))
.setSmallIcon(R.drawable.ic_push)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(intent)
.setAutoCancel(true)
if(sound)
builder.setSound(alarmSound)
else
builder.setSound(null)
if(vibro)
builder.setVibrate(longArrayOf(0, 400, 300, 400))
else
builder.setVibrate(longArrayOf(0))
NotificationManagerCompat.from(this).notify(m, builder.build())
}
Update:
Я попробовал удалять канал и создавать под тем же Id, но не работает (=