penatheboss penatheboss - 8 months ago 242
Swift Question

Sound name from UNNotificationSound

I am working with push notifications in CloudKit. Here is what I used before Swift 3:

notification.soundName = UILocalNotificationDefaultSoundName

(notification is a

However, this gives me a warning:

'UILocalNotificationDefaultSoundName' was deprecated in iOS 10.0: Use UserNotifications Framework's +[UNNotificationSound defaultSound]

So I added the UserNotification framework to my Xcode project, and I imported it to my ViewController. I then tried this:

notification.soundName = UNNotificationSound.default()

However, this gives me an error:

Cannot assign value of type 'UNNotificationSound' to type 'String?'

So the default sound is a
, but I need the sound name. How can I get the sound name from the sound? Thanks!


Use the new UserNotifications framework:

import UserNotifications

let content = UNMutableNotificationContent()
content.sound = UNNotificationSound.default()

let request = UNNotificationRequest(identifier: "id", content: content, trigger: nil)