NSCalendarUnitMinute is working but NSCalendarUnitDay is not working with UILocalNotification repeat interval

Hi readers,


My app has a daily reminder feature, but it only works for the first time but it's not repeating please have a glance at my code which is written below.


In Appdelegate:


UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:nil];

[[UIApplication sharedApplication] registerForRemoteNotifications];

[[UIApplication sharedApplication] registerUserNotificationSettings:uns];


In ViewController:


NSDateComponents *comps = [[NSDateComponents alloc] init];

[comps setYear:2017];

[comps setMonth:01];

[comps setDay:01];

[comps setHour:16];

[comps setMinute:05];

[comps setSecond:0];

[comps setTimeZone:[NSTimeZone localTimeZone]];

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

NSDate *setTime = [cal dateFromComponents:comps];

UILocalNotification* localNotification = [[UILocalNotification alloc] init];

localNotification.fireDate = setTime;

localNotification.alertBody = @"It’s time to practise your speech sounds for the day";

localNotification.timeZone = [NSTimeZone defaultTimeZone];

localNotification.repeatInterval = NSCalendarUnitDay;

localNotification.soundName = UILocalNotificationDefaultSoundName;


localNotification.applicationIconBadgeNumber =1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];//Rajai


Guys Please help me out with this,I don't even know where I'm stuck.

Replies

Have you looked at the UserNotifications framework? UILocalNotification has been deprecated in favour of it and, more importantly, it has a much nicer API for repeated notifications.

I’m not 100% sure why your current code is failing, but I have some things that you should fix:

  • In C-based languages numbers starting with zero are interpreted at octal. So,

    010
    is not 10, it’s 8. Thus, it’s best to avoid leading zeroes (not that they’ve causing problems in this specific case).
  • You have a time zone mixup. Your date component code is using

    +localTimeZone
    but your local notification code is using
    +defaultTimeZone
    .
  • For that matter, there’s no need to set a time zone in the date components. The date components are interpreted by the calendar, and the calendar defaults to local time.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"