I'm trying to build an alarm clock similar to the Alarm Clock Pro and the Nightstand application that are currently in the app store. Each of these applications is able to play an alarm clock sound for more than 30 seconds when the alarm time is hit (usually the next morning).
I've tried two approaches already with no luck:
Approach 1:
[self performSelector:@selector(playAlarm) withObject:nil afterDelay:myDouble];
Approach 2:
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate =[datePicker date];//firedate;
notif.timeZone = [NSTimeZone systemTimeZone];
notif.alertBody = @"Time to wake up!";
NSString *SoundFileName=nil;
if([[[NSUserDefaults standardUserDefaults] objectForKey:@"ActualSoundFile"] isKindOfClass:[NSString class]])
SoundFileName=[[[NSString alloc]initWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"ActualSoundFile"]]autorelease];
else
SoundFileName=[[[NSString alloc] initWithString:@""] autorelease];
if([SoundFileName length]>1)
notif.soundName = [SoundFileName stringByAppendingString:@".wav"];
else
notif.soundName = UILocalNotificationDefaultSoundName;
notif.alertAction=@"Snooze";
notif.repeatCalendar=[NSCalendar currentCalendar];
notif.repeatInterval =NSDayCalendarUnit;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"Alarm" forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
Does anyone know how they're able to play the alarm on a loop after 7 hours?
The selected answer is not the right answer, because the user may wake up during the first notification and choose to close it. Guess what, the second notification comes along giving the user the impression that the alarm is broken.
The correct answer according to App docs is as follows:
You can not play a sound more than 30 seconds when your notification arrives while your app is in the background (e.g. user closes the app before going to sleep).
To play a longer sound, you must tell your user to leave the alarm app in the foreground before going to sleep, then in didReceiveLocalNotification you implement playing a longer sound manually.
You need to fire local notification by assigning date into fireDate property, and assign sound file into
UILocalNotification *localNotif = [[[UILocalNotification alloc] init]autorelease];
localNotif.fireDate = scheduleDate;
NSLog(@"fireDate is %@",localNotif.fireDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"WAKE UP...!!!";
localNotif.alertAction = @"View";
localNotif.soundName = @"Default.wav";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
This way, local notification will be fired even if application is closed, remember that "Default.wav" file should be less than or equal to 30 seconds, Even Alarm clock pro app plays sound =30 seconds in local notification.
If application is alive, you can implement delegate method of appdelegate, and can apply your logic to display alert view and play sound even >30 seconds .....
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With