iosswiftxcodeunnotificationrequest

How to get UNNotificationSound to use the .caf files I have in a blue folder in my project


I'm creating a UNNotificationRequest with a UNNotificationSound as:

content.sound = UNNotificationSound(named: "alarmSounds/alarmAscending.caf")

The sound is in the folder (blue) alarmSounds in the main project folder.

The sound used is always the default sound instead of mine. I also tried it without the "alarmSound/" in the "named:" parameter but still not working.

Is there something special I need to do with the sound files?

This is using xcode 9 and trying it on an iOS 11 device.


Solution

  • Make sure the sound file must be contained in the app’s bundle or in the Library/Sounds folder of the app's data container. If files exist in both locations then the file in the app's data container will be preferred.

    To confirm/correct this...

    Right click the file in the "Project Navigator", select "Show File Inspector" and make sure the file has a check mark next to your project in the "Target Membership" area.