如何为以下方案安排本地通知? [英] How can I schedule local notification for the following scenario?

查看:93
本文介绍了如何为以下方案安排本地通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专家,我有一个场景,我需要每天三次(早上,下午,晚上)通知用户。根据每个日期的数据库值,这些通知的时间每天都会有所不同。

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.

我可以执行以下操作:
- 在应用程序退出之前,在didFinishLaunchingWithOptions中,我可以检查下一个通知到底是什么,是否设置(开/关)。如果设置,我安排它。如果不是,我继续下一个通知类型并做同样的事情。如果关闭了所有通知,显然不会安排任何通知。

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.

现在,当通知显示时,我会看到带有两个按钮的提醒关闭和视图。
如果用户选择查看我的应用程序恢复活动状态,并且在用户退出之前安排下一个通知。

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.

到目前为止一直很好。

现在,如果用户选择关闭,我该怎么办?它不会启动我的应用程序,因此下一个通知不会被安排?

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?

帮助!帮帮我!帮助!

推荐答案

您可以一次安排所有(或多个)通知。您无需等待用户查看您的应用以安排下一个通知。

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.

来自 UILocalNotification上的文档


一个应用程序只能有有限数量的预定
通知;系统保持最快的64通知
(自动重新安排的通知计为单个
通知)并丢弃其余

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

因此,如果您每天有3个通知,则可以一次预先安排3周的通知。我想如果用户一个月没有打开你的应用程序你仍会遇到问题,但是你需要担心这种情况吗?

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.

示例:

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.

顺便说一下,在应用程序启动后立即调用 didFinishLaunchingWithOptions 方法,不是在它关闭之前。也就是说,您可以随时安排新的通知。

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.

这篇关于如何为以下方案安排本地通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆