获取 通知权限 状态
//获取通知权限状态
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
boolean isOpened = manager.areNotificationsEnabled();//false=未开启,true=开启
开启 通知权限
//开启 通知权限
try {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//这种方案适用于 API 26, 即8.0(含8.0)以上可以用
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, CHANNEL_ID);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) {
//这种方案适用于 API21-25,即 5.0-7.1 之间的版本可以使用
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", CHANNEL_ID);
}
startActivity(intent);
} catch (Exception e) {
// 出现异常则跳转到应用设置界面
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getApplication().getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
网友评论