iOS 4.3, iPhone 4 not playing custom sound with UILocalNotification - iphone

There are posts similar to mine, but I have not found one exactly like it. Here is what happens:
In my app I schedule a local notification:
UIApplication* app = [UIApplication sharedApplication];
[app cancelAllLocalNotifications];
UILocalNotification *localNotif = [[ UILocalNotification alloc ] init];
NSDate *date = [ [[ NSDate alloc] initWithTimeIntervalSinceNow:30 ] autorelease ];
if (localNotif == nil) {
return;
}
localNotif.fireDate = date;
localNotif.alertBody = #"Nearing Return Location!";
localNotif.alertAction = #"Ok";
localNotif.soundName = #"2_bubble_wrap.caf";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
Which works just fine in iOS 5. In iOS 4.3 I took care to make sure that the sound was on and loud before testing. Naturally the file is less than 30 seconds duration. When it triggers on the iPhone 4, I got no sound, just the alert box. When I retested with the default sound, it played that with the notification. When I replaced the .caf file name with gibberish, it also played the default sound, so it knew my file was there just did not play it. When I run it in the iOS 4.3 simulator it does play my .caf sound. I am losing it. Any ideas anyone?
Greg
-

Simulator != Device especially when you play media files, as your Mac supports more codecs than iPhones do. Checking that your caf file is encoded the way the iPhone needs it and correcting any issues with encoding may solve the problem.

Related

Why are my local notifications not having sound on by default in iOS 7?

My app uses geofencing and sends a notification.
By default, sounds are off in Settings - Notifications for the app.
iOS 7 to be precise.
Does anyone know how to fix this?
Here is the code responsible for this:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeInterval:5 sinceDate:[NSDate date]];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:#"You are near %#. Don't forget to check in!",place.name];
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
If the sound is turned off for your app in the Settings app, then your notification will not play any sound. The user does not allow you to do that.
If the sound is not turned off in the Settings app, that code that you posted should work.
Also if the user set the ringer switch off, then there will be no sound played.
Have you set the applicationIconBadgeNumber in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions?
Comment this code, and try again.....I don't why, but I got the same problem. After I commented this line, my app works correctly.

how to start to play music in locked screen in iPhone

I am using UILocalNotification to alert the user if app not running and MPMusicPlayerController to play iPod music and MPMoviePlayerController to play radio stream url.
According to Apple doc:
if iphone is locked and device receives local notification then it will play 30 second sound and when user slides the slider then it will take app in foreground and two scenarios take place
If App not running then calls method
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
If App already running then calls method -
(void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notify
and i can perform my task in these methods.
Problem :
I need my application to be able to play my sound (it may be iPod music or stream url but not local notification sound for 30 sec) when the iPhone is locked and local notification received and app is running in foreground.
Reference App :
"Alarm Clock HD" and other apps also available in app store are working fine in this way but these are not able to play alarm when app is running in background (i could not understand why???)
I also tried how-to-prevent-iphone-from-deep-sleeping :
which prevent the iPhone from deep sleep by playing silent music periodically but how to notify app that notification has received so start to play "actual music" ? And how to start to play new music?
#manju sorry i got late but it work for you.
selectedDate = [timePicker date];
it is selectedDate date object,at this time your notification will came.
[NSDate date] by this you can find current time. and find different both time via.
NSTimeInterval timeDifference = [ selectedDate timeIntervalSinceDate:[NSDate date]];
Now when your are schedule your notification then .write this code
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask = 0;
NSTimer *backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:timeDifference target:self selector:#selector(backgroundTask) userInfo:nil repeats:NO];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
-(void)backgroundTask
{
NSLog(#"Here you can play song via player ");
}
Don't forgot to add in app.infolist
UIApplication BackgroundMode Audio
it will solve your problem , play song when device is lock.
use AVAudioSessionCategoryPlayback for audio session. Check audio session programming guide here

alarm iphone sdk

I'm trying to make alarm in my app. I made UILocalNotification and it works fine. But i need to set a mp3 song as a sound of my notification and it is not working
Here is some code
NSString *filePath = [DOCUMENTS stringByAppendingPathComponent:#"personal.mp3"];
//NSLog(#"%#",filePath);
UILocalNotification *localNotif = [[UILocalNotification alloc]init];
localNotif.fireDate = alarmPicker.date;
localNotif.timeZone = [NSTimeZone localTimeZone];
localNotif.alertBody = #"Alarm";
localNotif.alertAction = #"View";
localNotif.soundName = filePath;
localNotif.applicationIconBadgeNumber = 1;
//localNotif.repeatInterval = NSYearCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
Should I add framework or what i should do? right now it is not playing my mp3. Thanks
UILocation notification does not support .mp3 these are the files supported
Because custom alert sounds are played by the iOS system-sound
facility, they must be in one of the following audio data formats:
Linear PCM MA4 (IMA/ADPCM) µLaw aLaw You can package the audio data in
an aiff, wav, or caf file. Then, in Xcode, add the sound file to your
project as a nonlocalized resource of the application bundle.
Please see the answer for this question link
Also see this Preparing Custom Alert Sounds
EDIT :ADDING ANSWER FOR DURATION
You can inspect a sound to determine its data format by opening it in
QuickTime Player and choosing Show Movie Inspector from the Movie
menu.
Custom sounds must be under 30 seconds when played. If a custom sound
is over that limit, the default system sound is played instead.
You can use Audio editor/converter to trim your song
Switch
WavePad
Audacity

Local Notification doesn't work on iOS5

After configuring everything in notification center, which allows the app to display the notification, my app's local notification doesn't fire.
Do you encounter the same problem?
more information:
The same app compiled from the same source code a few days ago, which compiled with XCode 4.1 and iOS 4.3 SDK, everything works well.
In addition, the app compiled with old version XCode and iOS SDK, can work on iOS5, after upgrade.
However, the app which compiled with the same code, but XCode 4.2 and iOS5 SDK doesn't work.
Do you have any ideas?
Or is there any special work for iOS5?
The sample code is like:
UIApplication *app = [UIApplication sharedApplication];
NSArray *oldNotifications = [app scheduledLocalNotifications];
// Clear out the old notification before scheduling a new one.
if (0 < [oldNotifications count]) {
[app cancelAllLocalNotifications];
}
// Create a new notification
UILocalNotification *alarm = [[UILocalNotification alloc] init];
if (alarm) {
alarm.fireDate = theDate;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = NSDayCalendarUnit; //repeat every day
alarm.alertBody = [NSString stringWithFormat:#"alert"];
[app scheduleLocalNotification:alarm];
[alarm release];
}
Thanks,
Michael
In iOS 5, notifications are managed by Notification Center. You have to register your application with the Notification Center (programmatically), or (non-programmatically) go to Settings > Notifications and select appropriate settings i.e. enable Notification Center, select Alert Style, and others.
You can use following piece of code to register your application with Notification Center (programmatically), by putting it in applicationDidFinishLaunching::
// Although Register For Remote Notifications is not required for Local Notifications,
// but in iOS 5's Notifications, we have to register otherwise the system doesn't register/recognize
// the notifications posted from the application. Note that this behavior is not documented
// as of Oct 2011, and it's possible that it's a bug and will be handled in the future releases.
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
HTH.

UILocalNotification iOS5 issue (no alert shown)

Is it only me or did Apple break the UILocalNotification mechanism in iOS5 (GM + public release)? It seemed to work fine in the betas but since GM only the sound plays back, no alert is shown (yes I have checked the Notification Settings and the app is all on ON).
I've also made a small separate project (with a brand new app identifier just to be on the safe side), and tested the UILocalNotification class with some very simple code, on the order of:
UILocalNotification *singleLocalPush = [[UILocalNotification alloc] init];
singleLocalPush.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
singleLocalPush.hasAction = YES;
singleLocalPush.alertBody = #"Alert Body";
singleLocalPush.alertAction = #"Alert Action";
[[UIApplication sharedApplication] scheduleLocalNotification:singleLocalPush];
anyone experience similar problems? This is pretty annoying if your app IS about local notifications :)
Best,
Kacper
You should make sure your app is enabled for notifications-
Navigate to Setting->Notifications and make sure your app is enabled.
My app's local notifications work fine on iOS 5, and it was set to be deployed to 3.1.2.