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

Gui*_*rot 92 android android-notifications android-notification-bar

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

但是,作为开发人员,我们无法知道对通知的调用是否有效.

我真的需要检查当前应用程序是否禁用了通知,但我在API中找不到任何设置.

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

Blu*_*ell 140

你不能100%不能.

此Google I/O 2012视频中询问,并且新通知的项目负责人声明您不能.


编辑

2016年更新:现在您可以查看它,如此Google I/O 2016视频中所述.

NotificationManagerCompat.areNotificationsEnabled()从支持库中使用,检查API 19+上是否阻止了通知.API 19下面的版本将返回true(启用通知).

在此输入图像描述

  • @Stavros_S你需要使用`NotificationManagerCompat.from(ctx).areNotificationsEnabled()` (15认同)
  • 仅供参考,问题在48:05(视频问答时)中被问及(并回答),只有一个简短的字......不.http://www.youtube.com/watch?v=Yc8YrVc47TI&feature=player_detailpage#t=2885s (9认同)
  • 视频中没有任何内容显示我们无法读取此设置。需要明确的是:我只想能够读取复选框的当前状态,而不是更改它。恐怕你没有理解我的问题。 (2认同)
  • 我们需要这个,因为我们一次显示1个通知(可以是应用程序内或系统栏中).如果显示系统通知,我们不会显示应用内横幅,反之亦然.如果我们无法知道是否显示通知,我们将无法再管理其生命周期.我想我们现在必须彻底改变我们管理通知的方式...... (2认同)
  • @Stavros_S使用完整链接更新了答案.[NotificationManagerCompat.areNotificationsEnabled()](https://developer.android.com/reference/android/support/v4/app/NotificationManagerCompat.html#areNotificationsEnabled()). (2认同)

des*_*aci 39

实际上这很容易做到:

/**
 * 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 在发布问题时,Android 4.1是最新的,这仅适用于Android 4.4+并且似乎使用反射和文档不建议将其用于非系统应用程序. (3认同)

Pra*_*ash 33

来自@blundell的回答是正确的,但新版本有一些细微的变化.

NotificationManagerCompat.from(context).areNotificationsEnabled(??)
Run Code Online (Sandbox Code Playgroud)


use*_*630 5

如果您正在使用 Xamarin 并且需要此答案,则可以使用以下代码:

//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;
    }
}
Run Code Online (Sandbox Code Playgroud)