LaVOZs

The World’s Largest Online Community for Developers

'; ios - How to play stored file in document directory as Local Notification sound? - LavOzs.Com

I'm trying to play a sound track stored in my documents directory . when i get this sound and play it in avplayer with URL as path it works fine, when i give the same url to local notification then it don't work. is there any possible way to play a stored sound as local notification File path : file:///var/mobile/Containers/Data/Application/BC43728E-7E45-448E-86C1-54684A82DF5F/Documents/lTTajzrSkCw.m4a

func saveAlarm() {
    let center = UNUserNotificationCenter.current()
    let content = UNMutableNotificationContent()
    content.title = "Alarm"
    content.body = "Testing our first alarm"
    if songs.count > 0 {

        let str = Constants.getUrl(for: songs[0].ID , ext: songs[0].ext)?.absoluteString
        content.sound = UNNotificationSound(named: UNNotificationSoundName(str ?? ""))
    }

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest(identifier: "Alarm", content: content, trigger: trigger)
    center.add(request, withCompletionHandler: {(error) in
        if error != nil {
            print(error?.localizedDescription)
        }
    })
}




static func getUrl(for fileName: String, ext: String) -> URL? {
   guard let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return nil }
   return documentsURL.appendingPathComponent(fileName).appendingPathExtension(ext)
}
Related
How to link to apps on the app store
Play sound in local notification
How can I create local notifications in iOS?
Playing a sound while app is in the foreground and the screen is locked - iOS
Play Local Notification sound twice
Local Notification sound not playing?
iOS - Reading an Audio file from Documents Directory
Local Notification custom sound NOT playing after OS8.3
How to play a sound using Swift?
Use UNNotificationRequest (UserNotificationFramework) for data only local notification on iOS