Android 4.1: 如何检查应用程序是否禁用了通知?

Android 4.1为用户提供了一个复选框,用于禁用特定应用程序的通知。

然而,作为一个开发人员,我们没有办法知道通知调用是否有效。

我确实需要检查当前应用程序的通知是否被禁用,但是我在 API 中找不到任何这方面的设置。

有没有办法在代码中检查这个设置?

63740 次浏览

You can't 100% can't.

It is asked in this Google I/O 2012 video and the Project lead for the new notifications declares that you can't.


Edit

2016 update: Now you can check it, as said in this Google I/O 2016 video.

Use NotificationManagerCompat.areNotificationsEnabled(), from support library, to check if notifications are blocked on API 19+. The versions below API 19 will return true (notifications are enabled).

enter image description here

It seems like there is no way to query notification state.

I recommend this:

  • Design you application with notifications.
  • Let user to disable notifications from application's settings.
  • Check whether notifications are clicked. If user clicks notification, save this to preferences.
  • In your app, if notification setting is on, and if user is Android 4.1+ (API 16), but if user doesn't click notification for some days / weeks, assume that user disabled notifications.

Not 100% correct. But this gives an opinion.
For example if user doesn't click any app notification for 10-15 days, probably he disabled it

Actually this is pretty easy to do:

/**
* Created by desgraci on 5/7/15.
*/
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(Context context) {


AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);


ApplicationInfo appInfo = context.getApplicationInfo();


String pkg = context.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;
}
}

If you are using Xamarin and you need this answer you can use this code:

//return true if this option is not supported.
public class NotificationsUtils
{
private const String CHECK_OP_NO_THROW = "checkOpNoThrow";
private const String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";


public static bool IsNotificationEnabled(global::Android.Content.Context context) {


AppOpsManager mAppOps = (AppOpsManager) context.GetSystemService(global::Android.Content.Context.AppOpsService);


ApplicationInfo appInfo = context.ApplicationInfo;


String pkg = context.ApplicationContext.PackageName;


int uid = appInfo.Uid;


try {


var appOpsClass = Java.Lang.Class.ForName("android.app.AppOpsManager");
var checkOpNoThrowMethod = appOpsClass.GetMethod(CHECK_OP_NO_THROW,Java.Lang.Integer.Type,Java.Lang.Integer.Type,new Java.Lang.String().Class);//need to add String.Type


var opPostNotificationValue = appOpsClass.GetDeclaredField (OP_POST_NOTIFICATION);
var value = (int)opPostNotificationValue.GetInt(Java.Lang.Integer.Type);
var mode = (int)checkOpNoThrowMethod.Invoke(mAppOps,value, uid, pkg);
return (mode == (int)AppOpsManagerMode.Allowed);


} catch (Exception)
{
System.Diagnostics.Debug.WriteLine  ("Notification services is off or not supported");
}
return true;
}
}

Answer from @blundell is correct but there is a minor change in newer versions.

NotificationManagerCompat.from(context).areNotificationsEnabled()

I use this method to check whether the notifications are enabled or not, the above-mentioned methods will work for checking whether notifications enabled or not. But from Android 8 onwards for creating notifications we have to create a channel first, so from Oreo, we have to check for your notification channel enabled or not.

    /**
* Checking Whether notifications are enabled or not
* @return true if notifications are enabled otherwise false
*/
public static final String CHANNEL_ID = "your_channel_id";


private boolean isNotificationChannelEnabled(){
if(NotificationManagerCompat.from(this).areNotificationsEnabled()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = manager.getNotificationChannel(CHANNEL_ID);
if (channel == null)
return true; //channel is not yet created so return boolean
// by only checking whether notifications enabled or not
return channel.getImportance() != NotificationManager.IMPORTANCE_NONE;
}
return true;
}
return false;
}