判断App通知是否被屏蔽,弹窗提醒并提供跳转到对应开启通知页面的按钮

需求:

1、全新、升级安装后判断系统是否屏蔽App的通知。

2、如果通知被屏蔽,弹出提醒开启通知的弹窗。

3、点击弹窗中“立即开启”按钮,跳转到系统对应的此App的通知设置界面。

解决方法:

1、判断通知App的通知功能是否被屏蔽。代码如下:

NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
boolean areNotificationsEnabled = notificationManagerCompat.areNotificationsEnabled();

备注:此方法只针对Android4.3以及以上

2、跳转到对应的通知设置页面。代码如下:

public static void goSystemNotificationSetting(Context context) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            Uri uri = Uri.parse("package:" + context.getPackageName());
            Intent intentSet = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri);
            context.startActivity(intentSet);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            Intent intent = new Intent();
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("app_package", context.getPackageName());
            intent.putExtra("app_uid", context.getApplicationInfo().uid);
            context.startActivity(intent);
        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {

            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.setData(Uri.parse("package:" + context.getPackageName()));
            context.startActivity(intent);
        } else {

            Intent localIntent = new Intent();
            localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
            context.startActivity(localIntent);
        }
    }

 

猜你喜欢

转载自blog.csdn.net/nsacer/article/details/80350735
今日推荐