SRMR
SRMR

Reputation: 3134

Scheduled Local Notifications showing up when toggled ON for time period when toggled OFF

I have a daily scheduled notification at 8:30:01 PM.

When the Notifications are toggled OFF in Settings app for a week, no notifications show up, which is perfect.

The problem is when Notifications are toggled back ON in Settings app after that week, all of the Notifications for the previous week show up.

I was wondering how to get Notifications to not "build up" so to speak.

Is there a line of code I'm missing in here to "clear them out"?

ViewController.m:

- (void)viewDidLoad {
    [super viewDidLoad];

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

    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comp = [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
    comp.hour = 20; // 19 = 7PM  20=8pm
    comp.minute = 30; // 7:45 PM  8:30
    comp.second = 01; // 7:45:01 PM

    localNotification.fireDate = [cal dateFromComponents:comp];
    localNotification.alertBody = @"Local Notification in iOS8";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.repeatInterval = NSCalendarUnitDay;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

*These are Notifications that show from when Notifications was OFF, as soon as I toggle Notifications back ON:*

enter image description here

Upvotes: 2

Views: 320

Answers (1)

David Ansermot
David Ansermot

Reputation: 6112

When you set the notifications to OFF, you should call [[UIApplication sharedApplication] cancelAllLocalNotifications];

Upvotes: 1

Related Questions