3

I'm developing a project that notifies the user every day on a different time from a pre-defined database, five times a day to be precise.

My problem is how schedule all these times, 5times*365days = 1825 fire dates !! is it possible to schedule them all?

any ideas would be very much appreciated

Roham Rafii
  • 2,929
  • 7
  • 35
  • 49
4mahmoud
  • 813
  • 2
  • 19
  • 31

2 Answers2

13

According to Apple documentation:

An application can have only a limited number of scheduled notifications; the system keeps the soonest firing 64 notifications (with automatically rescheduled notifications counting as a single notification) and discards the rest

I solved that problem by setting a "queue" of notifications. For example, in my app I have three different types of notifications, let's just say type A, B and C.

I schedule the A, B and C notifications for the next month, everytime the user opens the app, I checked how many notifications left. If, for example, the are no more A notifications, the app schedules more A notifications and so on.

How I achieve this?

Everytime I schedule a notification, I use the userInfo property. I set a dictionary with a key called type and a value.

In my app delegate I check all the pending notifications and count how many left for each type. The code looks like this:

NSArray *scheduledNotifications = [UIApplication scheduledLocalNotifications];

NSUInteger AType, BType, CType;

for (UILocalNotification *notif in scheduledNotifications) {
        //Classify notifications by type
        NSUInteger notifType = [[notif.userInfo objectForKey:@"type"]integerValue];
        if (notifType == 0) {
           AType++;
        }else if(notifType == 1){
            BType++;
        }else{
            CType++;
        }

}

If the count of any type is zero the app schedules more notifications.

Finally, if the notifications are show for example, everyday at the same hour you can use the repeatInterval property BUT you can't create your own repeating intervals, you can only use the repeating intervals defined in NSCalendarUnit.

Hope it helps.

Eduardo
  • 1,383
  • 8
  • 13
  • Yes, I've used something similar -- create a queue or ordered list of the pending notifications and walk through it when the app opens, etc. (Re)schedule notifications in date order. In fact, with this scheme you only ever really need one notification outstanding, since it's "meaning" is recorded in your queue (though it's good to schedule several so that the "notification arrived" and badge number stuff will more or less match expectations when the app's not running). – Hot Licks Apr 27 '12 at 01:13
  • @Edu That helped me a lot, it came to my mind but didn't know how to achieve it, but since im going to schedule an entire year by scheduling a set of notifications < 64 every time user pones the app off course after ensuring there's no pending notification, those 1825 will repeat the exact same time next year, so if i set repeatInterval property to kCFCalendarUnitYear, i dont have to schedule any more notifications next year ,right? – 4mahmoud Apr 27 '12 at 10:29
  • Yes, I found NSCalendarUnit a little tricky, so I recommend you to set some notifications and see by yourself how they work. Changing my iPhone's date help me to find out how to use the repeatingInterval so I encourage you to do so. It's also important to mention that a notification with a repeatingInterval counts as ONE notification. :) – Eduardo Apr 28 '12 at 00:17
  • I don't understand this code. `carNotificationsDic` is not used and `scheduledNotifications` is not defined. Why would you separate type A, B, C instead of sort them bye due date? – PiTheNumber Feb 05 '13 at 16:34
  • Sorry about that, `carNotificationsDic` is not needed, it's part of my code but is not necessary here. `scheduledNotifications` is a `UIApplication`'s property. – Eduardo Feb 06 '13 at 04:00
1

You could try Eventkit framework using that you could add reminders,events,alarms.

1)It has no number of notification limitation like UILocalNotification.

2)With event kit you could add both time based as well location based events.

3)You don't need to worry about privacy too.

Although both UILocalNotification and EventKit is powerful in its own right..

Note that:ios 6 or above for EventKit framework

It's upto you to choose which one depending upon your needs.

The below links will give you good headstart

Resource 1

Resource 2.

The image below will give you a nice peek of what evenkit is.

EventKit_image

Use eventkit if it suits your need.

Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241