多个UILocalNotification管理

发布于 2024-09-10 09:57:48 字数 523 浏览 1 评论 0原文

我正在开发一个应用程序,因为我无法使用自定义时间间隔来安排本地通知,所以必须创建 N 个本地通知来模拟定时安排。应用程序对每个 X 对象执行一次此操作(现在我们将其称为日历事件)。因此,用户可以有 100 个日历事件,每个事件都有 20 个本地通知,每隔几分钟/几天/几周/无论什么时间提醒用户一次。

棘手的地方在于:假设其中一个通知在 2 分钟后发出,另一个通知在 3 分钟后发出。假设我忽略第一个,但点击操作按钮转到第二个应用程序。我的处理程序方法 didFinishLaunchingWithOptions: 在应用程序委托中没有被调用;应用程序直接进入主屏幕。我似乎能获得一种方法来响应以这种方式加载的应用程序的唯一方法是使用 applicationDidBecomeActive: 。

我该如何处理这些通知?即使在 applicationDidBecomeActive: 中,我也无法通过 [[UIApplication sharedApplication] ScheduledLocalNotifications] 访问通知,因为它们似乎在调用该方法之前已被清除。

谢谢!

I am developing an application that, because I can't use a custom time interval to schedule local notifications, has to create N local notifications to simulate a timed schedule. The application does this once for every X objects (let's call them calendar events for now). So a user can have 100 calendar events, each with 20 local notifications to remind the user once every few minutes/days/weeks/whatever.

Here's where it gets tricky: Let's say one of those notifications goes off 2 minutes from now, and one goes off 3 minutes from now. Say I ignore the first one, but tap the action button to go to the application on the second one. My handler method didFinishLaunchingWithOptions:in the application delegate is not being called; the application goes straight to the home screen. The only way I can seem to get a method to respond to the application being loaded in this manner is with applicationDidBecomeActive:.

How can I handle these notifications? Even within applicationDidBecomeActive:, I can't access the notifications through [[UIApplication sharedApplication] scheduledLocalNotifications] because they seem to be cleared before that method is called.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃→可爱长大的 2024-09-17 09:57:48

需要注意的事项:

UILocalNotification 的最大数量

您给出的场景是每个“日历事件”将需要大约 20 个本地通知,并且您预计一个典型用户最多有 100 个日历事件。这将产生 2,000 个 UILocalNotification。您可能需要注意,iOS4 将每个应用程序的 UILocalNotification 数量限制为 64 个。

请参阅:UILocalNotification 类参考

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


处理 UILocalNotification

自 iOS4 以来,应用程序不再仅通过 application:didFinishLaunchingWithOptions: 启动:

application:didFinishLaunchingWithOptions: 仅在应用程序启动时调用一次从非后台状态启动(例如,用户首次启动、之前的启动被 iOS 终止以节省内存、用户从多任务栏终止应用程序、设备重新启动)。

因此,在 iOS4 中实际上需要考虑三种不同的场景来处理 UILocalNotification(这是引入多任务的地方)。

当您的应用程序处于后台时,处理通知的位置是通过 application:didReceiveLocalNotification:,它在两种情况下触发:

  1. 当您的应用程序处于后台时,当用户点击“查看”操作按钮以从警报通知“启动”您的应用程序
  2. 时当您的应用程序在前台运行时会触发通知。

application:didFinishLaunchingWithOptions: 仅处理一种情况:

  1. 当用户点击“查看”操作按钮从警报通知启动您的应用程序时,您的应用程序在后台运行

不支持多任务处理的设备 (例如 iPhone 3G)根本不会使用场景 1,而只使用场景 3。

在场景 2 中,您可以处理自己的通知,包括显示任何适当的 UIAlertView 或执行某些操作。


现在,最后要注意的是,当应用程序未在前台运行时触发并被用户忽略的通知(即用户没有点击“查看”操作按钮)将不会触发场景1 或 3. 他们不会在后续启动时向您的应用程序发送任何通知。

您是对的,您无法再通过 [[UIApplication sharedApplication] ScheduledLocalNotifications] 访问这些通知,因为它们在通知时已经过期。

希望这有帮助!

Few things to note:

Maximum number of UILocalNotification

You gave the scenario that each 'calendar event' will need about 20 local notifications, and you expect a typical user to have up to 100 calendar events. That would make 2,000 UILocalNotification. You may want to note that iOS4 limits the number of UILocalNotification to 64 per app.

See: UILocalNotification Class Reference

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


Handling UILocalNotification

Since iOS4, apps no longer launch exclusively via application:didFinishLaunchingWithOptions:

application:didFinishLaunchingWithOptions: is called only once when the app is launched from a non-background state (eg. first launch by user, previous launch was killed by iOS to conserve memory, user killed the app from the multitasking bar, device was restarted).

So there are actually three different scenarios to consider handling UILocalNotification in iOS4 (which is where multitasking has been introduced).

When your app is backgrounded, the place to handle notifications is via application:didReceiveLocalNotification:, which fires in two scenarios:

  1. When user taps the 'View' action button to 'launch' your app from the alert notification while your app is backgrounded
  2. When the notification fires while your app is running in the foreground.

application:didFinishLaunchingWithOptions: handles just one scenario:

  1. When user taps the 'View' action button to launch your app from the alert notification while your app is not running in the background

Devices that do not support support multitasking (eg iPhone 3G) will not use scenario 1 at all, but exclusively scenario 3.

In scenario 2, you handle your own notification, including displaying any appropriate UIAlertView or doing some action.


Now, the last thing to note is that notifications that are triggered while the app is not running in foreground and are ignored by user (ie. user did not tap 'View' action button) will not trigger scenario 1 or 3. They will not deliver any notification to your app at all on subsequent launches.

You're right that you can't access these notifications any more via [[UIApplication sharedApplication] scheduledLocalNotifications] because they've already been expired at the time of notification.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文