由于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 Questions 但没有得到解决方案。
任何帮助将不胜感激!