{"id":936,"date":"2023-03-11T18:01:36","date_gmt":"2023-03-11T10:01:36","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=936"},"modified":"2023-04-29T15:43:58","modified_gmt":"2023-04-29T07:43:58","slug":"android-determines-whether-application-notification-permissions-are-enabled-and-jumps-to-setting-page","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/android-determines-whether-application-notification-permissions-are-enabled-and-jumps-to-setting-page\/","title":{"rendered":"Android\u5224\u65ad\u5e94\u7528\u901a\u77e5\u6743\u9650\u662f\u5426\u5f00\u542f\u4ee5\u53ca\u8df3\u8f6c\u5230\u8bbe\u7f6e\u754c\u9762"},"content":{"rendered":"<p>\u56e0\u4e3a\u9879\u76ee\u7528\u5230\u63a8\u9001\u529f\u80fd\uff0c\u6240\u4ee5\u9700\u6c42\u662f\u77e5\u9053\u7528\u6237\u662f\u5426\u5f00\u542f\u901a\u77e5\u680f\u6743\u9650\uff0c\u5f53\u901a\u77e5\u680f\u6743\u9650\u672a\u5f00\u542f\u65f6\u8df3\u8f6c\u5230\u5e94\u7528\u8bbe\u7f6e\u754c\u9762\uff0c\u4ee5\u4fbf\u7528\u6237\u81ea\u884c\u5f00\u542f\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">public class NotificationUtil {\n    private static final String CHECK_OP_NO_THROW = &quot;checkOpNoThrow&quot;;\n    private static final String OP_POST_NOTIFICATION = &quot;OP_POST_NOTIFICATION&quot;;\n\n    \/**\n     * \u83b7\u53d6\u901a\u77e5\u680f\u6743\u9650\u662f\u5426\u5f00\u542f\n     * @param context\n     * @return\n     *\/\n    @RequiresApi(api = Build.VERSION_CODES.KITKAT)\n    public static boolean isNotificationEnabled(Context context) {\n        AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);\n        ApplicationInfo appInfo = context.getApplicationInfo();\n        String pkg = context.getApplicationContext().getPackageName();\n        int uid = appInfo.uid;\n\n        Class appOpsClass = null;\n        \/* Context.APP_OPS_MANAGER *\/\n        try {\n            appOpsClass = Class.forName(AppOpsManager.class.getName());\n            Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);\n            Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);\n\n            int value = (Integer) opPostNotificationValue.get(Integer.class);\n            return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);\n        } catch (ClassNotFoundException e) {\n            e.printStackTrace();\n        } catch (NoSuchMethodException e) {\n            e.printStackTrace();\n        } catch (NoSuchFieldException e) {\n            e.printStackTrace();\n        } catch (InvocationTargetException e) {\n            e.printStackTrace();\n        } catch (IllegalAccessException e) {\n            e.printStackTrace();\n        }\n        return false;\n    }\n\n    \/**\n     * \u8fdb\u5165\u5e94\u7528\u8bbe\u7f6e\u754c\u9762\n     * @param context\n     *\/\n    public static void requestNotificationSetting(Context context) {\n        Intent intent = new Intent();\n        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);\n        if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.GINGERBREAD) {\n            intent.setAction(&quot;android.settings.APPLICATION_DETAILS_SETTINGS&quot;);\n            intent.setData(Uri.fromParts(&quot;package&quot;, context.getPackageName(), null));\n        } else if (Build.VERSION.SDK_INT &lt;= Build.VERSION_CODES.FROYO) {\n            intent.setAction(Intent.ACTION_VIEW);\n            intent.setClassName(&quot;com.android.settings&quot;, &quot;com.android.setting.InstalledAppDetails&quot;);\n            intent.putExtra(&quot;com.android.settings.ApplicationPkgName&quot;, context.getPackageName());\n        }\n        context.startActivity(intent);\n    }\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u56e0\u4e3a\u9879\u76ee\u7528\u5230\u63a8\u9001\u529f\u80fd\uff0c\u6240\u4ee5\u9700\u6c42\u662f\u77e5\u9053\u7528\u6237\u662f\u5426\u5f00\u542f\u901a\u77e5\u680f\u6743\u9650\uff0c\u5f53\u901a\u77e5\u680f\u6743\u9650\u672a\u5f00\u542f\u65f6\u8df3\u8f6c\u5230\u5e94\u7528\u8bbe\u7f6e\u754c\u9762\uff0c\u4ee5\u4fbf\u7528\u6237\u81ea [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-936","post","type-post","status-publish","format-standard","hentry","category-android-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=936"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/936\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}