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

主宰稳场 提交于 2020-01-24 12:03:29

问题


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:*


回答1:


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



来源:https://stackoverflow.com/questions/29956882/scheduled-local-notifications-showing-up-when-toggled-on-for-time-period-when-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!