gpt4 book ai didi

ios - 在静音模式下播放声音,应用已终止状态

转载 作者:行者123 更新时间:2023-12-01 16:08:22 26 4
gpt4 key购买 nike

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

我知道,苹果不支持静音模式下的声音提示。我尝试发送带有自定义声音的推送通知。当设备处于非静音状态时播放,并以静音模式振动。

但是,最近我发现了一个应用程序-Chipolo,即使该应用程序处于终止状态,该应用程序也可以以静音模式显示自定义声音。

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

任何帮助都非常感谢!

最佳答案

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

在NotificationService扩展中

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 SoundFileNameOpcode中的两个参数。如果 OpcodeRingDevice表示我正在播放PushNotification中提到的声音

不要忘记启用后台模式

enter image description here

希望这能够帮到你 :)

关于ios - 在静音模式下播放声音,应用已终止状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48971597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com