获取App通知权限是否开启的方法

我们有时候需要获知用户是否允许了App在通知栏显示通知,设置入口一般见于AppInfo即应用详情页面。
方法来自于官方的support包,必须先更新build.gradle中的support包版本到24以上:

compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:support-annotations:24.0.0'
compile 'com.android.support:support-v4:24.0.0'

鄙人的项目里只用到了这3个support包,如果大家有其余的,都要改成24以上。
然后再在代码中通过NotificationManagerCompat包获取是否打开了通知显示权限:

NotificationManagerCompat manager = NotificationManagerCompat.from(App.getInstance().getContext());
boolean isOpened = manager.areNotificationsEnabled();

要注意的是,areNotificationsEnabled方法的有效性官方只最低支持到API 19,低于19的仍可调用此方法不过只会返回true,即默认为用户已经开启了通知。
查了各种资料,目前暂时没有办法获取19以下的系统是否开启了某个App的通知显示权限。

最后,附上打开应用详情页面的代码:

// 根据isOpened结果,判断是否需要提醒用户跳转AppInfo页面,去打开App通知权限
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", App.getInstance().getApplication().getPackageName(), null);
intent.setData(uri);
startActivity(intent);

猜你喜欢

转载自blog.csdn.net/ysy950803/article/details/71910806
今日推荐