scheduled local notification is not being stored in the scheduledLocalNotification array
Asked Answered
M

3

2

I am currently scheduling local notifications to appear once per day at 6PM if a user has not already opened the app that day. If the user has already loaded the application, then I want to cancel the notification for that day and schedule a new one for tomorrow at 6PM. The notification displays properly, however, when I try to iterate of the list of scheduled notifications (this is not the only local notification I have in the application), the [[UIApplication sharedApplication] scheduledLocalNotifications] array is always empty. Below is the code snippet that is giving me trouble:

// 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 scheduleNotification function is straightforward and just constructs 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 don't understand why the array of local notifications is always empty. Like I said before the notifications display at the correct time, so they are getting scheduled. Any help is appreciated.

Menstrual answered 2/1, 2012 at 23:15 Comment(1)
You ever find a solution for this? I'm getting an empty array as well. Kind of annoying...Strict
H
7

Having similar issues right now. My guess here is that iOS does not schedule the notifications immediately but only at the end of the current run loop. I am running into these problems when setting the scheduledLocalNotifications property several times in the same run loop and changes don't seem to be updated accordingly. I think I will just keep a copy of the local notifications array myself and only set scheduledLocalNotifications and never read it.

Harwill answered 10/7, 2012 at 18:33 Comment(0)
P
1
localNotification.fireDate = fireIn;

in this line check wheather "fireIn" is object of NSDate or NSString.

If it's NSString convert it into NSDate object with help of NSDateformatter and then assing it to localNotification fireDate.

I was facing same problem previously and resolved it with above mentioned steps.

Philistine answered 20/12, 2012 at 12:27 Comment(0)
J
0

if the schedule has invalid fire date, it will not get scheduled and result empty array returned, because there never has a valid one schedule, try to print out your localNotification like below, you may find out the problem.

NSLog(@"Notification--->: %@", localNotification);
Jayson answered 4/1, 2012 at 1:17 Comment(2)
the printout for localNotification seems fine to me, and the notification does fire, it just doesn't exist in the scheduledLocalNotifications array. Here's the printout though: <UIConcreteLocalNotification: 0x9885740>{fire date = Wednesday, January 4, 2012 6:00:00 PM Pacific Standard Time, time zone = America/Los_Angeles (PST) offset -28800, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Wednesday, January 4, 2012 6:00:00 PM Pacific Standard Time}Menstrual
what about insert the debug output scheduledLocalNotifications right after your scheduleNotification function, what it gives?Jayson

© 2022 - 2024 — McMap. All rights reserved.