I fully understand since Android O and above, there's no easy way to customize notification sound through app's code.
The common way to do so, is to invoke Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS
private void showNotificationSoundListPreferenceDialogFragmentCompat(Preference preference) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getContext().getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, com.yocto.wenote.reminder.Utils.createNotificationChannel());
try {
startActivity(intent);
} catch (android.content.ActivityNotFoundException e) {
Log.e(TAG, "", e);
trackEvent("showNotificationSoundListPreferenceDialogFragmentCompat", "fatal", e.getMessage());
}
}
}
It looks like following
However, I notice some apps in the market, do provide way to adjust notification sound volume dynamically.
May I know, what is the way to achieve so?
Go to Settings. Depending on your phone's Android version, do one of the following: Tap Sound > Advanced > Default notification sound. Tap Sound & notification > Notification sound.
When the notifications come, 1) if you do not enable "Pause for interruptions" feature in the app Settings, the sound will be lowered, thus you may miss something. 2) if you enable "Pause for interruptions" in the app Settings, the player will pause and continue after the notifications.
This is merely a hack, a way to get it to do so, and it will achieve your required behavior. In my application, targeting API 26; i have implemented a sound and vibration customization but manually.
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "Channel1");
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Vibrations
Vibrator vib = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
vib.vibrate(VibrationEffect.createWaveform(new long[] {200,300,500,100,100}, 1));
//Sound
Intent soundIntent = new Intent(this, PlaySound.class);
serviceIntent.setAction("ACTION_START_PLAYBACK");
serviceIntent.putExtra("UriSound", soundUri.toString());
context.startForegroundService(soundIntent);
// if notification is outted, just delete notification; thus delete intent
Intent outNotification = new Intent(context, PlaySound.class);
deleteIntent.setAction("ACTION_STOP_PLAYBACK");
PendingIntent pendingOutNotification =
PendingIntent.getService(this, 0, outNotification, 0);
builder.setDeleteIntent(pendingOutNotification);
} else {
builder.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
builder.setSound(uri);
}
notificationManager.notify(0, builder.build());
In your PlaySound.class extend service
, Play your sound on a media player
, and control volume or the particular sound based on the intent
of that particular channel
...
Target the volume or the uri based on the users input from the slider, and then send via intent
or save it in a key-value pair using sharedpreference
s.
You can achieve this by following steps Check for appropriate permission
isNotificationPolicyAccessGranted()
returns boolean and is a part of NotificationManager. It is required Android N onwards as it can mend "do not disturb" If not provided ask for the permission explicitly
Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS
set up AudioManager in your activity create a custom view with slider to fetch an int between getStreamMinVolume(5) and getStreamMaxVolume(5)
then set desired volume with
setStreamVolume(5, yourdesiredvalue in int , 0)
I hope it will help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With