Philip Young
Philip Young

Reputation: 433

UNNotificationSound doesn't play on macOS. Same file works on iOS

Been pulling my hair for the past week.

What I've learned:

Here's the code I used on appDelegate

let center = UNUserNotificationCenter.current()
        center.delegate = self

        let cont = UNMutableNotificationContent()
        cont.title = "Test notification"
        let sound = UNNotificationSoundName("eventually.m4r")
        cont.sound = UNNotificationSound(named: sound)
        let req = UNNotificationRequest(identifier: "Ay", content: cont , trigger: nil)
        center.add(req)

I've religiously followed https://developer.apple.com/documentation/usernotifications/unnotificationsound without no avail.

What I've made sure:

p.s. this might be potentially a macOS bug, but I don't understand how some file works. What's confusing is app like Slack and others could use custom sound notification. I've also had a bug where repeat:true trigger only called once on macOS, but repeatedly on iOS `UNTimeIntervalNotificationTrigger` repeats: true only fired once. macOS bug? Works on iOS

Upvotes: 8

Views: 990

Answers (3)

zubko
zubko

Reputation: 1797

As for developing the macOS app in Xcode 15.4 on macOS 14.5, I found out that the app with the sound should be in the Applications folder as it's said in the previous answer, but the sound file should be referenced without extension:

content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "ting"))

The file I use is .aiff

And then I can continue developing the app while keeping the old version in Appplications - the updated one that I run from Xcode will also play the proper sound. 🤪

Upvotes: 0

Diggory
Diggory

Reputation: 645

I recently has the same issue.

After some great detective work from another developer it seems that the issue was caused by the "Play user interface sound effects" option in the 'Sound Effects' section of the 'Sound' System Preferences pane.

I had to turn that option off, delete my app, reboot, then re-enable that option and the custom sounds now work. Files in the .caf format still don't work, but that can be worked around.

Upvotes: 2

sirob
sirob

Reputation: 96

In recently exploring this I found that all of the following need to be true for it to find the sound on Mac:

  1. The sound file must ends up in the Resources folder inside the bundle, not a sub-directory of Resources.

  2. The application must be in Applications. After building from XCode, move/copy the bundle into Applications to test, it won't work if you run it in-place.

  3. The file must've been placed into the bundle by XCode as a resource, it's not enough for it to simply be placed there. (This is not really relevant for XCode devs, but this is a problem using something like Unity as you can't use a simple build step to get the files to the correct place)

Also, importantly, the OS sometimes remembers when it has looked up a notification sound and failed to find it. So it may be that you had it set up wrong during your first test and then got it set up correctly, but the OS still doesn't play the sound. Test with a new filename or reboot the Mac to be sure.

Upvotes: 8

Related Questions