Ios 在静音模式下播放声音,应用程序关闭状态

Ios 在静音模式下播放声音,应用程序关闭状态,ios,audio,apple-push-notifications,Ios,Audio,Apple Push Notifications,我需要在ios应用程序中接收推送通知时播放自定义声音 我知道,苹果不支持静音模式下的声音警报。我已尝试使用自定义声音发送推送通知。当设备不静音且在静音模式下振动时播放 但是,最近我发现了一个应用程序——在静默模式下显示自定义声音,即使应用程序处于关闭状态 播放声音警报时使用了什么技术 非常感谢您的帮助 接收推送通知时,我们无法更改系统声音,但我们可以使用MediaPlayer和AVFoundation播放音频。我们需要使用通知服务扩展来处理前台和后台通知 通知服务扩展中的 import User

我需要在ios应用程序中接收推送通知时播放自定义声音

我知道,苹果不支持静音模式下的声音警报。我已尝试使用自定义声音发送推送通知。当设备不静音且在静音模式下振动时播放

但是,最近我发现了一个应用程序——在静默模式下显示自定义声音,即使应用程序处于关闭状态

播放声音警报时使用了什么技术


非常感谢您的帮助

接收推送通知时,我们无法更改系统声音,但我们可以使用
MediaPlayer
AVFoundation
播放音频。我们需要使用通知服务扩展来处理前台和后台通知

通知服务扩展中的

import UserNotifications
import AVFoundation
import MediaPlayer

class NotificationService: UNNotificationServiceExtension {


    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

        // Properties
        var error:NSError?
        var audioPlayer = AVAudioPlayer()

        let tempInfo = request.content.userInfo as! [String:NSObject]
        var songName = ""
        if tempInfo.index(forKey: "SoundFileName") != nil {
            songName = (tempInfo["SoundFileName"] as! String)
        }

        var opecode = ""
        if tempInfo.index(forKey: "Opcode") != nil {
            opecode = (tempInfo["Opcode"] as! String)
        }

        if opecode == "RingDevice" {
            var songTitle = ""
            var songExtension = ""
            if songName == "" {
                songTitle = "Input"
                songExtension = "caf"
            } else  {
                let testStr = songName.components(separatedBy: ".")
                songTitle = "\(testStr[0])"
                songExtension = "\(testStr[1])"
            }

                if let url = Bundle.main.url(forResource: "\(songTitle)", withExtension: "\(songExtension)")  {

                    let volumeView = MPVolumeView()
                    if let view = volumeView.subviews.first as? UISlider{
                        view.value = 1.0 //---0 t0 1.0---

                    }

                    do {
                        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)

                        try AVAudioSession.sharedInstance().setActive(true)

                        audioPlayer = try AVAudioPlayer(contentsOf: url,  fileTypeHint: AVFileType.caf.rawValue)
                        audioPlayer.volume = 1.0
                        DispatchQueue.main.asyncAfter(deadline: .now() + 5) { // Give 5 sec delay to play audio or else audio will not hearable
                             audioPlayer.play()
                        }
                    } catch _ as NSError {
                        print("[SoundPlayer] There was an error: \(String(describing: error))")
                    }

                    if (audioPlayer.isPlaying) {
                        audioPlayer.stop()
                    }
                    audioPlayer.play()
                }
        }


        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...            
            bestAttemptContent.title = "\(bestAttemptContent.title)"

            contentHandler(bestAttemptContent)
        }
    }

    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }

}
在我的例子中,我需要检查pushnotification
SoundFileName
Opcode
中的两个参数。如果
Opcode
RingDevice
则表示我正在播放PushNotification中提到的声音

别忘了启用背景模式


希望这对您有所帮助:)

至于在设备处于静音模式时播放声音,您根本无法立即播放。 只有苹果可以在紧急情况下(比如找到设备)超越静音开关。您的应用程序也需要获得苹果公司的特别许可


甘尼什的答案是没有它你能做的最好的事情。

你找到解决办法了吗?还没有。我联系了苹果公司寻求技术支持,关于这一点,他们的回复是出于设计,应该是这样的。在静音模式和应用程序关闭状态下无法播放声音。苹果公司的话——“你所描述的行为和由此产生的限制是设计造成的。如果你认为苹果公司应该考虑一种替代方法,我们鼓励你提交一份增强请求,其中包括该设计决策对你的影响,以及你希望看到的不同做法。”但我想知道,Chipolo等应用程序是如何做到这一点的。不知道,谢谢你,甘尼什。我试过这种方法。但当应用程序处于关闭状态,手机处于静音模式时,它并没有播放音频。我可以在后台播放音频,也就是说,当应用程序使用home按钮最小化时,我在死区状态下为我工作。你启用后台模式了吗?回答更新,检查一下