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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11649151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:21:34  来源:igfitidea点击:

Android 4.1: How to check notifications are disabled for the application?

androidandroid-notificationsandroid-notification-bar

提问by Guillaume Perrot

Android 4.1 offers the user a check box to disable notifications for a specific application.

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

However, as a developer we have no way to know whether a call to notify was effective or not.

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

I really need to check if the notifications are disabled for the current application but I can't find any setting for that in the API.

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

Is there ever a way to check this setting in the code?

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

回答by Blundell

You can't 100% can't.

你不能100%不能。

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

这个 Google I/O 2012 视频中提出了这个问题,新通知的项目负责人声明你不能。



Edit

编辑

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

2016 年更新:现在您可以检查它,如这个 Google I/O 2016 视频中所述

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).

使用NotificationManagerCompat.areNotificationsEnabled()支持库中的 来检查通知是否在 API 19+ 上被阻止。API 19 以下的版本将返回 true(已启用通知)。

enter image description here

在此处输入图片说明

回答by desgraci

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;
    }
}

回答by Prakash

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

@blundell 的回答是正确的,但较新的版本有细微的变化。

NotificationManagerCompat.from(context).areNotificationsEnabled()

回答by user3030630

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

如果您正在使用 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;
    }
}

回答by trante

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.
  • 设计带有通知的应用程序。
  • 让用户禁用来自应用程序设置的通知。
  • 检查通知是否被点击。如果用户单击通知,请将其保存到首选项。
  • 在您的应用中,如果通知设置已开启,并且用户是 Android 4.1+ (API 16),但如果用户在几天/几周内没有点击通知,则假设用户禁用了通知。

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

不是 100% 正确。但这给出了意见。
例如,如果用户在 10-15 天内没有点击任何应用通知,可能是他禁用了它

回答by sai Pavan Kumar

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 8onwards for creating notifications we have to create a channel first, so from Oreo, we have to check for your notification channel enabled or not.

我使用这种方法来检查通知是否已启用,上述方法将用于检查通知是否已启用。但是从Android 8开始,为了创建通知,我们必须先创建一个频道,所以从 Oreo 开始,我们必须检查您的通知频道是否已启用

    /**
     * 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;
    }