简体   繁体   English

android,如何监听通知权限变更事件

[英]android, how to listen to the notification permission change event

on android app could open the notification permission by在 android 应用程序可以通过以下方式打开通知权限

Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
        .putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
startActivity(intent);

and turn it on or off.并将其打开或关闭。 在此处输入图像描述

how to listen to permission change happened here?如何收听这里发生的权限更改? will it be broadcasted?会播出吗?

EDIT: >28 seems there is broadcast, but no solution found yet for api before that.编辑:> 28 似乎有广播,但在此之前尚未找到 api 的解决方案。 https://developer.android.com/reference/android/app/NotificationManager#ACTION_APP_BLOCK_STATE_CHANGED https://developer.android.com/reference/android/app/NotificationManager#ACTION_APP_BLOCK_STATE_CHANGED

ACTION_APP_BLOCK_STATE_CHANGED
Added in API level 28

public static final String ACTION_APP_BLOCK_STATE_CHANGED
Intent that is broadcast when an application is blocked or unblocked. This broadcast is only sent to the app whose block state has changed. 

I don't believe it is possible to be notified, but I'd love a correction if I'm wrong.我不相信有可能得到通知,但如果我错了,我希望得到更正。 That said, a user could also change this at any time, not just when you fire your Intent.也就是说,用户也可以随时更改此设置,而不仅仅是在您触发 Intent 时。 While areNotificationsEnabled() will help here, unfortunately you might also need to cover the case of notification channels in newer APIs as well as notification channel groups.虽然 areNotificationsEnabled() 在这里会有所帮助,但不幸的是,您可能还需要涵盖较新 API 中的通知通道以及通知通道组的情况。

I think it is not possible.我认为这是不可能的。 If the user grants permission for the app, the only way that you find out is if you call checkSelfPermission() again.如果用户授予该应用程序的权限,您发现的唯一方法是再次调用checkSelfPermission()

If the user revokes a previously-granted permission, your process is terminated, and you would find out about the permission change by calling checkSelfPermission() the next time your app runs.如果用户撤销了先前授予的权限,您的进程将终止,您将在下次运行应用程序时调用 checkSelfPermission() 来了解权限更改。 So I think every time you start your app, call checkSelfPermission() to find out if there were any changes.所以我认为每次你启动你的应用程序时,调用 checkSelfPermission() 来找出是否有任何变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM