1

I'm repeating a Local Notification at the same time every day.

Right now everything is working fine, except when Notifications are toggled OFF, then back ON, there is a build up of notifications from the days it was OFF.

Is there a best way to check if toggled OFF then back ON, and clear all old notifications?

Thanks!

- (void)viewDidLoad {
    [super viewDidLoad];

    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

    // before we create any new ones, cancel all existing notifications
    [[UIApplication sharedApplication]cancelAllLocalNotifications];

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

    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comp = [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
    comp.hour = 19; // 19 = 7PM
    comp.minute = 45; // 7:45 PM
    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;

    // this will schedule the notification to fire at the fire date
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    // this will fire the notification right away, it will still also fire at the date we set
    [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
SRMR
  • 3,064
  • 6
  • 31
  • 59

2 Answers2

1

Before presenting the notification check if the users settings allow it:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

    // before we create any new ones, cancel all existing notifications
    [[UIApplication sharedApplication]cancelAllLocalNotifications];

    if ([[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone) {

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

        NSCalendar *cal = [NSCalendar currentCalendar];
        NSDateComponents *comp = [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
        comp.hour = 19; // 19 = 7PM
        comp.minute = 45; // 7:45 PM
        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;

        // this will schedule the notification to fire at the fire date
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        // this will fire the notification right away, it will still also fire at the date we set
        [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

    }
}
Dash
  • 17,188
  • 6
  • 48
  • 49
  • Thanks for the response! Just put that line of code before `[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];` and it does the trick? Just checking to make sure I'm not missing anything. – SRMR May 30 '15 at 20:20
  • This checks to make sure notifications are allowed before you bother to create and schedule one. – Dash May 30 '15 at 20:24
  • Oh awesome! I'm getting an error saying `no visible @interface for uiusernotificationsettings declares the selector maypresentusernotificationoftype`, any ideas? – SRMR May 30 '15 at 20:26
  • Weird I guess that method is actually private but Apple accidentally included it in their documentation. I updated the code with an alternative way to do this. – Dash May 30 '15 at 20:35
  • That is weird. Thanks I'll try this out! – SRMR May 30 '15 at 20:43
-2

On toggle on event, try following -

[[UIApplication sharedApplication] cancelAllLocalNotifications];
Avi
  • 2,196
  • 18
  • 18