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

Swift macOS通知无声音求助(非DND模式)

macOS通知无声音排查步骤

1. 修正授权逻辑的执行时机

你的代码中requestAuthorization是异步操作,但直接在其后调用add(request)可能导致授权未完成就发送通知,声音权限未生效。应把添加请求的逻辑放到授权回调内:

let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.requestAuthorization(options: [.alert, .sound]) { granted, error in
    guard granted, error == nil else { return }
    DispatchQueue.main.async {
        notificationCenter.add(request)
    }
}

注意授权回调在后台线程,需切回主线程执行添加请求操作。

2. 检查系统端应用通知声音设置

  • 打开「系统设置」→「通知」,找到你的应用
  • 确认「声音」选项已勾选,且未选择「无」的声音
  • 同时确保「播放通知声音」开关处于开启状态

3. 尝试使用自定义通知声音

若默认声音异常,可添加.caf格式的自定义声音文件到项目(需勾选对应target),然后指定声音:

// 假设自定义声音文件名为custom_notification.caf
content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "custom_notification.caf"))

4. 验证当前通知权限状态

先查询应用的声音权限是否真的被授予:

UNUserNotificationCenter.current().getNotificationSettings { settings in
    print("声音权限状态: \(settings.soundSetting.rawValue)")
    // 正常状态应返回 enabled
}

若返回disabled,需引导用户到系统设置手动开启应用的通知声音权限。

5. 重启应用与通知中心

  • 完全退出你的应用后重新打开测试
  • 可通过终端执行命令重启通知中心:killall NotificationCenter

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 08:15:31