android 跳转到应用通知设置界面(Android 8.0以上)

boolean areNotificationsEnabled = mNotificationManagerCompat.areNotificationsEnabled();

        if (!areNotificationsEnabled) {
            // Because the user took an action to create a notification, we create a prompt to let
            // the user re-enable notifications for this application again.
            Snackbar snackbar = Snackbar
                    .make(
                            mMainRelativeLayout,
                            "You need to enable notifications for this app",
                            Snackbar.LENGTH_LONG)
                    .setAction("ENABLE", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            // Links to this app's notification settings
                            openNotificationSettingsForApp();
                        }
                    });
            snackbar.show();
            return;
        }

首先通过areNotificationsEnabled查询应用是否有通知权限,如果没有则执行openNotificationSettingsForApp()方法。

private void openNotificationSettingsForApp() {
     // Links to this app's notification settings.
     Intent intent = new Intent();
     intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
     //这种方案适用于 API 26, 即8.0(含8.0)以上可以用
     intent.putExtra(Settings.EXTRA_APP_PACKAGE, getApplicationContext().getPackageName());
     intent.putExtra(Settings.EXTRA_CHANNEL_ID, getApplicationInfo().uid);
     //这种方案适用于 API21——25,即 5.0——7.1 之间的版本可以使用
     intent.putExtra("app_package", pkg);
     intent.putExtra("app_uid", uid);
     startActivity(intent);
 }

通过intent启动系统应用的通知管理界面。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值