Since UILocalNotification
is deprecated in iOS 10 so I have updated my local notification flow using the UNUserNotification framework.
The app plays custom sound using AVPlayer when the app is in the foreground and it works fine. But in background mode when local notification is triggered, instead of custom sound, a default notification sound is being played.
However, things were working fine in iOS9, using "didReceiveLocalNotification" method app can play custom sound even in background mode.
Update 1:
I'm setting local notification as follows:
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationStringForKey("reminder!", arguments: nil)
content.body = NSString.localizedUserNotificationStringForKey("Reminder body.", arguments: nil)
if let audioUrl == nil {
content.sound = UNNotificationSound.defaultSound()
} else {
let alertSound = NSURL(fileURLWithPath: self.selectedAudioFilePath)
content.sound = UNNotificationSound(named: alertSound.lastPathComponent!)
}
content.userInfo = infoDic as [NSObject : AnyObject]
content.badge = 1
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
let identifier = "Reminder-\(date)"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
let center = UNUserNotificationCenter.currentNotificationCenter()
center.addNotificationRequest(request, withCompletionHandler: { (error) in
if error != nil {
print("local notification created successfully.")
}
})
I have already gone through many SO Questions but didn't get the solution.
Any help will be greatly appreciated!
Found out that alertSound
doesn't have the correct file path hence the content.sound
sets nothing. I was saving the recorded file in documents directory and the file path retrieve for real device is different as compared to the simulator.
Setting a sound file which is placed in project bundle did the trick
content.sound = UNNotificationSound(named: "out.caf")