My Code
private let synthesizer = AVSpeechSynthesizer()
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
let utterance = AVSpeechUtterance(string: "Hello Sim")
utterance.voice = AVSpeechSynthesisVoice(language: "th_TH")
utterance.rate = 1.0
utterance.pitchMultiplier = 1.0
utterance.volume = 1.0
utterance.preUtteranceDelay = 0
self.synthesizer.usesApplicationAudioSession = false
self.synthesizer.speak(utterance)
} catch {
print(error.localizedDescription)
}
if let bestAttemptContent = self.bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
Info.plist enable
UIBackgroundModes
audio
fetch
processing
remote-notification
payloadNotification
{
"aps":{
"alert":{
"title":"title"
"subtitle":"subtitle"
"body":"body"
}
"mutable-content":1
"content-available":1
"sound":"tester.caf"
}
}
This code can play in simulator but can't play in real device