user6788419
user6788419

Reputation: 7350

Custom Local Notification Sound Not Working

I am trying to give custom notification sound for the local notification. But it still plays with default sound

let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey:
            "Test", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey:
            "Test message", arguments: nil)
content.sound = UNNotificationSound.init(named: "marbles-daniel_simon.mp3")
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60,
                                                        repeats: true)
 let request = UNNotificationRequest(identifier: "test", content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)

I have added sound file in project like below

enter image description here

Upvotes: 3

Views: 3783

Answers (3)

Jamil
Jamil

Reputation: 2999

I faced this issue and found a solution by put my sound file in the app root directory(where AppDelegate file exist).

Upvotes: 0

Jery Starr
Jery Starr

Reputation: 7

To make this answer easier.. 1. make sure sound is in the right format (.caf/m4r etc) 2. Select build phases tab found between build settings and build rules 3. Make sure your app is the target that is selected on the right side plane under Projects then targets. 4. Now expand the copy bundle resources tab 5. now click the plus button to add your sound. when the screen pops up select other and search for your sound (please select copy if needed tab) 6. That's it.

////// make sure that the rest you do in your app delegate file did finish launching with options. now you can get the sound to play by calling it with init(sound: "yoursoundname.caf")

Upvotes: -2

Arrabidas92
Arrabidas92

Reputation: 1153

I check for your problem in Apple documentation about Local and Remote Notifications, here is something interesting :

Local and remote notifications can specify custom alert sounds to be played when the notification is delivered. You can package the audio data in an aiff, wav, or caf file. Because they are played by the system-sound facility, custom sounds must be in one of the following audio data formats:

  • Linear PCM
  • MA4 (IMA/ADPCM)
  • µLaw
  • aLaw

Place custom sound files in your app bundle or in the Library/Sounds folder of your app’s container directory. Custom sounds must be under 30 seconds when played. If a custom sound is over that limit, the default system sound is played instead. When specifying custom sounds, specify only the filename of the sound file that you want played. If the system finds a suitable sound file with the name you provided, it plays that sound when delivering the notification. If the system does not find a suitable sound file, it plays the default sound.

content.sound = UNNotificationSound(named: "MySound.aiff")

Update : Add files to the Xcode project root. Make sure Add to targets is selected when adding files so that they are automatically add to the bundle resources

Add custom sound

Upvotes: 1

Related Questions