作者:若v0丷會飛的獨角獸 | 来源:互联网 | 2023-02-04 05:58
由于UILocalNotification
在iOS 10中已弃用,因此我已使用UNUserNotification框架更新了本地通知流程。
当应用程序位于前台时,它会使用AVPlayer播放自定义声音,并且可以正常工作。但是在后台模式下,当触发本地通知时,会播放默认的通知声音,而不是自定义声音。
但是,在iOS9中一切正常,使用“ didReceiveLocalNotification”方法的应用程序即使在背景模式下也可以播放自定义声音。
更新1:
我将本地通知设置如下:
let cOntent= UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationStringForKey("reminder!", arguments: nil)
content.body = NSString.localizedUserNotificationStringForKey("Reminder body.", arguments: nil)
if let audioUrl == nil {
content.sound = UNNotificationSound.defaultSound()
} else {
let alertSound = NSURL(fileURLWithPath: self.selectedAudioFilePath)
content.sound = UNNotificationSound(named: alertSound.lastPathComponent!)
}
content.userInfo = infoDic as [NSObject : AnyObject]
content.badge = 1
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
let identifier = "Reminder-\(date)"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
let center = UNUserNotificationCenter.currentNotificationCenter()
center.addNotificationRequest(request, withCompletionHandler: { (error) in
if error != nil {
print("local notification created successfully.")
}
})
我已经经历了很多SO问题,但是没有得到解决方案。
任何帮助将不胜感激!
1> Muhammad Uma..:
发现alertSound
没有正确的文件路径,因此不content.sound
设置任何内容。我将记录的文件保存在documents目录中,与模拟器相比,实际设备的文件路径检索有所不同。
设置放置在项目包中的声音文件可以解决问题
content.sound = UNNotificationSound(named: "out.caf")