iPhone - play alarm sound while the app in background - iphone

Do you know how to play an alarm sound when the iPhone is sleeping,
like the built-in Clock app in iPhone?
VERY IMPORTANT EDIT:
in the built-in Clock app in iPhone
when the alarm sound is playing, if user switch the Silent switch to Silent (Vibrate mode),
the alarm sound still continue to play.
Do you know how to do the same?

this code will help you to play music in background:
the sound file of the music should be only 30 sec
notification support 30 sec sound file and it should be in the bundle folder if it is in the document folder then you have to put the path of the sound file
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSString *dateValueRemaining =[NSString stringWithFormat:#"23/08/2012 11:30:33"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSDate *dateRemaining = [dateFormat dateFromString:dateValueRemaining];
NSDate *pickerDate = dateRemaining;
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit
| NSMonthCalendarUnit
| NSDayCalendarUnit)
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit
| NSMinuteCalendarUnit
| NSSecondCalendarUnit)
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
NSLog(#"itemDate: %#", itemDate);
if (localNotif) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotif];
}
localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) {
return;
}
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Your Time is Over";
localNotif.alertAction = #"View";
//---THIS SONG FILE IN THE NSBUNDLE FOLDER---------//
localNotif.soundName = #"s1.mp3";
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

The set button is wired up to run a method called scheduleNotification in the view controller which uses the UILocalNotification class to schedule a notification. The code looks as follows:
(void)scheduleNotification
{
[reminderText resignFirstResponder];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil)
{
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Did you forget something?";
notif.alertAction = #"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}

Before the app enters the background, play a silent sound with AVAudioPlayer and keep it play infinite number of times. And when your notification kicks in, again use AVAudioPlayer to play your notification sound, not the UILocalNotification object. It worked for me.

So looking at i-qi app, most likely they've set their UIBackgroundModes to "audio" in their info.plist. This means that they're able to play audio in the background. They're probably playing silent audio until the timer ends because background audio sessions will get cut if they're not in use.
In terms of the silent switch, thats probably based on the session category they're using. Check out this resource:
http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html#//apple_ref/doc/uid/TP40007875-CH4-SW1

Only method is to use local Notifications or Push notifications..both allow sounds to be played for 30 seconds

Related

Local Notification Not showing up when app opens

I have a local notification set up. Once its fired, lets say my app is in "sleep" mode that is home button pressed or i quit it. Now the notification is received. I see red tag on my app and when I click on the app, it should fire didReceiveLocalNotification but it does't. How can I make it do that when I open my app?
The didReceiveLocalNotification method is only called when your application is running in the foreground. If you see a badge and click on the App to start it, then you need to process the local notification using application:willFinishLaunchingWithOptions: (or application:didFinishLaunchingWithOptions:) To get at your local notification in either of these two methods, use UIApplicationLaunchOptionsLocalNotificationKey as a key to the options dictionary.
[edit] From the UILocalNotification documentation:
if the local notification only badges the application icon, and the
user in response launches the application, the
application:didFinishLaunchingWithOptions: method is invoked, but no
UILocalNotification object is included in the options dictionary.
-(void)alarmNotification
{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *newdate = timePicker.date;
NSLog(#"pickerdate:%#",newdate);
NSDate *pickerDate =[newdate dateByAddingTimeInterval:-60*1];
NSLog(#"pickerdate:%#",timePicker.date);
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = txtMeetingName.text;
// Set the action button
localNotif.alertAction = NSLocalizedString(#"View!", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
localNotif.userInfo = infoDict;
localNotif.applicationIconBadgeNumber =1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Then implement this code......
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
// Handle the notificaton when the app is running
NSDateComponents *comps = [[NSCalendar currentCalendar] components:notif.repeatInterval fromDate:notif.fireDate toDate:[NSDate date] options:0];
// you can set your own sound
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/win.wav", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
UIAlertView *alertView =
[[UIAlertView alloc] initWithTitle:NSLocalizedString(#"move on", nil)
message:[NSString stringWithFormat:#"You have to start %# meeting within 5 minute",notif.alertBody]
delegate:self
cancelButtonTitle:nil
otherButtonTitles:NSLocalizedString(#"OK", nil), nil];
notif.soundName = UILocalNotificationDefaultSoundName;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog(#"error%#",[error description]);
else
[audioPlayer play];
[alertView show];
[alertView release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex{
if (buttonIndex==0) {
[audioPlayer stop];
}
NSLog(#"U HAVE CLICKED BUTTON");
}
hope this will help you.....
When you are setting fire date then there have you set the userInfo property of your notification object???
& r u checking it on device or on simulator??

Auto increment applicationIconBadgeNumber when application quits

Programmers, I am new to objective C.
I am working on local notifications task and my task is that when user quits the applications and he doesn't run application again, suppose for one day, then the app icon badge number should auto increment to one. If it doesn't run for 48 hours, then it should increment and become 2 (means I have to repeat notification) and so on.
I am using local notifications for that. This how I am doing it:
- (void)applicationWillResignActive:(UIApplication *)application
{
[self launchNotification];
}
-(void)launchNotification
{
NSDate *todaydate = [NSDate date];
NSDate *firedates = [todaydate dateByAddingTimeInterval:10.0];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = firedates;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit)
fromDate:firedates];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]+10];
localNotif.repeatInterval = NSSecondCalendarUnit;
localNotif.soundName = UILocalNotificationDefaultSoundName;
// Specify custom data for the notification
//NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
//localNotif.userInfo = infoDict;
// Schedule the notification
localNotif.applicationIconBadgeNumber = +1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
I am working from long time and finding no way out.

firing a local notification on everyday from time selected from picker

In my app i want to set up a local notification on everyday at a particular time.Time is the one that selected from a Time picker.Is there any method for this.Plese help me.
Just specify the firedate as the date from your UIDatePicker and NSDayCalendarUnit as the repeatinterval:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = datePicker.date;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"This is the Alert-Body Text"];
localNotif.alertAction = #"Button-Text";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
you can set Local Notifications.
Right out of apple's sample code:
- (void)scheduleNotificationWithItem:(ToDoItem *)item interval:(int)minutesBefore {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:item.day];
[dateComps setMonth:item.month];
[dateComps setYear:item.year];
[dateComps setHour:item.hour];
[dateComps setMinute:item.minute];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(#"%# in %i minutes.", nil),
item.eventName, minutesBefore];
localNotif.alertAction = NSLocalizedString(#"View Details", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:item.eventName forKey:ToDoItemKey];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
you can set a local notification for a specific timstamp.
you have to check userinfo dictionary in applicationDidFinishedLaunching for any notification data.
you have to handle:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

UILocalNotification Scheduling an Alert

I schedule a Notification, and give it 60 minutes warning before it should display an alert message...
As soon as I add the notification the method in my App Delegate is called:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
How can I know that it will show up in the background as an alert? Is there any other Delegate method I need to override or use in order to ensure that given a scheduled Alert with a 60 Minute Interval...
- (void)scheduleNotificationWithItem:(NSDate *)item interval:(int)minutesBefore
{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
//NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *currentDateComponents = [calendar components:( NSWeekdayCalendarUnit |
NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSWeekCalendarUnit | NSMinuteCalendarUnit) fromDate:item];
NSLog(#"- current components year = %i , month = %i , week = % i, weekday = %i", [currentDateComponents year], [currentDateComponents month], [currentDateComponents week], [currentDateComponents weekday]);
NSLog(#"[currentDateComponents minute]: %i", [currentDateComponents minute]);
NSLog(#"[currentDateComponents hour]: %i", [currentDateComponents hour]);
NSLog(#"[currentDateComponents day]: %i", [currentDateComponents day]);
NSLog(#"[currentDateComponents week]: %i", [currentDateComponents week]);
NSLog(#"[currentDateComponents month]: %i", [currentDateComponents month]);
NSLog(#"[currentDateComponents year]: %i", [currentDateComponents year]);
[dateComps setDay: [currentDateComponents day]];
[dateComps setMonth:[currentDateComponents month]];
[dateComps setYear:[currentDateComponents year]];
[dateComps setHour:[currentDateComponents hour]];
[dateComps setMinute:[currentDateComponents minute]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:#"%#\n%#",
streetAddress,
stringOfWhenAuctionIsOn];
localNotif.alertAction = NSLocalizedString(#"View Details", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:streetAddress
forKey:idOfStreetAlert];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
If the notification shows up right away there probably is something wrong with your fireDate property. It looks like you are trying to verify that the date sent is correct but you should also verify that that fireDate is what you expect.
Also, you could perhaps do
localNotif.fireDate = [item dateByAddingTimeInterval:-60*minutesBefore];
to achieve the same effect and don't have to mess around with NSDateComponents. I have not tested this but it might work.
As for what happens when the timer fires and your app is not running. If the app has not been terminated application:didReceiveLocalNotification: will get called. If on the other side your app has been terminated you need to check in application:didFinishLaunchingWithOptions: as pointed out by #Ishu. I usually just pass the notification to a common method to avoid duplicating the code.
No delegate method for background, you need to write code in didFinishLaunchingWithOptions,
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
//your code
NSLog(#"Recieved Notification %#",localNotif);
}
it create your logic when app get notification from background.Dont worry app get notification ones you set the notification.

how to use UILocationNotification in iPad?

In my universal application I m using the below code for UILocalNotification
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
NSString *str = [txtfield_MyPills.text stringByAppendingString:#" "];
NSString *str_Dosage = [txtfield_Dosage.text stringByAppendingString:#" "];
NSString *str_Frequency = [txtfield_Frequency.text stringByAppendingString:#" "];
NSString *str1 = [str stringByAppendingString:str_Dosage];
NSString *strResult = [str1 stringByAppendingString:str_Frequency];
localNotif.alertBody =strResult;;// [txtfield_MyPills text];//[eventText text];
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
Its workin fine in iphone but if I run my application for iPad it shows that UilOcation is undeclared?
Please help me out and If u have sample code for UILocalNotification for iPad please give link for that
Thanks in Advance
not available in 3.2 iOS
http://developer.apple.com/iphone/library/documentation/iphone/Reference/UILocalNotification_Class/Reference/Reference.html
only available in 4.0 or later
As Aron said Uiloaclnotifications are not available in 3.2.
As you might know the new version of iOs for iPad is coming very soon.
Asoming that it will take time until all users will upgrade you can do that -
Try this-
Class notifs = (NSClassFromString(#"UILocalNotification));
If (notifs != nil){
//the notification code here
}
I was informed by apple support that you can test that only on real devices that run 3.2 and not on the simulator.
You can look here in the mail composer code example at apple to see how they implemented that:
https://developer.apple.com/iphone/library/iPad/index.html#samplecode/MailComposer/Listings/Classes_MailComposerViewController_m.html
Hope it will help
Shani