新的 iOS10 用户通知框架的自定义声音不起作用。 同一文件适用于旧的 UILocalNotification,但现在始终播放默认系统声音。当我设置 nil 时,我没有声音,所以只是带有名称的 init 是有问题的
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "XYZ"
notificationContent.body = "XYZ"
notificationContent.sound = UNNotificationSound(named: "sound_name.aif")
我改变了这一行:
notificationContent.sound = UNNotificationSound(named: "sound_name.aiff")
至:
notificationContent.sound = UNNotificationSound.init(named: "sound_name.aiff")
正如你所看到的,我将声音转换为
*.aiff
,在阅读 API 参考之后,我将声音文件移动到名为 Sounds 的文件夹中。
我确保我的声音文件时长低于 30 秒
UNNotificationSoundName
结构:
if let soundFilename {
content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: soundFilename))
} else {
content.sound = UNNotificationSound.default
}