4

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]; 
}
user2533604
  • 655
  • 1
  • 11
  • 28
  • 2
    use repeatinterval property of localnotification localNotification.repeatInterval = NSCalendarUnitDay;you also get help http://stackoverflow.com/questions/23748062/ios-daily-local-push-notifications – kamalesh kumar yadav Jul 30 '14 at 06:33

1 Answers1

15

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]; 
}
Sviatoslav Yakymiv
  • 7,887
  • 2
  • 23
  • 43
  • @[Sviatoslav Yakymiv] it means notification will be triggered at 12:00 daily? – Nitesh Apr 04 '17 at 04:21
  • @Nitesh yes. It will – Sviatoslav Yakymiv Apr 04 '17 at 04:44
  • How can I repeat notification based on different times in my array to set automatically notify me daily on given time in array when the date is the index of an array I have a moths array – Xcodian Solangi Apr 16 '18 at 11:21
  • @SviatoslavYakymiv What if I have hour array and minute array and want to repeat daily Have a look at this question plz https://stackoverflow.com/questions/49857236/trigger-local-notifications-automatically-daily-on-dynamic-time-given-in-arrays – iOS Developer Apr 17 '18 at 05:14