You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity移动端Android全版本推送通知权限状态检测方法咨询

Android全版本通知权限状态检测方案(Unity)

你遇到的问题核心原因是:Android 10(API 29)不存在POST_NOTIFICATIONS权限,这个权限是Android 13(API 33)才新增的,所以用Permission.HasUserAuthorizedPermission("android.permission.POST_NOTIFICATIONS")检测低版本系统必然返回false——系统根本不识别这个权限。

要实现全版本的通知权限状态检测,需要分Android版本分支处理:

分版本检测逻辑

  • Android 13+(API 33及以上):直接检测POST_NOTIFICATIONS权限的授权状态
  • Android 8.0~12(API 26~32):通过系统NotificationManager的areNotificationsEnabled()方法判断
  • Android 8.0以下(API 25及以下):通过AppOpsManager检查OP_POST_NOTIFICATION的操作权限

Unity实现代码

using UnityEngine;

public static class NotificationPermissionChecker
{
    public static bool AreNotificationsEnabled()
    {
        if (Application.platform != RuntimePlatform.Android)
            return false;

        using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        {
            AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            
            // 获取系统SDK版本
            int androidSdkVersion = new AndroidJavaClass("android.os.Build$VERSION").GetStatic<int>("SDK_INT");

            if (androidSdkVersion >= 33)
            {
                // Android 13+ 检查POST_NOTIFICATIONS权限
                return Permission.HasUserAuthorizedPermission("android.permission.POST_NOTIFICATIONS");
            }
            else if (androidSdkVersion >= 26)
            {
                // Android 8.0+ 使用NotificationManager判断
                using (var notificationManager = currentActivity.Call<AndroidJavaObject>("getSystemService", "notification"))
                {
                    return notificationManager.Call<bool>("areNotificationsEnabled");
                }
            }
            else
            {
                // Android 8.0以下 使用AppOpsManager检查
                using (var appOpsManager = currentActivity.Call<AndroidJavaObject>("getSystemService", "appops"))
                {
                    string packageName = currentActivity.Call<string>("getPackageName");
                    int opPostNotification = new AndroidJavaClass("android.app.AppOpsManager").GetStatic<int>("OP_POST_NOTIFICATION");
                    // 检查操作权限,MODE_ALLOWED=0,MODE_IGNORED=1
                    int mode = appOpsManager.Call<int>("checkOpNoThrow", opPostNotification, AndroidJNI.GetEnv().GetJavaVM().GetVersion(), packageName);
                    return mode == 0 || mode == 1;
                }
            }
        }
    }
}

额外注意事项

  1. 权限声明:构建API设为33时,需确保AndroidManifest.xml中包含POST_NOTIFICATIONS权限(Unity Mobile Notification包通常会自动添加,可在Project Settings > Player > Publishing Settings > Build中查看合并后的Manifest确认):

    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    
  2. Android 13+权限申请:该版本需要动态申请权限,不能直接检测,示例代码:

    if (!NotificationPermissionChecker.AreNotificationsEnabled())
    {
        Permission.RequestUserPermission("android.permission.POST_NOTIFICATIONS");
    }
    
  3. 测试验证:在Android 10设备上调用NotificationPermissionChecker.AreNotificationsEnabled()会自动走低版本分支,正确返回用户是否在设置中关闭了通知。

内容的提问来源于stack exchange,提问作者Vladimir Simonov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 10:48:23