2015-01-07 50 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

相關問題