I implemented local notification in my app but I am just wondering is there a way to play a sound that is not part of the main bundle of iPhone App. Basically in my app, I want user to record a sound that gets played when the local notification is generated instead of playing a pre-recorded or default sound. As far as i know this can be implementable because i have seen 2-3 App in app store which is doing the same thing which i want to do
- (void)alertSelector:(NSString *)AlertTitle WithFiringTime:(NSDate *)date
{
UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];
[localNotification setFireDate:date];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
NSDictionary *data = [NSDictionary dictionaryWithObject:date forKey:@"payload"];
[localNotification setUserInfo:data];[localNotification setAlertBody:AlertTitle];
[localNotification setAlertAction:@"View"]; [localNotification setHasAction:YES];
localNotification.soundName=@"voice.aif";
if (!localNotification)
return;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
My guess is your sound isn't formatted appropriately to play by the OS. Make sure your sound is formatted in IMA4 format. Also, make sure your file is in your application bundle, and you should be good to go.
For more reference and similar question, see this SO question.
Choose custom sound for Local Notifications
Here is a link for Apple's Local and Push Notification Guide. It explains the sounds in detail and the default sounds that can be used.
Make sure your sound file is added to the "Copy Bundle Resources" list under Project Settings->(Target)->Build Phases. This fixed the issue for me. At first I only got the default sound, even though I specified a custom file name (and the file was added to the project). But after I manually added the file to this list it started working.
I used a system sound from my mac to test with. I converted it using this command in a console:
From the description of the
soundName
property -It doesn't seem that sounds other than those present in the bundle can be used. Are you sure that the apps are using local notifications with their custom sounds? Maybe they are playing sounds with alerts or something like that.
HTH,
Akshay
are you cleaning the old notifications with?
else you can, delete the app from the phone (maybe need a reboot too) the it should work :-)
beside that the code mentioned above should be correct. and this line
localNotification.soundName=@"voice.aif";
should be enough to play custom sounds. but if you had notification tested first with no sound-file added, the default sound file (UILocalNotificationDefaultSoundName
) is registered! and to re-register you have to cancel the previous notification (cancelAllLocalNotifications
) or delete the app, to be able to "re"-register your notfication,beside that, the apple doc says:
hope it helps now :-)
You can convert from wav and mp3 using terminal :
'in.wav' is input sound which u want to convert and 'sound.caf' is output file ,change directory to where u want to have the out put file
Well I was experiencing same Issue , It sounds like this was not supported before or not doable to play custom sound for local notification that is not included in App Bundle. But later this is supported and you can assign custom sounds from App container specifically in Library/Sounds directory . And it worked with me . you can either download sounds from server to this directory or test it by copying a .caf(Apple said other extensions may work) file from bundle to Library/sounds directory and then remove reference for the file from bundle so that you can make sure that if sounds played it is from directory out of bundle . This all works very well , the main issue with me if user force quoted the app it stops working for local notification while it still works with push kit notifications , Im scheduling local one though when push kit push arrives. Here is the code for testing playing sound from directory :
After making sure .caf file is under Library/Sounds directory . All you have to do is merely assigning file name with extension to local notification soundName property like if the file is in bundle.