Skip to content

Instantly share code, notes, and snippets.

@pbakondy
Created July 20, 2015 10:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pbakondy/26a9985f36201bfedbc9 to your computer and use it in GitHub Desktop.
Save pbakondy/26a9985f36201bfedbc9 to your computer and use it in GitHub Desktop.
Cordova - Push Notifications - Android - How to check notifications are disabled for the application?
// Cordova - Push Notifications - Android
// How to check notifications are disabled for the application?
// Works on Andoid 4.4+ (KitKat)
// Otherwise returns 'true'
// original:
// http://stackoverflow.com/questions/11649151/
public class NotificationsUtils {
private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
@TargetApi(Build.VERSION_CODES.KITKAT)
public boolean isNotificationEnabled() {
Context context = this.cordova.getActivity();
ApplicationInfo appInfo = context.getApplicationInfo();
String pkg = context.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Object mAppOps = null;
Class appOpsClass = null;
try {
mAppOps = context.getSystemService(Context.APP_OPS_SERVICE);
appOpsClass = Class.forName(AppOpsManager.class.getName());
// java.lang.reflect.Method
Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
// java.lang.reflect.Field
Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (Integer)opPostNotificationValue.get(Integer.class);
int mode = (Integer)checkOpNoThrowMethod.invoke((AppOpsManager)mAppOps, value, uid, pkg)
return (mode == AppOpsManager.MODE_ALLOWED);
} catch (NoClassDefFoundError e) {
e.printStackTrace();
} 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 true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment