ios 如何为以下场景安排本地通知?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13773123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I schedule local notification for the following scenario?
提问by Haris Farooqui
Experts, I have a scenario wherein I need to notify user three times a day (morning, afternoon, evening). And the timings for these notifications will be different for each day, based on database values for each date.
专家,我有一个场景,我需要每天通知用户三次(早上、下午、晚上)。根据每个日期的数据库值,这些通知的时间每天都不同。
These three notifications are configurable. Meaning user might just choose to set afternoon and evening while turning off morning notification under settings.
这三个通知是可配置的。这意味着用户可能只是选择设置下午和晚上,同时在设置下关闭早上通知。
As per my understanding I can achieve this using local notifications.
根据我的理解,我可以使用本地通知来实现这一点。
I can do the following: - before the application exits, inside didFinishLaunchingWithOptions, I can check what is the next notification due, is it set (on/off). If it is set I schedule it. If not I move on to the next notification type and do the same thing. If all the notifications are turned off, obviously, no notifications will be scheduled.
我可以执行以下操作: - 在应用程序退出之前,在 didFinishLaunchingWithOptions 中,我可以检查下一个到期通知是什么,是否已设置(开/关)。如果设置了,我会安排它。如果不是,我将转到下一个通知类型并执行相同的操作。如果关闭所有通知,显然不会安排任何通知。
Now when the notification shows up i get to see alert with two buttons "Close" and "View". If user selects "View" My app is back to active and before user exits next notification is scheduled.
现在,当通知出现时,我会看到带有“关闭”和“查看”两个按钮的警报。如果用户选择“查看”,我的应用程序将恢复活动状态,并在用户退出之前安排下一次通知。
So far so good.
到现在为止还挺好。
Now if user choose to select "Close" What do I do? It wont launch my app and consequently next notification wont be scheduled?
现在如果用户选择“关闭”我该怎么办?它不会启动我的应用程序,因此不会安排下一个通知?
How do I achieve this? Is there any better way to do this?
我如何实现这一目标?有没有更好的方法来做到这一点?
Help! Help! Help!
帮助!帮助!帮助!
回答by Nate
You can simply schedule all (or many) notifications at one time. You don't need to wait for the user to Viewyour 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
一个应用程序只能有有限数量的预定通知;系统保留最早发出的 64 条通知(自动重新安排的通知计为一个通知)并丢弃其余通知
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?
因此,如果您每天有 3 条通知,则可以一次预先安排 3 周的通知。我想如果用户一个月不打开您的应用程序,您仍然会遇到问题,但是您需要担心这种情况吗?
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 Closewhen 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.
顺便说一下,该didFinishLaunchingWithOptions
方法在您的应用程序启动后立即被调用,而不是在它关闭之前。也就是说,您可以随时安排新通知。