在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. Angular - 在加載視圖之前如何檢查用戶是否被允許?
- 16. 如何檢查用戶名是否被允許或不喜歡Twitter?
- 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