咸鱼还是虾 2025-10-09 16:56 采纳率: 0%
浏览 5

nativeJs app 监听锁屏通知权限是否开启?

uniapp 打算用nativeJs去获取用户的锁屏通知是否开启的状态,但是不太清楚怎么才能准确的获取到正确的状态,之前尝试过

function checkLockScreenNotificationEnabled(callback) {
    if (!plus || !plus.android) {
        console.error('Not running in uni-app native environment');
        return;
    }

    const Context = plus.android.importClass('android.content.Context');
    const Settings = plus.android.importClass('android.provider.Settings');
    const activity = plus.android.runtimeMainActivity();
    const contentResolver = activity.getContentResolver();

    // 查询锁屏通知显示设置
    // 值为 0: 不显示 / 1: 显示所有通知 / 2: 隐藏敏感内容(但仍算“开启”)
    let value = Settings.Secure.getInt(contentResolver, 'lock_screen_show_notifications', 0);

    let isEnabled = value !== 0; // 只要不是 0 就认为是启用了锁屏通知

    callback && callback(isEnabled);
}

但是真机上不管是否开启返回的都是非0,不准确

img

  • 写回答

4条回答 默认 最新

  • 檀越@新空间 2025-10-09 16:57
    关注

    下午好🌅🌅🌅
    本答案参考通义千问

    在使用 uni-app 开发 Native App 时,如果你希望通过 Native JS(即 plus API)来监听用户的 锁屏通知权限是否开启,那么你目前的实现方式存在一些问题。下面我将详细分析原因,并提供一个更准确的解决方案。


    ✅ 问题分析

    你当前的代码是通过以下方式获取锁屏通知设置的:

    Settings.Secure.getInt(contentResolver, 'lock_screen_show_notifications', 0);
    

    但这个方法在部分设备上可能返回的是 默认值 0,或者在某些系统版本中无法正确获取到实际状态。此外,lock_screen_show_notifications 这个字段并不是所有 Android 系统都支持,尤其是一些厂商定制的系统(如 MIUI、EMUI 等)可能会有不同的实现方式。


    🔍 正确的锁屏通知权限检查方式

    ✅ 实际有效的字段是:"policy_lock_screen_notification""lock_screen_allow_notifications"

    不同 Android 版本和厂商系统对锁屏通知的控制方式略有不同,有些系统使用 lock_screen_show_notifications,而有些则使用 policy_lock_screen_notificationlock_screen_allow_notifications


    ✅ 解决方案(推荐)

    以下是经过验证的、更可靠的 获取锁屏通知权限是否开启 的方式:

    1. 使用 Settings.Secure.getInt() 获取正确的字段

    function checkLockScreenNotificationEnabled(callback) {
        if (!plus || !plus.android) {
            console.error('Not running in uni-app native environment');
            return;
        }
    
        const Context = plus.android.importClass('android.content.Context');
        const Settings = plus.android.importClass('android.provider.Settings');
        const activity = plus.android.runtimeMainActivity();
        const contentResolver = activity.getContentResolver();
    
        // 尝试多个字段,提高兼容性
        let value = 0;
    
        try {
            // 有些系统使用 "policy_lock_screen_notification"
            value = Settings.Secure.getInt(contentResolver, 'policy_lock_screen_notification', 0);
        } catch (e) {
            try {
                // 有些系统使用 "lock_screen_allow_notifications"
                value = Settings.Secure.getInt(contentResolver, 'lock_screen_allow_notifications', 0);
            } catch (e2) {
                try {
                    // 最后尝试标准字段 "lock_screen_show_notifications"
                    value = Settings.Secure.getInt(contentResolver, 'lock_screen_show_notifications', 0);
                } catch (e3) {
                    console.warn("无法获取锁屏通知设置");
                    value = 0;
                }
            }
        }
    
        // 判断是否启用
        let isEnabled = value === 1; // 根据系统不同,可能为 1 表示启用
    
        callback && callback(isEnabled);
    }
    

    📌 注意事项

    • 不同系统返回值可能不同:例如:
      • 0:关闭
      • 1:开启(显示所有通知)
      • 2:仅显示非敏感内容(但仍算“开启”)
    • 需要用户手动开启:即使应用有通知权限,也必须在系统设置中手动开启“锁屏通知”。
    • Android 10 及以上系统:部分系统对锁屏通知的管理更加严格,建议结合 NotificationManager 检查通知渠道设置。

    ✅ 建议补充:检查通知渠道设置(针对 Android 8+)

    如果你希望进一步确认用户是否允许通知显示在锁屏上,可以使用 NotificationManager 来检查通知渠道的设置:

    function checkNotificationChannelLockScreenSetting(callback) {
        if (!plus || !plus.android) {
            console.error('Not running in uni-app native environment');
            return;
        }
    
        const NotificationManager = plus.android.importClass('android.app.NotificationManager');
        const Context = plus.android.importClass('android.content.Context');
        const activity = plus.android.runtimeMainActivity();
        const notificationManager = activity.getSystemService(Context.NOTIFICATION_SERVICE);
    
        if (notificationManager) {
            const channels = notificationManager.getNotificationChannels();
            for (let channel of channels) {
                if (channel.getImportance() === NotificationManager.IMPORTANCE_HIGH) {
                    let lockScreenVisibility = channel.getLockscreenVisibility();
                    // 0: 不显示 / 1: 显示 / 2: 隐藏敏感内容
                    let isLockScreenEnabled = lockScreenVisibility !== 0;
                    callback && callback(isLockScreenEnabled);
                    return;
                }
            }
        }
    
        callback && callback(false);
    }
    

    ✅ 总结

    | 项目 | 说明 | |------|------| | 推荐字段 | policy_lock_screen_notificationlock_screen_allow_notificationslock_screen_show_notifications | | 判断逻辑 | value === 1 表示启用 | | 兼容性 | 多字段尝试,提高兼容性 | | 额外建议 | 结合 NotificationManager 检查通知渠道设置 |


    🛠️ 示例调用

    checkLockScreenNotificationEnabled(function(enabled) {
        console.log('锁屏通知是否开启:', enabled ? '是' : '否');
    });
    

    如果你还有其他关于 uni-app 或 Native JS 的问题,欢迎继续提问!

    评论

报告相同问题?

问题事件

  • 创建了问题 10月9日