远程通知中的自定义声音 iOS 10, swift 3 [英] Custom sounds in remote notifications iOS 10, swift 3
问题描述
远程通知中的自定义声音有一个大问题.
I've a big problem with a custom sounds in remote notifications.
这是我的场景:
我有两种类型的通知,每一种都有自定义声音.当我的应用程序处于非活动状态或在后台时,声音就像一个魅力,操作系统在通知中心横幅警报中显示通知.
I've two types of notifications each one with a custom sound. The sound, plays like a charm, when my app is inactive or in background, and the OS shows the notification in notification center banner-alert.
当在前台状态收到通知时,我处理通知,提取 aps 声音元素并通过 AVAudioPlayer 播放声音:
When the notification is received in foreground state, I process the notification, extract the aps sound element and I play the sound via AVAudioPlayer:
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)")
}
}
}
问题由此开始.如果应用程序返回后台 - 当我的播放器代码执行时处于非活动状态,通知中心的操作系统警报将不再播放自定义声音.
And here start the problem. If the app returns to background - inactive when my player code is executed the OS alert in notification center never plays again the custom sounds.
总结:
应用处于非活动状态 - 后台收到通知 -> 自定义声音播放正常.
App inactive - background it receives notification -> Custom sounds plays ok.
应用前台收到通知 -> 代码通过 playSound 函数播放声音,然后如果应用在后台非活动状态下收到其他通知,操作系统播放默认通知声音.
App foreground it receives notification -> code plays sound via playSound function then if app receives other notification in background-inactive state OS plays default notification sound.
提前致谢.
推荐答案
问题已自动解决.删除应用程序,声音恢复正常.
Problem auto-solved. Deleting app, sound return to normal behaviour.
这篇关于远程通知中的自定义声音 iOS 10, swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!