我需要在我的ios应用中接收推送通知时播放自定义声音。
我知道,苹果不支持静音模式下的声音警报。我尝试使用自定义声音发送推送通知。它在设备未静音时播放,并以静音模式振动。
但是,最近我找到了一个应用程序 - Chipolo,即使应用程序处于被杀死状态,也会以静音模式显示自定义声音。
用于播放声音警报的技术是什么?
非常感谢任何帮助!
答案 0 :(得分:2)
我们无法在接收推送通知时更改系统声音,但我们可以使用MediaPlayer
和AVFoundation
播放音频。我们需要使用Notification Service扩展来处理前台和后台通知
在NotificationService Extension中
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)
}
}
}
在我的情况下,我需要检查推送通知SoundFileName
和Opcode
中的两个参数。如果Opcode
是RingDevice
,则表示我正在播放PushNotification中提到的声音
别忘了启用后台模式
希望这会对你有所帮助:)。
答案 1 :(得分:0)
关于在设备处于静音模式时播放声音,您根本无法立即执行。 只有Apple才能在出现紧急情况(例如找到设备)时覆盖静音开关。您也需要获得Apple的特殊许可,您的应用才能做到这一点。
Ganesh的答案是没有它的最佳选择。