Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

objective c - Fire local notification every day on different times

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...