远程通知中的自定义声音iOS 10,Swift 3 [英] Custom sounds in remote notifications iOS 10, swift 3

查看:195
本文介绍了远程通知中的自定义声音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)")
        }
    }

}

从这里开始问题.如果应用程序返回到后台-执行我的播放器代码后处于非活动状态,则通知中心中的OS警报将不再播放自定义声音.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆