【发布时间】:2018-02-28 01:26:48
【问题描述】:
我的应用正在下载一个 .wav 文件并将其移动到库/声音,然后通过 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 = "PushNotifierLocalNotification"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)
编辑:这对于捆绑的声音(拖放到 Xcode)来说不是问题,但只有当我使用由我的应用下载并移入库/声音的声音时,我才会看到上述问题。
【问题讨论】:
-
您运行的是哪个操作系统版本和设备?
-
如果您直接创建
UNNotificationSound并将其分配给content.sound是否有效?我能够毫无问题地播放自定义声音。let sound = UNNotificationSound(named: "carhorn.wav") content.sound = sound -
希望您没有使用 iOS10 作为您的测试设备...因为在 10.2 之前的版本中存在错误。见here
-
man1: 你能确定下载的文件(.wav)是否听得见吗?
-
这听起来像是文件保护的问题。您为下载的声音文件分配了什么级别的保护?查看
FileProtectionType了解更多信息。
标签: ios swift push-notification apple-push-notifications