iCoder4777
iCoder4777

Reputation: 1692

UILocalNotification or Push Notification

In my app I have an array of Friends' birthday and I need to notify user when someone's birthday is coming. Array will have around 200 friends' birthday. In this case,

UILocalNotification will work out or not, as Apple says Each application on a device is limited to the soonest-firing 64 scheduled local notifications. If yes, how I have to implement UILocalNotication.

I don't want to go for PushNotification. Any suggestions would be appreciated.

I'm scheduling Local Notification like this:-

-(void) scheduleNotification{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [self.notifications removeAllObjects];
    for (int i = 0; i< [delegate.viewController.contactList count] ; i++) {
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        NSString *name = [[delegate.viewController.contactList objectAtIndex:i]objectForKey:NAME_KEY];
        NSString *birthday = [[delegate.viewController.contactList objectAtIndex:i]objectForKey:BIRTHDAY_KEY];
        if (birthday) {
            [formatter setDateFormat:@"MM/dd/yyyy"];
            [formatter setLocale:[NSLocale currentLocale]];
            [formatter setTimeZone:[NSTimeZone systemTimeZone]];
            NSDate *date = [formatter dateFromString:birthday];
            if (date == nil) {
                [formatter setDateFormat:@"MM/dd"];
                [formatter setLocale:[NSLocale currentLocale]];
                [formatter setTimeZone:[NSTimeZone systemTimeZone]];
                date = [formatter dateFromString:birthday];
            }
            NSCalendar *gregorianEnd = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
            NSDateComponents *componentsEnd = [gregorianEnd components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date];

            componentsEnd.year = [[NSDate date] year];
            date = [gregorianEnd dateFromComponents:componentsEnd];
            self.alarmTime = [date dateByAddingTimeInterval:self.mTimeInterval];
            localNotification.fireDate = _alarmTime;
            localNotification.timeZone = [NSTimeZone defaultTimeZone];
            localNotification.applicationIconBadgeNumber = 1;

            NSString *itemName = @"B'day Alert!!!";
            NSString *msgName = [NSString stringWithFormat:@"Celebrate %@'s B'day",name];
            NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:itemName,MessageKey, msgName,TitleKey, nil];
            localNotification.userInfo = userDict;
            localNotification.soundName = self.soundName;
            localNotification.alertBody = [NSString stringWithFormat:@"Celebrate %@'s B'day",name];


            [self.notifications addObject:localNotification];
            } 
        }
    }
}

I have implemented my applicationDidEnterBackground delegate as:

- (void)applicationDidEnterBackground:(UIApplication *)application
{

    UILocalNotification* notification;

    for (int i = 0; i< [self.settingVC.notifications count]; i++) {

        notification = [self.settingVC.notifications objectAtIndex:i];
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }

}

Also, in didReceiveLocalNotification delegate I have this:

 - (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSString *itemName = [notification.userInfo objectForKey:TitleKey];
    NSString *messageTitle = [notification.userInfo objectForKey:MessageKey];
    [self _showAlert:itemName withTitle:messageTitle];
    application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;
}

What changes should i make in my above functions.

Upvotes: 0

Views: 464

Answers (1)

bvogelzang
bvogelzang

Reputation: 1415

You could still use UILocalNotification and only schedule 64 of the closest birthdays. Reschedule these any time there is activity in the app so the most recent ones are updated. I would imagine 64 notifications would allow the user plenty of time before they needed to launch the app again.

Upvotes: 2

Related Questions