6

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?

daSn0wie
  • 879
  • 1
  • 8
  • 15

3 Answers3

7

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.

MyCSharpCorner
  • 1,313
  • 11
  • 15
  • this isn't correct. you can implement the same sound as many times as you like in successive order at 30 second intervals. the person writing the app needs to clear out any successive notifications that are set once the user engages. – daSn0wie Feb 06 '12 at 15:08
  • When the system presents your notification while your app is terminated or is suspended in the background, there are two options: "Close" and another button. Tapping the Close button does not notify your app so you can't clear any successive notifications like you say. As a result, the user will keep getting those 30 second alarms you scheduled even though they choose Close every time. In my opinion this a pretty confusing experience for the user. – MyCSharpCorner Feb 07 '12 at 18:40
  • you can bring the user back to your app at which point you execute:[[UIApplication sharedApplication] cancelAllLocalNotifications]; – daSn0wie Feb 21 '12 at 14:50
  • how do you bring the user back to your app after they tap the Close button from within the UILocalNotificationPopup? – MyCSharpCorner Feb 23 '12 at 16:42
  • http://stackoverflow.com/questions/7507189/iphone-uilocalnotification-load-specific-view – daSn0wie Feb 25 '12 at 04:21
  • 2
    Did you read the link you posted? It says to go back to the app, you have to tap View button. We are talking about Close button here. There is no way you can go back to the app if the user taps Close from the local notification popup. – MyCSharpCorner Mar 04 '12 at 06:16
1

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 
{ 
}
jigneshbrahmkhatri
  • 3,627
  • 2
  • 21
  • 33
  • how do you keep the application 'alive'? when i leave my app running and go to sleep, the sound only plays for 30 seconds. The alarm clock app plays the sound until i actually wake up and do a user action. – daSn0wie Jul 01 '11 at 17:05
-2

So I think I found a valid solution:

To simulate the alarm sound playing for more than 30 seconds, just add multiple localnotifications one after the other, 30 seconds apart.

daSn0wie
  • 879
  • 1
  • 8
  • 15
  • 1
    This 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. – MyCSharpCorner Jan 31 '12 at 09:15
  • That needs to be handled by the app. once the alert is selected, you need to cancel out of all the other localnotifications – daSn0wie Feb 03 '12 at 21:01
  • This is not the right answer as @MyCSharpCorner and Hardik Shah have already stated. daSnOwie if you do your research, may alarms dont pop-up a notification window. Or if they did and you would dismiss the notification, rather than accepting it, the app would never have the chance to run. I have [a tread on this too.] (http://stackoverflow.com/questions/9725192/how-do-i-start-playing-audio-when-in-silent-mode-locked-in-ios-6) – Andres Canella Oct 08 '12 at 02:55