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

☆樱花仙子☆ 提交于 2019-11-25 18:55:04
Blundell

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

desgraci

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;     } } 

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

NotificationManagerCompat.from(context).areNotificationsEnabled(‌​) 

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;     } } 
trante

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!