Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 10 UserNotifications custom sound in background mode

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!

like image 679
Muhammad Umair Avatar asked May 13 '17 11:05

Muhammad Umair


People also ask

How do you set custom notification sounds for apps on iPhone?

Set Custom Notification Sounds On iPhone To set a custom notification sound go to 'Settings,' scroll down, and then tap on 'Sound & Haptics. ' Under 'Sounds and Vibration Patterns,' select the type of notification that needs to be changed. From the menu, choose the sound that needs to be set as the notification sound.

What is background push service?

Unlike regular push notifications, background push notifications do not show app icon badges, trigger a sound, or display a message when received by your app. The system wakes the app in the background for a short time to silently initiate downloads, update content, or perform other actions.


1 Answers

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")
like image 82
Muhammad Umair Avatar answered Oct 30 '22 14:10

Muhammad Umair