8

I am developing a calendar app which consists of multiple events(e.g 500 events) which consists of birthdays.I am downloading events form web service. I want to give user the alert on each birthdate at a specific time sat 10:00 AM on the birthday. I am using local notification for scheduling a alert but I am stuck as iOS allows only 64 notifications per app and I have multiple birthdays. I don't know how to schedule more notifications once the app crosses the limit. Please suggest how would I solve this problem. below is my code to schedule notifications

- (void)scheduleNotification:(NSMutableArray *)datesArray withMessage:(NSMutableArray *)messagesArray  NotificationID:(NSString *)notificationID
{

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateStyle:NSDateFormatterNoStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];


    NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
    [formatter1 setDateStyle:NSDateFormatterMediumStyle];
    [formatter1 setDateFormat:@"dd/MM/yyyy"];

   // NSDate *myTime = [formatter dateFromString:@"06:10 PM"];
    NSDate *myTime = [formatter dateFromString:fireTime];

    for (int i = 0; i < [datesArray count]; i++)
    {
        NSDate *myDate = [formatter1 dateFromString:[datesArray objectAtIndex:i]];

        NSDateComponents *dateComponents = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:myDate];
        NSDateComponents *timeComponents = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:myTime];

        NSDateComponents * newComponents = [[NSDateComponents alloc]init];
        NSDateComponents *todayComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];

        int day = [dateComponents day];
        int month = [dateComponents month];

        [newCompoents setDay:[dateComponents day]];
        [newCompoents setMonth:[dateComponents month]];

        if (day >= [todayComponents day] && month >= [todayComponents month]) { 
            NSLog(@"day = %d, month = %d", day, month);
            [newComponents setYear:[todayComponents year]];
        } else {
            NSLog(@"%d, %d", day, month);
            [newComponents setYear:[todayComponents year]+1];
        }


        [newComponents setHour:[timeComponents hour]];
        [newComponents setMinute:[timeComponents minute]];

        NSDate *combDate = [calendar dateFromComponents: newComponents];

        localNotif = [[UILocalNotification alloc] init];
        if (localNotif == nil)
            return;
        localNotif.fireDate = combDate;
        localNotif.timeZone = [NSTimeZone defaultTimeZone];

        // Notification details

        NSString *message = [@"Wish" stringByAppendingFormat:@" %@%@", [messagesArray objectAtIndex:i],@" On His Birthday"];
        localNotif.alertBody = message;

        // Set the action button
        localNotif.alertAction = @"View";

        localNotif.soundName = UILocalNotificationDefaultSoundName;
        localNotif.applicationIconBadgeNumber = 1;

        // Specify custom data for the notification
        NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
        localNotif.userInfo = infoDict;

        // Schedule the notification
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    }

}

correct me if I am making any mistakes and suggest how to schedule more notifications once 64 limit is crossed.

Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
  • IMO you can reschedule LocalNotification when ever you open your app, like check for Total scheduled localNotifications count and if it is less than 64, reschedule there.. – Bala Jul 26 '13 at 09:57
  • and just take a look at this answer http://stackoverflow.com/a/7721445/1059705 – Bala Jul 26 '13 at 10:06
  • yes I can count the total number of notifications using that method. what do you mean by when ever you open your app. do i need to check the count in my app delegate and schedule more notifications. – Suhit Patil Jul 26 '13 at 10:22
  • both in didReceiveLocalNotification: and didLaunchApplication – Bala Jul 26 '13 at 10:31

1 Answers1

1

I have a similar problem and a few additional options that may help you, though they all have flaws that means correct behavior is not guaranteed.

Keep in mind that overlapping birthdays could be useful here. If two people end up having a birthday on the same day, cancel and reschedule with both of their info associated. You could find other intervals where repetition occurs if you're OK w/ a generic message.

Obviously, encouring your user to press the notification to open the app would help (possible with your ad revenue too). You could try a nice message for your last one.

It's hacky, but you could use geofencing (but probably not remote notifications) as described here: Can push notifications be used to run code without notifying user? . If you add a feature that uses it, you might even be able to get it approved.

I've considered passing a custom calendar as well, but NSCalendar is toll free bridged to CFCalendarRef and the story seems to be that I'm not going to have luck trying to dig into that (and getting it approved at least). I would be happy to be told I'm wrong.

Community
  • 1
  • 1
Jason Newell
  • 591
  • 6
  • 11