Android 4.1: How to check notifications are disabled for the application?

AndroidAndroid NotificationsAndroid Notification-Bar

Android Problem Overview


Android 4.1 offers the user a check box to disable notifications for a specific application.

However, as a developer we have no way to know whether a call to notify was effective or not.

I really need to check if the notifications are disabled for the current application but I can't find any setting for that in the API.

Is there ever a way to check this setting in the code?

Android Solutions


Solution 1 - Android

You can't 100% can't.

It is asked in this Google I/O 2012 video and the Project lead for the new notifications declares that you can't.


Edit

2016 update: Now you can check it, as said in this Google I/O 2016 video.

Use NotificationManagerCompat.areNotificationsEnabled(), from support library, to check if notifications are blocked on API 19+. The versions below API 19 will return true (notifications are enabled).

enter image description here

Solution 2 - Android

Answer from @blundell is correct but there is a minor change in newer versions.

NotificationManagerCompat.from(context).areNotificationsEnabled()

Solution 3 - Android

Actually this is pretty easy to do:

/**
 * Created by desgraci on 5/7/15.
*/
public class NotificationsUtils {

    private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
    private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";

    public static boolean isNotificationEnabled(Context context) {

        AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);

        ApplicationInfo appInfo = context.getApplicationInfo();

        String pkg = context.getApplicationContext().getPackageName();

        int uid = appInfo.uid;

        Class appOpsClass = null; /* Context.APP_OPS_MANAGER */

        try {

            appOpsClass = Class.forName(AppOpsManager.class.getName());

            Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);

            Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
            int value = (int)opPostNotificationValue.get(Integer.class);

            return ((int)checkOpNoThrowMethod.invoke(mAppOps,value, uid, pkg) == AppOpsManager.MODE_ALLOWED);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return false;
    }
}

Solution 4 - Android

If you are using Xamarin and you need this answer you can use this code:

//return true if this option is not supported.
public class NotificationsUtils 
{
	private const String CHECK_OP_NO_THROW = "checkOpNoThrow";
	private const String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";

	public static bool IsNotificationEnabled(global::Android.Content.Context context) {
		
		AppOpsManager mAppOps = (AppOpsManager) context.GetSystemService(global::Android.Content.Context.AppOpsService);

		ApplicationInfo appInfo = context.ApplicationInfo;

		String pkg = context.ApplicationContext.PackageName;

		int uid = appInfo.Uid;

		try {

			var appOpsClass = Java.Lang.Class.ForName("android.app.AppOpsManager");
			var checkOpNoThrowMethod = appOpsClass.GetMethod(CHECK_OP_NO_THROW,Java.Lang.Integer.Type,Java.Lang.Integer.Type,new Java.Lang.String().Class);//need to add String.Type

			var opPostNotificationValue = appOpsClass.GetDeclaredField (OP_POST_NOTIFICATION);
			var value = (int)opPostNotificationValue.GetInt(Java.Lang.Integer.Type);
			var mode = (int)checkOpNoThrowMethod.Invoke(mAppOps,value, uid, pkg);
			return (mode == (int)AppOpsManagerMode.Allowed);

		} catch (Exception) 
		{
			System.Diagnostics.Debug.WriteLine  ("Notification services is off or not supported");
		} 
		return true;
	}
}

Solution 5 - Android

It seems like there is no way to query notification state.

I recommend this:

  • Design you application with notifications.
  • Let user to disable notifications from application's settings.
  • Check whether notifications are clicked. If user clicks notification, save this to preferences.
  • In your app, if notification setting is on, and if user is Android 4.1+ (API 16), but if user doesn't click notification for some days / weeks, assume that user disabled notifications.

Not 100% correct. But this gives an opinion.
For example if user doesn't click any app notification for 10-15 days, probably he disabled it

Solution 6 - Android

I use this method to check whether the notifications are enabled or not, the above-mentioned methods will work for checking whether notifications enabled or not. But from Android 8 onwards for creating notifications we have to create a channel first, so from Oreo, we have to check for your notification channel enabled or not.

    /**
     * Checking Whether notifications are enabled or not
     * @return true if notifications are enabled otherwise false
     */
    public static final String CHANNEL_ID = "your_channel_id";

    private boolean isNotificationChannelEnabled(){
        if(NotificationManagerCompat.from(this).areNotificationsEnabled()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                NotificationChannel channel = manager.getNotificationChannel(CHANNEL_ID);
                if (channel == null)
                    return true; //channel is not yet created so return boolean
                // by only checking whether notifications enabled or not
                return channel.getImportance() != NotificationManager.IMPORTANCE_NONE;
            }
            return true;
        }
        return false;
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGuillaume PerrotView Question on Stackoverflow
Solution 1 - AndroidBlundellView Answer on Stackoverflow
Solution 2 - AndroidPrakashView Answer on Stackoverflow
Solution 3 - AndroiddesgraciView Answer on Stackoverflow
Solution 4 - Androiduser3030630View Answer on Stackoverflow
Solution 5 - AndroidtranteView Answer on Stackoverflow
Solution 6 - Androidsai Pavan KumarView Answer on Stackoverflow