我正在努力实现像查找我的iPhone应用程序这样的功能。即使在关闭状态下,它也会以最大音量播放声音。我不想用VOIP电话来做这件事,因为我的应用程序不需要它,所以它可能会被拒绝。
以下是我要做的:
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
var audioPlayer = AVAudioPlayer()
let songTitle = "ping"
let songExtension = "mp3"
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 {
let data = try Data(contentsOf: url)
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: .mixWithOthers)
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer = try AVAudioPlayer(data: data, fileTypeHint: AVFileType.mp3.rawValue)
audioPlayer.volume = 1.0
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch let error {
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) [modified]"
contentHandler(bestAttemptContent)
}
}
每当我尝试调试应用程序时,我都会遇到错误:错误域=NSOSStatusErrorDomain Code=5561015905";(null(">
你不能这么做。苹果可以在紧急情况下覆盖静音开关,比如找到设备。你不是苹果,所以你不能获得特殊豁免,让你的通知播放覆盖静音开关的声音。