-
-
Save chonamdoo/3a1410ab651a932718d45d4287fccb78 to your computer and use it in GitHub Desktop.
Cordova - Push Notifications - Android - How to check notifications are disabled for the application?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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