Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule UILocalNotification everyday at specific time

I am using below piece of code to schedule a localnotification at specific time. But the notification is repeating for every minute instead of after one day. Whether i have missed any notification settings. My timezone is (Asia/Calcutta (IST) offset 19800) and using iPhone 4s.

- (void)applicationDidEnterBackground:(UIApplication *)application
{     
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
    NSDate *now = [NSDate date];
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:now]; 
    [components setHour:12];
    [components setMinute:00];

    UILocalNotification *notification = [[UILocalNotification alloc]init];
    notification.fireDate = [calendar dateFromComponents:components];
    [notification setAlertBody:@"U got notification!!!"];
    // notification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
}
like image 586
user2533604 Avatar asked Dec 06 '22 01:12

user2533604


1 Answers

You should specify repeatInterval.

- (void)applicationDidEnterBackground:(UIApplication *)application
{     
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
    NSDate *now = [NSDate date];
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:now]; 
    [components setHour:12];
    [components setMinute:00];

    UILocalNotification *notification = [[UILocalNotification alloc]init];
    notification.fireDate = [calendar dateFromComponents:components];
    notification.repeatInterval = NSDayCalendarUnit;
    [notification setAlertBody:@"U got notification!!!"];
    // notification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
}
like image 198
Sviatoslav Yakymiv Avatar answered Dec 31 '22 06:12

Sviatoslav Yakymiv