Question

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?

Was it helpful?

Solution

You have specified your repeat interval to NSDayCalendarUnit. So, your notification will be fire but at next day at specified time.

For testing purpose change your this repeat interval and check your code is working properly.

I have tested. Your code is working properly here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top