在Android设置中,用户可以关闭通知,如果他们不想。那么有没有像isNotificationAllowed()
检查的任何方法是我的应用程序被允许显示通知?以及如何打开android设置页面来指导我的用户打开通知?如何检查我的应用是否被允许显示通知
回答
编辑 - 新建答案:
好像谷歌加入适当的API调用: NotificationManagerCompat.from(context).areNotificationsEnabled()
OLD答:
的人谁看这个问题,注意NotificationListenerService从“显示通知不同”。 这两个是不同的东西!如果某个应用有权访问NotificationListenerService
,则表示不会显示其通知,反之亦然。为了检查用户是否已经从您的应用程序或不阻止通知,您可以使用反射:
public class NotificationsUtils {
private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
public static boolean isNotificationEnabled() {
AppOpsManager mAppOps = (AppOpsManager) GlobalContext.getContext().getSystemService(Context.APP_OPS_SERVICE);
ApplicationInfo appInfo = GlobalContext.getContext().getApplicationInfo();
String pkg = GlobalContext.getContext().getApplicationContext().getPackageName();
int uid = appInfo.uid;
Class appOpsClass = null; /* Context.APP_OPS_MANAGER */
try {
appOpsClass = Class.forName(AppOpsManager.class.getName());
Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (int)opPostNotificationValue.get(Integer.class);
return ((int)checkOpNoThrowMethod.invoke(mAppOps,value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}
}
谢谢!它有很大的帮助! –
谢谢!你救了我的一天! –
AppOpsManager在API级别19(Android 4.4)中添加,所以这只能用于API级别19+ – nibarius
试试这个:
Is there a way an app can check if it is allowed to access notifications?
而且这里有一个多一点的解释:
Check for access to notifications using NotificationListenerService
这是为NotificationListenerService,它与SHOW NOTIFICATION不同! – Kasra
试试这个:
if (Settings.Secure.getString(getActivity().getContentResolver(), "enabled_notification_listeners").contains(getActivity().getPackageName())) {
// Notification access service already enabled
Toast.makeText(getActivity(),"notification enabled",Toast.LENGTH_LONG).show();
} else {
Intent intent = new Intent();
intent.setClassName("com.android.settings", "com.android.settings.Settings$AppNotificationSettingsActivity");
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);
startActivity(intent);
}
这是用于NotificationListenerService,它与SHOW NOTIFICATION不同! – Kasra
NotificationManagerCompat.from(context).areNotificationsEnabled()
似乎是很长的路要走。
我通过手动更改设置中的通知权限测试了它,它工作正常,在这两种情况下布尔值都正确返回。伟大的单线解决方案。只是在时间......我只是在这个答案发布后的几个小时内寻找解决方案..这么好的时机:)谢谢! – gnB
@gnB很好!很高兴它帮助;) – asamoylenko
这应该是接受的答案+1 – Sniper
- 1. 检查我的应用是否可以显示通知
- 2. 我如何知道用户是否允许推送通知
- 3. webservice如何检查用户是否被允许查看项目?
- 4. 检查Google Nearby是否已被允许
- 5. 检查用户是否允许本地通知。 iOS 8. Obj-C
- 6. 检查用户是否被允许在asp.net中查看网页
- 7. Java Spring - 检查用户是否被允许查看页面
- 8. Facebook是否允许通过应用程序sdk发送通知?
- 9. 如何知道MKMapView是否被允许使用当前位置?
- 10. 如何检查我的主机是否不允许SSH访问
- 11. ZF检查用户是否被允许使用资源
- 12. 如何检查是否有比datagrid高度显示更多行允许
- 13. 如何知道用户是否允许提示权限
- 14. 如何检查网站是否允许我使用我的书签注入Javascript?
- 15. 如何检查用户名是否被允许或不喜欢Twitter?
- 16. Angular - 在加载视图之前如何检查用户是否被允许?
- 17. 如何检查我的fb应用程序是否被禁止?
- 18. 检查列是否允许空值,C#?
- 19. 何时检查是否应该允许帐户使用Web应用程序?
- 20. 如何检测我的Android应用程序是否显示?
- 21. 如何检查谷歌浏览器中的用户是否已经允许通知?
- 22. 如何检查是否显示
- 23. 如何检查我的请求 - 响应是否被编码?
- 24. 如何我可以检查DatagridView的行是否被检查
- 25. 如何检查通知是否被调用,然后不要调用方法
- 26. 检测通知是否已被删除
- 27. iPhone:知道编程是cookie被允许或不允许
- 28. 如何调用PHP api来检查Ionic中的新通知并显示通知?
- 29. 如何检查我的应用的任何用户是否被Facebook禁止?
- 30. 显示通知检查的应用程序
这可以帮助你: http://stackoverflow.com/questions/24145343/checking-android-system-security-notification-access-setting-programmatically – TheTanic
的[安卓4.1可能重复:如何检查的通知禁用的应用程序?](http://stackoverflow.com/questions/11649151/android-4-1-how-to-check-notifications-are-disabled-for-the-application) – Sam