美文网首页Flutter学习
Android 判断当前应用是否开启消息通知

Android 判断当前应用是否开启消息通知

作者: 雪域红鹰 | 来源:发表于2022-05-05 15:40 被阅读0次

方式一

创建通知工具类


   public class NotificationsUtils {

    //判断是否需要打开设置界面
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static void OpenNotificationSetting(Context context, OnNotificationLitener mOnLitener) {
        if (!isNotificationEnabled(context)) {
            showSettingDialog(context);
        } else {
            if (mOnLitener != null) {
                mOnLitener.onNext();
            }
        }
    }

    /**
     * 判断该app是否打开了通知
     * 可以通过NotificationManagerCompat 中的 areNotificationsEnabled()来判断是否开启通知权限。NotificationManagerCompat 在 android.support.v4.app包中,是API 22.1.0 中加入的。而 areNotificationsEnabled()则是在 API 24.1.0之后加入的。
     * areNotificationsEnabled 只对 API 19 及以上版本有效,低于API 19 会一直返回true
     * */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static boolean isNotificationEnabled(Context context) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
            boolean areNotificationsEnabled = notificationManagerCompat.areNotificationsEnabled();
            return areNotificationsEnabled;
        }

        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;

    }

    /**
     * 打开手机设置页面
     * 假设没有开启通知权限,点击之后就需要跳转到 APP的通知设置界面,对应的Action是:Settings.ACTION_APP_NOTIFICATION_SETTINGS, 这个Action是 API 26 后增加的
     * 如果在部分手机中无法精确的跳转到 APP对应的通知设置界面,那么我们就考虑直接跳转到 APP信息界面,对应的Action是:Settings.ACTION_APPLICATION_DETAILS_SETTINGS*/
    private static void gotoSet(Context context) {

        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= 26) {
            // android 8.0引导
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName());
        } else if (Build.VERSION.SDK_INT >= 21) {
            // android 5.0-7.0
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("app_package", context.getPackageName());
            intent.putExtra("app_uid", context.getApplicationInfo().uid);
        } else {
            // 其他
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /*=====================添加Listener回调================================*/
    public interface OnNotificationLitener {
        /**
         * 不需要设置通知的下一步
         */
        void onNext();
    }

    private OnNotificationLitener onNotificationLitener;

    public void setOnNextLitener(OnNotificationLitener mOnNextLitener) {
        this.onNotificationLitener = mOnNextLitener;
    }



    public static void showSettingDialog(final Context mContext) {
        //提示弹窗

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("打开推送通知");
        builder.setMessage("打开通知第一时间获取相关咨询");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加"Yes"按钮
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                gotoSet(mContext);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加取消
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
               alertDialog.cancel();
            }
        });
          AlertDialog alertDialog = builder.create();
          alertDialog.show();
      }
    }

调用方式

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //判断是否需要开启通知栏功能
            NotificationsUtils.OpenNotificationSetting(MainActivity.this, new NotificationsUtils.OnNotificationLitener() {
                @Override
                public void onNext() {
                    Toast.makeText(MainActivity.this,"已开启通知权限",Toast.LENGTH_SHORT).show();
                }
            });
        }

方式二

 private void checkNotifySetting() {
        NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        // areNotificationsEnabled方法的有效性官方只最低支持到API 19,低于19的仍可调用此方法不过只会返回true,即默认为用户已经开启了通知。
        boolean isOpened = manager.areNotificationsEnabled();

        if (isOpened) {
            LogUtil.v("TEST ", "通知权限已经被打开" +
                    "\n手机型号:" + android.os.Build.MODEL +
                    "\nSDK版本:" + android.os.Build.VERSION.SDK_INT +
                    "\n系统版本:" + android.os.Build.VERSION.RELEASE +
                    "\n软件包名:" + MainApplication.getAppCtx().getPackageName());
        } else {
            LogUtil.v("TEST ", "还没有开启通知权限,点击去开启");

            try {
                // 根据isOpened结果,判断是否需要提醒用户跳转AppInfo页面,去打开App通知权限
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                //这种方案适用于 API 26, 即8.0(含8.0)以上可以用
                intent.putExtra(EXTRA_APP_PACKAGE, MainApplication.getAppCtx().getPackageName());
                intent.putExtra(EXTRA_CHANNEL_ID, MainApplication.getAppCtx().getApplicationInfo().uid);

                //这种方案适用于 API21——25,即 5.0——7.1 之间的版本可以使用
                intent.putExtra("app_package", MainApplication.getAppCtx().getPackageName());
                intent.putExtra("app_uid", MainApplication.getAppCtx().getApplicationInfo().uid);
                startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
                // 出现异常则跳转到应用设置界面:锤子坚果3——OC105 API25
                Intent intent = new Intent();

                //下面这种方案是直接跳转到当前应用的设置界面。
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", MainApplication.getAppCtx().getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);
            }
        }
    }

相关文章

网友评论

    本文标题:Android 判断当前应用是否开启消息通知

    本文链接:https://www.haomeiwen.com/subject/ybczyrtx.html