问题
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