Sam B
Sam B

Reputation: 27598

how to get UILocalNotification repeat on certain days

I am making a alarm clock app that fires a local notification on a certain user selected day i.e. if user selected M, F, Sat then it should fire an alarm every M/F/Sat only. The problem that I have is that my bloody alarm is firing every single day at that time. How can I restrict it to user selected days? Here is my code

//For testing purposes I want the alarm to fire only on Monday at 7:00am.
//This fires every day at 7:00am
[[UIApplication sharedApplication] cancelAllLocalNotifications];

 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *now = [NSDate date];    

    // set components for time 7:00 a.m. Monday
    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit |   NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];

    [componentsForFireDate setWeekday: 2] ;
    [componentsForFireDate setHour: 7] ;
    [componentsForFireDate setMinute:0] ;
    [componentsForFireDate setSecond:0] ;

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];

    // Create the notification
    UILocalNotification *notification = [[UILocalNotification alloc]  init] ;

    notification.fireDate = fireDateOfNotification ;
    notification.timeZone = [NSTimeZone localTimeZone] ;
    notification.alertBody = [NSString stringWithFormat: @"Wake up!"] ;
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Waking Up time"] forKey:@"wakeUp"];

//Problem is here NSDayCalendarUnit makes my alarm fire every day
    notification.repeatInterval= NSDayCalendarUnit ;

    //notification.soundName=UILocalNotificationDefaultSoundName;
    notification.soundName=@"alarm-clock-ringing-01.wav";

    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

Upvotes: 0

Views: 3275

Answers (2)

Zhang
Zhang

Reputation: 11607

Would it work if you actually make the local notification fire only once but when your device receives the notification, make it post the same notification again. Your app does not need to be running for this to work.

You handle the local notification when your application is not running, in the applicationDidFinishLaunching of your app delegate:

UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

// handles notification when application is relaunched after being terminated
// not when app is already in the foreground running.
if(localNotif)
{   
    // repeat the same local notification again
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

Note: repeatInterval will perform the same action by the unit you specified (in this case "day", will repeat every day regardless of what you specified as the date of the notification)

Upvotes: 1

simonbs
simonbs

Reputation: 8042

You could make three alarms: one for the closest Monday, closest Wednesday and closest Friday and set notification.repeatInterval = NSWeekCalendarUnit on each of them.

Upvotes: 0

Related Questions