引导用户开启通知权限

在做极光推送的时候,从log看是可以确定收到消息推送的, 但是在状态栏以及下拉通知栏中都没有消息通知,其实是因为没有开启通知权限,但是坑的是通知权限不像其他网络或sd卡读写权限一样可以去申请,然后在用户同意的情况下,获取到权限,通知权限必须是用户自己手动点开,这样的体验不是很好,但是也没有什么办法。虽然不能申请,但是可以检测通知是否开启

/**
 * 判断是否开启通知权限
 * @param context
 * @return
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static boolean isNotificationEnabled(Context context) {
    String CHECK_OP_NO_THROW = "checkOpNoThrow";
    String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";

    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 = (Integer) opPostNotificationValue.get(Integer.class);
        return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

在用户没有开启通知的情况下引导用户到设置中打开允许通知。不过值得注意的是在打开通知权限的时候,也提醒用户打开锁屏通知,不然在锁屏的状态下,仍然没有提示比如小米,但有些手机本身就是没有锁屏通知的,比如联想的ZUK

if (!Util.isNotificationEnabled(this)) {
    AlertDialog notificationDialog = new AlertDialog.Builder(this)
            .setTitle("友情提示").setMessage(R.string.notification)
            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    PrefUtil.set(Global.getUser().getUserId()+Constants.notification_note, 1);
                }
            }).setPositiveButton("设置", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //跳转设置界面
                    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", 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", getPackageName());
                    }
                    startActivity(localIntent);
                }
            })
            .setCancelable(false)
            .create();
    notificationDialog.setCanceledOnTouchOutside(true);
    notificationDialog.show();
}

大部分手机通知开光是默认开启的,除非用户给手动关闭了,但还是有一些奇葩手机通知是默认关闭的,比如联想zuk……Android8.0,也就是targetSdkVersion指定到了26或者更高,Notification需要做适配,请参考https://blog.csdn.net/guolin_blog/article/details/79854070

    原文作者:reglog
    原文地址: https://blog.csdn.net/reglog/article/details/79863751
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞