由于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.")
}
})我已经经历了很多问题,但没有得到解决办法。
任何帮助都将不胜感激!
发布于 2017-05-13 16:20:24
发现alertSound没有正确的文件路径,因此content.sound没有设置任何文件路径。我将记录的文件保存在文档目录中,实际设备的文件路径检索与模拟器不同。
设置一个放置在项目包中的声音文件就成功了。
content.sound = UNNotificationSound(named: "out.caf")发布于 2019-03-20 10:49:24
在Swift 4.2和Xcode 10.1中
本地通知播放声音
let content = UNMutableNotificationContent()
content.title = "Notification Tutorial"
content.subtitle = "from ioscreator.com"
content.body = " Notification triggered"
//Default sound
content.sound = UNNotificationSound.default
//Play custom sound
content.sound = UNNotificationSound(named:UNNotificationSoundName(rawValue: "123.mp3"))"123.mp3“文件需要在您的包中。
发布于 2017-05-13 11:44:46
在推送通知包中,需要包含要在后台播放的自定义声音。这个声音也需要包含在您的应用程序中。
例如,下面是如何在中创建自定义通知包的方法。注意,在我的通知对象中,我有一个声音对象。
array("to" => $sDeviceID,
"data" => array($sMessageType => $sMessage,
"customtitle" => $sMessageTitle,
"custombody" => $sMessage,
"customimage" => "$mIcon") ,
"notification" => array("title" => "sMessageTitle",
"text" => $sMessage,
"icon" => "$mIcon",
"sound" => "mySound.mp3"));https://stackoverflow.com/questions/43952714
复制相似问题