远程通知iOS 10、swift 3中的自定义声音
我对远程通知中的自定义声音有一个大问题 这是我的设想: 我有两种类型的通知,每种都有一个自定义的声音。当我的应用程序处于非活动状态或处于后台时,声音会像符咒一样播放,操作系统会在通知中心的条幅警报中显示通知 在前台状态下收到通知时,我处理通知,提取aps声音元素,然后通过AVAudioPlayer播放声音:远程通知iOS 10、swift 3中的自定义声音,ios,swift,apple-push-notifications,ios10,Ios,Swift,Apple Push Notifications,Ios10,我对远程通知中的自定义声音有一个大问题 这是我的设想: 我有两种类型的通知,每种都有一个自定义的声音。当我的应用程序处于非活动状态或处于后台时,声音会像符咒一样播放,操作系统会在通知中心的条幅警报中显示通知 在前台状态下收到通知时,我处理通知,提取aps声音元素,然后通过AVAudioPlayer播放声音: func playSound(_ sound: String) { myLog("Let's play sound \(sound)") // Plays new soun
func playSound(_ sound: String) {
myLog("Let's play sound \(sound)")
// Plays new sound file
DispatchQueue.main.async {
// Stops last player if playing
if (self.soundPlayer != nil) {
if (self.soundPlayer.isPlaying) {
self.soundPlayer.stop()
}
}
do {
let path = Bundle.main.path(forResource: sound, ofType:nil)!
let url = URL(fileURLWithPath: path)
self.soundPlayer = try AVAudioPlayer(contentsOf: url)
self.soundPlayer.delegate = self
if (self.soundPlayer.prepareToPlay()) {
self.soundPlayer.play()
}
} catch let error {
myLog("Error playing .caf file: \(error)")
}
}
}
问题就从这里开始。如果在执行“我的播放器代码”时,应用程序返回到后台-非活动状态,则notification center中的操作系统警报将不再播放自定义声音
总结:
- 应用程序未激活-后台收到通知->自定义声音播放正常
- 应用程序前台接收通知->代码通过playSound功能播放声音,然后如果应用程序在后台非活动状态下接收到其他通知,操作系统将播放默认通知声音
提前谢谢 问题自动解决。删除应用程序,声音恢复正常行为 问题自动解决。删除应用程序,声音恢复正常行为 我还使用AVPlayer在应用程序位于前台时播放声音,效果良好。但在后台模式下,当触发本地通知时,会播放默认通知声音,而不是自定义声音。然而,在iOS9中,使用“didReceiveLocalNotification”方法工作正常。有什么帮助吗?我还使用AVPlayer在应用程序位于前台时播放声音,效果很好。但在后台模式下,当触发本地通知时,会播放默认通知声音,而不是自定义声音。然而,在iOS9中,使用“didReceiveLocalNotification”方法工作正常。需要帮忙吗?