UNNotificationSound 不起作用

问题描述 投票:0回答:2

新的 iOS10 用户通知框架的自定义声音不起作用。 同一文件适用于旧的 UILocalNotification,但现在始终播放默认系统声音。当我设置 nil 时,我没有声音,所以只是带有名称的 init 是有问题的

let notificationContent = UNMutableNotificationContent()
notificationContent.title = "XYZ"
notificationContent.body = "XYZ"
notificationContent.sound = UNNotificationSound(named: "sound_name.aif")
audio notifications ios10
2个回答
6
投票

我改变了这一行:

notificationContent.sound = UNNotificationSound(named: "sound_name.aiff")

至:

notificationContent.sound = UNNotificationSound.init(named: "sound_name.aiff")

正如你所看到的,我将声音转换为

*.aiff
,在阅读 API 参考之后,我将声音文件移动到名为 Sounds 的文件夹中。

我确保我的声音文件时长低于 30 秒


0
投票

尝试使用

UNNotificationSoundName
结构:

if let soundFilename {
    content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: soundFilename))
} else {
    content.sound = UNNotificationSound.default
}
© www.soinside.com 2019 - 2024. All rights reserved.