NSUserNotification with custom soundName
Asked Answered
Y

2

11

Did anyone manage to make a NSUserNotification soundName to work with a custom sound? I tried with aif and caf format 44100KHz 16bit 2 second of duration. The notification is displayed at the proper time, with the right title and text, but the default sound gets played instead of my custom sound.

The sound files are correctly copied in the application bundle. If I try this the sounds work ok:

NSSound* sound = [NSSound soundNamed:@"morse.aif"];
[sound play];

But when I use the same sound in my notification, the default notification sound gets played:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    NSUserNotification* notification = [[NSUserNotification alloc]init];
    notification.title = @"Titolo";
    notification.deliveryDate = [NSDate dateWithTimeIntervalSinceNow:10];
    notification.soundName = @"morse.aif";
    [[NSUserNotificationCenter defaultUserNotificationCenter]scheduleNotification:notification];
}

I tried with and without extension, but with no success.

notification.soundName = @"morse.aif";
notification.soundName = @"morse2.caf";
notification.soundName = @"morse";     

none of these work.

My application is not signed and not sandboxed, but I don't think that's necessary for user notifications, and apart from the sound problem the notifications work great.

Yeomanry answered 4/9, 2012 at 16:5 Comment(4)
Did you figure this out? I am having the same issue. Asked in the Apple Dev Forms and was told to file a bug report to get them to "look into it". Really hoping there is some way to figure this out as I can't wait and hope Apple looks into it...Ez
As it often happens, the issue sorted out by itself after some time. Maybe there was some kind of caching system in place.Yeomanry
Getting the same thing here in July 2014.Helbona
I noticed that the app icon in notifications only gets updated when the version number in the info.plist gets bumped. I wonder if they're using that as a cache key?Blunderbuss
P
10

It seems to me like this issue is case-sensitivity. Using notification.soundName = @"Morse"; works perfectly for me. As you can see in the NSSound documentation for soundNamed The search folders for sounds are, in order:

~/Library/Sounds
/Library/Sounds
/Network/Library/Sounds
/System/Library/Sounds

If you look in the last one, which is probably where you're trying to pull from since they're the sounds in System Preferences, you can see their names

Sound names

So keep the case of the file, and omit the extension and it should work as expected.

Pinch answered 5/7, 2013 at 17:8 Comment(1)
If I put my custom Morse.aiff into ~/Liibrary/Sounds/ on macOS 10.14, it does not work. The default Morse sound is played.Icebox
B
2

If you have multiple versions of your App binary notification center may be searching the wrong binary for your sound files.

If you make sure to delete any old copies of the binary it should fix the issue.

From the Apple Dev Forums: https://devforums.apple.com/message/708511

This can happen if you have multiple version of your app binary floating around. NotificationCenter only fines one of them. If it is the one without the sound then it will not work.

Blunderbuss answered 23/3, 2015 at 2:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.