0

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.

Web.11
  • 406
  • 2
  • 8
  • 23
  • Have you tried uninstalling old app and install again? – Kamal Jun 03 '22 at 11:04
  • If you uninstall the app you lose also the settings. I have option sound 1 and sound 2, and users choose which sound want for notifications. – Web.11 Jun 03 '22 at 11:10
  • okay, then how about different notification channels https://stackoverflow.com/questions/53913251/different-notification-sound-not-working-in-oreo – Kamal Jun 03 '22 at 11:19
  • As you can read I do not want to create multiple channels for each settings, because the list will be big, and also it will not looks good on app settings and I have to add to much channels ( ex. audio + vibration, audio + no vibration, no audio + vibration, no audio no vibration and much more ).. So logic is to change settings without creating multiple channels, it must be a way :( – Web.11 Jun 03 '22 at 12:01

1 Answers1

0

Resolved by setting up a silent notification and adding manual sound and vibration.

So on When I create Channel I use:

notificationChannel.enableVibration(false);
notificationChannel.setSound(null,null);

Also when I build notification I add this code:

.setSilent(true)

Than to vibrate I use this:

private void vibrate (Context context){
        Vibrator v = (Vibrator) context.getSystemService(VIBRATOR_SERVICE);
        // Vibrate for 500 milliseconds
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
        } else {
            //deprecated in API 26
            v.vibrate(500);
        }
}

and for notification ringtone I use this code:

private void playSound(Context context, String SoundUri){
    Uri rawPathUri = Uri.parse(SoundUri);
    Ringtone r = RingtoneManager.getRingtone(context, rawPathUri);
    r.play();
}

When SoundUri is the String that matches the sound from settings user have choosen.

Web.11
  • 406
  • 2
  • 8
  • 23