Multiple Notifications Not Firing
- by motionpotion
I'm scheduling two notifications as shown below. The app is a long-lived app. One local notification is scheduled to run every hour. The other is scheduled to run once per day. Only the second scheduled notification (the hourly notifcation) fires.
- (void)scheduleNotification
{
LogInfo(@"IN scheduleNotification - DELETEYESTERDAY NOTIFICATION SCHEDULED.");
UILocalNotification *notif = [[UILocalNotification alloc] init];
NSDictionary *deleteDict = [NSDictionary dictionaryWithObject:@"DeleteYesterday"
forKey:@"DeleteYesterday"];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
[components setDay: day];
[components setMonth: month];
[components setYear: year];
[components setHour: 00];
[components setMinute: 45];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone systemTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
notif.fireDate = dateToFire;
notif.timeZone = [NSTimeZone systemTimeZone];
notif.repeatInterval = NSDayCalendarUnit;
notif.userInfo = deleteDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
and then I schedule this after above:
- (void)scheduleHeartBeat
{
LogInfo(@"IN scheduleHeartBeat - HEARTBEAT NOTIFICATION SCHEDULED.");
UILocalNotification *heartbeat = [[UILocalNotification alloc] init];
NSDictionary *heartbeatDict = [NSDictionary dictionaryWithObject:@"HeartBeat"
forKey:@"HeartBeat"];
heartbeat.userInfo = heartbeatDict;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
[components setDay: day];
[components setMonth: month];
[components setYear: year];
[components setHour: 00];
[components setMinute: 50];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone systemTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
heartbeat.fireDate = dateToFire;
heartbeat.timeZone = [NSTimeZone systemTimeZone];
heartbeat.repeatInterval = NSHourCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:heartbeat];
}
The above are scheduled when the app launches in the viewDidLoad of the main view controller.
- (void)viewDidLoad
{
[self scheduleNotification];
[self scheduleHeartBeat];
[super viewDidLoad];
//OTHER CODE HERE
}
Then in the appdelegate I have the following:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
LogInfo(@"IN didReceiveLocalNotification NOTIFICATION RECEIVED.");
NSString *notificationHeartBeat = nil;
NSString *notificationDeleteYesterday = nil;
application.applicationIconBadgeNumber = 0;
if (notification) {
notificationHeartBeat = [notification.userInfo objectForKey:@"HeartBeat"];
notificationDeleteYesterday = [notification.userInfo objectForKey:@"DeleteYesterday"];
LogInfo(@"IN didReceiveLocalNotification HEARTBEAT NOTIFICATION TYPE: %@", notificationHeartBeat);
LogInfo(@"IN didReceiveLocalNotification DELETEYESTERDAY NOTIFICATION TYPE: %@", notificationDeleteYesterday);
}
if ([notificationHeartBeat isEqualToString:@"HeartBeat"]) {
//CREATE THE HEARTBEAT
LogInfo(@"CREATING THE HEARTBEAT.");
//CALL THE FUNCTIONALITY HERE THAT CREATES HEARTBEAT.
}
if ([notificationDeleteYesterday isEqualToString:@"DeleteYesterday"]) {
//DELETE YESTERDAYS RECORDS
LogInfo(@"DELETING YESTERDAYS RECORDS.");
}
}
The notification that is scheduled last (scheduleHeartBeat) is the only notification that is fired.
Could somebody help me figure out why this is happening?