Currently, I plan to receive local notifications once a day at 6 pm if the user has not yet opened the application that day. If the user has already downloaded the application, I want to cancel the notification for this day and plan a new one for tomorrow at 6 pm. The notification displays correctly, however, when I try to iterate through the list of scheduled notifications (this is not the only local notification that I have in the application), the [[UIApplication sharedApplication] scheduleLocalNotifications] array is always empty. The following is a snippet of code that is causing me problems:
// See if we need to create a local notification to tell the user that they can receive a daily reward NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; // <- This is always empty // Iterate over all the pending notifications for( int iter = 0; iter < [notifications count]; iter++ ) { UILocalNotification* localNotification = [notifications objectAtIndex:iter]; if( [[localNotification.userInfo objectForKey:@"source"] isEqualToString:@"dailyReminder"] ) [[UIApplication sharedApplication] cancelLocalNotification:localNotification]; } NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; [calendar setTimeZone:[NSTimeZone localTimeZone]]; NSDateComponents *nowComponents = [calendar components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSSecondCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:serverDate]; int hour = [nowComponents hour]; int minute = [nowComponents minute]; int second = [nowComponents second]; int hoursToAdd = 18 - hour; NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease]; [comps setDay:1]; [comps setHour:hoursToAdd]; [comps setMinute:-minute]; [comps setSecond:-second]; NSDate *tomorrow6PM = [calendar dateByAddingComponents:comps toDate:serverDate options:0]; NSMutableDictionary* userInfo = [NSMutableDictionary dictionary]; [userInfo setObject:@"dailyReminder" forKey:@"source"]; scheduleNotification(tomorrow6PM, NSLocalizedString(@"Come Back!", @"daily reminder notification message"), NSLocalizedString(@"Launch", @"daily reminder notification button text"), [UIApplication sharedApplication].applicationIconBadgeNumber+1, userInfo);
The schedNotification function is simple and just creates a local notification:
void scheduleNotification(NSDate* fireIn, NSString* bodyText, NSString* alertText, NSUInteger badgeCount, NSMutableDictionary* userInfo) { static int appCount = 0; appCount += 1; if(!isLocalNotificationEnabled()) return; Class localNotificationClass = NSClassFromString(@"UILocalNotification"); UILocalNotification* localNotification = [[localNotificationClass alloc] init]; if(!localNotification) return; localNotification.fireDate = fireIn; localNotification.timeZone = [NSTimeZone systemTimeZone]; localNotification.alertBody = bodyText; localNotification.alertAction = alertText; localNotification.soundName = UILocalNotificationDefaultSoundName; localNotification.applicationIconBadgeNumber = badgeCount; localNotification.userInfo = userInfo; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; [localNotification release]; }
I do not understand why the local notification array is always empty. As I said before displaying notifications at the right time, so they are planned. Any help is appreciated.
source share