前言:在做推送的时候,App应用中的消息推送开关已经打开了可是还是收不到推送。以为是权限问题,于是乎查看了一下与Notification相关的权限。诶,诶,诶,没有。但是可以检测系统通知的开关。在这里记录一下
public class NotificationUtil {
private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static boolean isNotificationEnabled(Context context) {
AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
ApplicationInfo appInfo = context.getApplicationInfo();
String packageName = context.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Class<?> appOpsClass = null;
try {
appOpsClass = Class.forName(AppOpsManager.class.getName());
Method method = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
Field notificationFieldValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (int) notificationFieldValue.get(Integer.class);
return ((int) method.invoke(appOps, value, uid, packageName) == AppOpsManager.MODE_ALLOWED);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return false;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static void checkNotificationEnable(final Context context) {
if (!isNotificationEnabled(context)) {
new AlertDialog(context).builder()
.setMsg("检测到您还没有打开通知是否去打开")
.setPositiveButton("去打开", new View.OnClickListener() {
@Override
public void onClick(View v) {
toSetting(context);
}
}).setNegativeButton("暂不打开", null);
}
}
private static void toSetting(Context context) {
Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
context.startActivity(localIntent);
}
}
因为我的项目是最低兼容API16的所以没有适配API8以下的跳转系统设置问题。如果有需要的话可以使用以下的代码:
private static void toSetting(Context context) {
NotificationManagerCompat.from(context).areNotificationsEnabled();
Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 9) {
localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
} else if (Build.VERSION.SDK_INT <= 8) {
localIntent.setAction(Intent.ACTION_VIEW);
localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
}
context.startActivity(localIntent);
}
注意:
1,兼容问题:在API19以下是没有AppOpsManager这个类的,所以只能适用于API19以上
2,除了可以使用反射还可以使用官方推荐的方法support v4包:
NotificationManagerCompat.from(context).areNotificationsEnabled();
在API19以下的版本默认返回true。
3,代码中的AlertDialog是本地自定义的所以有setMsg();方法
如有不足的地方欢迎指正