Android 检测系统通知栏开关是否打开及跳转

前言:在做推送的时候,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();方法

如有不足的地方欢迎指正

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值