3

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

user6788419
  • 7,196
  • 1
  • 16
  • 28
  • Did you ever figure out what the issue was? And what device were you testing this on? – WBuck Mar 04 '18 at 03:15
  • @WBuck no, i was testing on iPhone 6 – user6788419 Mar 04 '18 at 05:09
  • I'm having the exact same issue, and I'm testing on an iPhone 6.. It works in the simulator though, using the iPhoneX and iPhone8 though... I posted a SO question as well. I'm wondering if this is a device issue. I'm going to reference your question in mine. – WBuck Mar 04 '18 at 12:32
  • @WBuck Please do a check on other devices too. I'm not sure about device issue. – user6788419 Mar 04 '18 at 12:41
  • I will be testing on another device probably next week. Unfortunately, both me and my wife only have an iPhone 6. I'll need to ask friends if I can borrow their phones for testing. – WBuck Mar 04 '18 at 12:45
  • change file name to `marbles_daniel_simon.mp3` and try once... – Fahim Parkar May 24 '23 at 08:55

3 Answers3

1

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

Arrabidas92
  • 1,113
  • 1
  • 9
  • 20
0

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

Jamil
  • 2,977
  • 1
  • 13
  • 23
-2

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")