Everything has changed since then, but I would like to share my solution to this problem. (Sorry for the names of long variables ...)
The idea is simple: always keep firedate in the future.
- this time the FinishLaunchingWithOptions or didReceiveLocalNotification function is called, just cancel your current notification and transfer the new one using the FireDate time unit in the future
-When your application starts iterating over all scheduled notifications, if firedate will not be in the future, you know that it was ignored
In my case, notifications have a weekly repetition interval. I first transfer any confirmed notifications to the didFinishLaunchingWithOptions file:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UILocalNotification* localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (localNotif != nil) { [NotificationsHelper rescheduleNotification:localNotif]; } }
And also in didReceiveLocalNotification:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *) notification { [NotificationsHelper rescheduleNotification:notification]; }
In App Launch, I check all notifications for anyone with FireDate in the past:
- (void)applicationDidBecomeActive:(UIApplication *)application { [self checkLocalNotifications:application]; }
Code for my function "checkLocalNotifications":
- (void) checkLocalNotifications:(UIApplication *) application { UIApplication* app = [UIApplication sharedApplication]; NSArray* eventArray = [app scheduledLocalNotifications]; for (int i = 0; i < [eventArray count]; i++) { UILocalNotification* notification = [eventArray objectAtIndex:i]; if ([NotificationsHelper wasWeeklyRepeatingNotificationIgnored:notification]) { [NotificationsHelper rescheduleNotification:notification]; NSLog(@"NotificationWasIgnored: %@ %@",notification.alertAction, notification.alertBody ); } } }
The code for my function isWeeklyRepeatingNotificationIgnored:
+ (BOOL) wasWeeklyRepeatingNotificationIgnored:(UILocalNotification*) the_notification { BOOL result; NSDate* now = [NSDate date];
Code for my rescheduleNotification function:
+ (void) rescheduleNotification:(UILocalNotification*) the_notification { UILocalNotification* new_notification = [[UILocalNotification alloc] init]; NSMutableDictionary* userinfo = [[NSMutableDictionary alloc] init]; [new_notification setUserInfo:userinfo]; [new_notification setRepeatInterval:the_notification.repeatInterval]; [new_notification setSoundName:UILocalNotificationDefaultSoundName]; [new_notification setTimeZone:[NSTimeZone defaultTimeZone]]; [new_notification setAlertAction:the_notification.alertAction]; [new_notification setAlertBody:the_notification.alertBody]; [new_notification setRepeatCalendar:[NSCalendar currentCalendar]]; [new_notification setApplicationIconBadgeNumber:the_notification.applicationIconBadgeNumber]; NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents* weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:the_notification.fireDate]; NSInteger weekday = [weekdayComponents weekday]; NSDate* next_week = [self addDay:weekday toHourMinute:the_notification.fireDate]; [new_notification setFireDate:next_week]; [[UIApplication sharedApplication] scheduleLocalNotification:new_notification]; [[UIApplication sharedApplication] cancelLocalNotification:the_notification]; }