Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUserNotification with custom soundName

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.

like image 334
Franco Solerio Avatar asked Sep 04 '12 16:09

Franco Solerio


2 Answers

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.

like image 80
Keith Smiley Avatar answered Oct 07 '22 12:10

Keith Smiley


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.

like image 37
jessecurry Avatar answered Oct 07 '22 12:10

jessecurry