Been pulling my hair for the past week.
What I've learned:
- Notification sound works on iOS, not macOS
Files on
~/Library/Sound
(notice the tilde, it's outside the project folder):- Some (not all) file played with
.m4r
and.aiff
format fired the sound - Sound fired when referencing it without file extension
UNNotificationSound("quack")
- Default sound when referencing it with file extension
UNNotificationSound("quack.m4r")
- Some (not all) file played with
Files on on root project directory (
/
), or onLibrary/Sound
:- For sound that played on
~/Library/Sounds
:- No notification sound (silent) when referencing it without file extension
quack
- Default sound when referencing it with file extension
quack.m4r
- No notification sound (silent) when referencing it without file extension
- For sound that played not played
~/Library/Sounds
:- Anything will play default notification sound.
- For sound that played on
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:
- I put the files (.m4r, .aiff) on root project directory
- File is included in the bundle
- target macOS and iOS is checked
- Sound played on iOS, but not macOS
- It's less than 30 second
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