I am trying to programmatically find out for which apps the Do Not Disturb setting is bypassed exceptionally.
So far, I am using the following code to check whether the phone is set in Do not Disturb mode or not :
public static boolean isDnDModeEnabled(Context context)
{
if(Build.VERSION.SDK_INT <23)
return false;
try {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int filterValue = notificationManager.getCurrentInterruptionFilter();
switch(filterValue)
{
case NotificationManager.INTERRUPTION_FILTER_ALL : Log.d("DND","Interruption filter all");
break;
case NotificationManager.INTERRUPTION_FILTER_ALARMS : Log.d("DND","Interruption filter alarms");
break;
case NotificationManager.INTERRUPTION_FILTER_PRIORITY : Log.d("DND","Interruption filter priority");
break;
case NotificationManager.INTERRUPTION_FILTER_UNKNOWN : Log.d("DND","Interruption filter unknown");
break;
case NotificationManager.INTERRUPTION_FILTER_NONE : Log.d("DND","Interruption filter none");
break;
}
if(filterValue == NotificationManager.INTERRUPTION_FILTER_ALL)
return false;
else if(filterValue == NotificationManager.INTERRUPTION_FILTER_PRIORITY)
{
//Logic based on which apps are allowed as priority
return true; //or false
}
else
return true;
}
catch(Exception e)
{
return false;
}
}
When I click on the Priority app notiications tab, I get a list of all installed apps for which I get to choose which apps to allow as priority exceptions.
My question is how to programmatically get the list of apps which are allowed as priority exceptions for Do Not Disturb mode, and thereby define the logic replacing the comment in the above code? Any solutions would be thoroughly appreciated.
The Android settings vary by version and device, but you can usually get to the Do Not Disturb controls by swiping down from the top of the screen to the Quick Settings box. Tap the Do Not Disturb icon, and then tap More Settings. Select the Priority Only Allows option, and on the next screen tap Calls.
Do Not Disturb Mode Settings. You can access these settings by clicking Menu ▸ Settings, then navigating to Performance ▸ Do Not Disturb Mode. Clicking Don't block notifications from these apps will open the list of apps that are currently not being blocked; additional apps can be added to the list by clicking Add app.
You're looking for a way to determine which apps on the system have a notification importance of Notification.IMPORTANCE_MAX
and, sorry to say but this is not possible by a non-system app. You need access to the INotificationService
so you can call getImportance(packageName)
. See the Notification Manager source but it's guarded by a check that ensures you're a system app or the app whose package you passed so reflection is out...
Google allows an app to obtain its own notification importance through the NotificationManager
with getImportance()
(see docs) but you can't call it with an arbitrary package.
The other answer here references checking out the source from the system settings app and that's exactly what I did and, after a while of tracing through the code, I discovered how they determine which apps should show up in the "Overrides Do Not Disturb" menu by this code here which led me down the path of discovering how we could determine the IMPORTANCE_*
Sorry man, but the suggestions made by the other answerer are also not going to work because they are either incorrect (packages.xml doesn't have the info) or are going to require root which isn't reliable on all devices.
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