I'm trying to create an app with 3 different notification sounds (But not different notification channels), I create custom notification sound, but when I try to change the sound, it stays still the same, I also tried to delete the channel and create it again but notification sound does not change.
First I create Channel:
String CHANNEL_ID = Constants.notification_channel_ID;
int NOTIFICATION_ID = 1991;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = getSystemService(NotificationManager.class);
NotificationChannel existingChannel = mNotificationManager.getNotificationChannel(CHANNEL_ID);
if (existingChannel != null){
mNotificationManager.deleteNotificationChannel(CHANNEL_ID);
}
CharSequence name = getResources().getString(R.string.drink_reminder);
String description = getResources().getString(R.string.notification_to_remind_you_to_drink);
int importance = NotificationManager.IMPORTANCE_HIGH;
if (s1 = true){
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test_sound);
}else{
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test_sound1);
}
AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION ).setUsage(AudioAttributes.USAGE_NOTIFICATION).build() ;
NotificationChannel notificationChannel = new NotificationChannel( CHANNEL_ID , name , importance) ;
notificationChannel.setDescription(description);
notificationChannel.enableLights(true) ;
notificationChannel.setLightColor(Color.BLUE) ;
notificationChannel.enableVibration( tinyDB.getBoolean(Constants.settings_notification_vibration_key,true));
notificationChannel.setVibrationPattern( new long []{ 100 , 200 , 300 , 400 , 500 , 400 , 300 , 200 , 400 }) ;
notificationChannel.setSound(sound , audioAttributes) ;
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(notificationChannel);
}
Then I create Notifiaction:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(this.getResources().getString(R.string.app_name))
.setContentText(this.getResources().getString(R.string.text))
.setContentIntent(pendingIntent)
.setSound(sound)
.setChannelId(CHANNEL_ID)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
So on first time this code works, when later on I call this method again to and change the sound, notifications are still working with same sound like in beginning, also I can't disable, enable vibration (It stays the same like first time is configured). As you see I'm also trying to recreate notification channel, but still the same. Any help will be appriciated.