penatheboss penatheboss - 2 months ago 121
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
CKNotificationInfo
)

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
UNNotificationSound
, but I need the sound name. How can I get the sound name from the sound? Thanks!

Answer

Use the new UserNotifications framework:

import UserNotifications

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

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