2015-01-07 25 views
9

在Android设置中,用户可以关闭通知,如果他们不想。那么有没有像isNotificationAllowed()检查的任何方法是我的应用程序被允许显示通知?以及如何打开android设置页面来指导我的用户打开通知?如何检查我的应用是否被允许显示通知

+0

这可以帮助你: http://stackoverflow.com/questions/24145343/checking-android-system-security-notification-access-setting-programmatically – TheTanic

+1

的[安卓4.1可能重复:如何检查的通知禁用的应用程序?](http://stackoverflow.com/questions/11649151/android-4-1-how-to-check-notifications-are-disabled-for-the-application) – Sam

回答

28

编辑 - 新建答案:

好像谷歌加入适当的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; 
} 
} 

来源:https://code.google.com/p/android/issues/detail?id=38482

+0

谢谢!它有很大的帮助! –

+0

谢谢!你救了我的一天! –

+0

AppOpsManager在API级别19(Android 4.4)中添加,所以这只能用于API级别19+ – nibarius

-1

试试这个:

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); 
} 
+0

这是用于NotificationListenerService,它与SHOW NOTIFICATION不同! – Kasra

24
NotificationManagerCompat.from(context).areNotificationsEnabled() 

似乎是很长的路要走。

+0

我通过手动更改设置中的通知权限测试了它,它工作正常,在这两种情况下布尔值都正确返回。伟大的单线解决方案。只是在时间......我只是在这个答案发布后的几个小时内寻找解决方案..这么好的时机:)谢谢! – gnB

+0

@gnB很好!很高兴它帮助;) – asamoylenko

+0

这应该是接受的答案+1 – Sniper

相关问题