I'm trying to play remote audio file from notification extension
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
//...
let url = URL(string: "https://firebasestorage.googleapis.com/v0/b/XXX.appspot.com/o/XXXXXXX?alt=media&token=XXXXXXX")
let item = AVPlayerItem(url: url!)
NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: item)
let player = AVPlayer(playerItem: item)
player.play()
}
But nothing happens.
In capabilities of app I've enabled following background modes: "Audio, AirPlay, and PiP", "Background fetch", "Remote notifications"
I need to play sound when notification arrives without user interaction. Also, media attachment is not an option, because iOS 10 support is mandatory.
UPD: I've tried this
func download(url: URL) {
URLSession.shared.downloadTask(with: url, completionHandler: { _, _, error in
if let error = error {
print(error)
}
self.play(url: url)
}).resume()
}
func play(url: URL) {
let player = try! AVAudioPlayer(contentsOf: url)
player.prepareToPlay()
player.volume = 1.0
player.play()
}
Nothing happened too