OGeek|极客世界-中国程序员成长平台

标题: ios - UNNotificationRequest 仅在 iPhone 解锁时播放自定义声音 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:48
标题: ios - UNNotificationRequest 仅在 iPhone 解锁时播放自定义声音

我的应用正在下载一个 .wav 文件并将其移动到 Library/Sounds,然后通过 UNNotificationRequest 使用该声音的文件名安排本地通知。此请求按预期发送通知,但我添加的自定义声音在手机解锁时大声播放,而不是在通知发送到手机的锁定屏幕时播放。但是,如果我使用 UNNotificationSound.default() 而不是我的自定义声音,则默认推送声音将在锁定屏幕和设备解锁时播放。任何人有任何想法可能导致这个问题?我的代码很简单:

let soundName = "demoSound.wav" // (or nil to use default)

let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests()

let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body";
content.sound = soundName.flatMap { UNNotificationSound(named: $0) } ??  UNNotificationSound.default()

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let identifier = "ushNotifierLocalNotification"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)

编辑:对于捆绑的声音(拖放到 Xcode 中)这不是问题,但只有当我使用由我的应用下载并移至库/声音中的声音时,我才会看到上述问题。



Best Answer-推荐答案


来自 here ,你会注意到:

Waveform Audio File Format (WAVE, or more commonly known as WAV due to its filename extension - both pronounced "wave"[6])3[7][8][9] (rarely, Audio for Windows)[10] is a Microsoft and IBM audio file format standard for storing an audio bitstream on PCs.

我假设 Apple 更喜欢原生 Apple 声音格式,例如 AIFFCAF ,两者都是由 Apple 开发的。

如果问题仍然存在,您能否使用 AIFF 或 CAF 声音文件格式重试并确认使用?

你也可以看看here .

关于ios - UNNotificationRequest 仅在 iPhone 解锁时播放自定义声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49020597/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4