How can I schedule local notification for the following scenario?

三世轮回 提交于 2019-11-27 19:21:43

You can simply schedule all (or many) notifications at one time. You don't need to wait for the user to View your app to schedule the next notification.

From the docs on UILocalNotification,

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

So, if you have 3 notifications per day, you can pre-schedule 3 weeks of notifications at once. I suppose you would still have a problem if the user doesn't open your app for a month, but do you need to worry about that scenario?

Anyway, I just wanted to make sure it's clear that you don't need to schedule these notifications one at a time.

Example:

UILocalNotification* n1 = [[UILocalNotification alloc] init];
n1.fireDate = [NSDate dateWithTimeIntervalSinceNow: 60];
n1.alertBody = @"one";
UILocalNotification* n2 = [[UILocalNotification alloc] init];
n2.fireDate = [NSDate dateWithTimeIntervalSinceNow: 90];
n2.alertBody = @"two";
[[UIApplication sharedApplication] scheduleLocalNotification: n1];
[[UIApplication sharedApplication] scheduleLocalNotification: n2];

So, even if the user chooses Close when the first notification appears, the second one will still be delivered.

By the way, the didFinishLaunchingWithOptions method gets called right after your application starts, not right before it closes. That said, you can schedule new notifications whenever you want.

You can also use the repeatInterval property so that they reschedule themselves indefinitely. However, you are limited to the units in NSCalendarUnit. See the docs for more info

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!