Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to adjust notification sound volume dynamically in Android O and above?

Tags:

android

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

enter image description here

However, I notice some apps in the market, do provide way to adjust notification sound volume dynamically.

enter image description here

May I know, what is the way to achieve so?

like image 336
Cheok Yan Cheng Avatar asked Dec 24 '18 07:12

Cheok Yan Cheng


People also ask

How do I control volume on notifications?

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.

Why does my volume go down when I get a notification Android?

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.


2 Answers

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 sharedpreferences.

like image 158
0xA Avatar answered Sep 30 '22 11:09

0xA


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!

like image 45
Vanshaj Daga Avatar answered Oct 04 '22 11:10

Vanshaj Daga