1

enter image description here

How can I toggle these switches programmatically? I have tried various things such as :

private void allowAlarms() {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
assert notificationManager != null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    notificationManager.setNotificationPolicy(
            new NotificationManager.Policy(NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS,
                    NotificationManager.Policy.PRIORITY_SENDERS_ANY,
                    NotificationManager.Policy.PRIORITY_SENDERS_ANY));
}
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY);

}

and even simply unmuting the phone:

    private void allowAlarms() {
    final AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    assert am != null;
    am.adjustVolume(AudioManager.STREAM_ALARM, AudioManager.ADJUST_UNMUTE);
    System.out.println("Alarms ON");
}

Nothing works, meaning that the alarm sound doesn't get toggled on/turned on. Thanks for any help!

Edit: just to confirm that ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED is true.

One more question: if I looked into Google source code to identify how they do it, any ideas roughly where to look?

Code Poet
  • 6,222
  • 2
  • 29
  • 50

1 Answers1

0

the official and most up-to-date api to manage android notification system comes with android 8 (api level 26)

You can check the following topic that discusses this in detail:

https://stackoverflow.com/a/50813781/1345973

The new api gives a better control over the notification system and how it could be tuned at application level.

Official Android Doc: https://developer.android.com/training/notify-user/channels

Using this api, you can override system configuration of do not disturb inside your app, but you can not change the overall settings that you are mentioning. Do may be this may not be considered as a solution to what you are asking.

Hichem BOUSSETTA
  • 1,791
  • 1
  • 21
  • 27