ios - 安排无限数量的本地通知 [英] ios - Schedule unlimited number of local notifications

查看:66
本文介绍了ios - 安排无限数量的本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个允许用户创建重复事件的应用.每个事件在一天中的特定时间可能有也可能没有提醒/警报.如果有,应用会在当天的那个时间发送本地通知.

I have an app that allows users to create recurrent events. Each one of the events may or may not have reminder/alerts at a specific time of day. If they have so, the app sends a local notification at that time of day.

事件存储在 CoreData 中.

Event(name: "Go to London", 
  date: 2020-04-03 21:40:55.419925+0200, 
  reminders: [2020-04-03 20:00:00.419925+0200, 
  2020-04-03 10:00:00.419925+0200, 
  2020-04-03 12:00:00.419925+0200]
)

事件可能发生在一年中的每一天或接下来 X 年的每一天.

An event may occur on each day of the year or everyday for the next X years.

用户每天可以创建无限数量的事件.因此,要发送的通知总数可以轻松超过 64(您可以在 iOS 中安排的本地通知总数).因此,当应用程序处于前台时,我无法安排所有通知.

A user may create unlimited number of events per day. And hence, the total number of notifications to be sent can easily surpasses 64 (total number of local notification that you can schedule in iOS). So I can not schedule all the notifications while the app is in foreground.

如果待处理的通知少于 64 个,我需要一种机制来定期安排通知.即使应用程序在后台,也应该这样做.

I need a mechanism to periodically schedule notifications if there are less than 64 notifications pending. This should be done even if the app is in the background.

如果您提供解决方案或指导我为这种情况找到解决方案,我会很高兴.

I would be happy if you provide a solution or guide me towards finding a solution for this scenario.

我尝试设置一个计时器来定期检查待处理通知的总数及其截止日期..但它不起作用,因为一旦应用进入后台,计时器就不会触发.

I tried to set up a Timer that periodically checks total number of pending notifications and their due dates. . But it did not work, because timers won’t fire once the app goes in background.

推荐答案

我不确定我是否理解您的问题.但我的印象如下:

I am not sure if I understand your problem right. But my impression is the following:

您的用户设置并更新事件数据库,其中每个事件都有特定的日期和时间.
他们这样做只是将新事件输入到数据库中.然后很容易从数据库中获取第一个 n(比如 10 个)事件.需要获取多个,因为不能保证本地通知的传递,请参阅文档:

Your users set up and update a database of events where each event has a certain date and time.
They do this simply be entering new events into the database. It is easy then to fetch the first n (say, 10) events from the database. It is required to fetch more than one, since the delivery of a local notification is not guaranteed, see the docs:

尽一切努力以一种方式传递本地和远程通知及时,但不能保证交货.

Every attempt is made to deliver local and remote notifications in a timely manner, but delivery isn't guaranteed.

在通知中心注册这些n个本地通知,并取消任何不再在n下的事件的通知.文档说:

Register these n local notifications with the notification center, and cancel any notification for events that are no longer among the n next ones. The docs say:

通常情况下,您会在条件发生变化时取消请求而您没有不再需要通知用户.例如,如果用户完成一个提醒,您将取消与该相关的任何活动请求提醒.要取消活动通知请求,请调用removePendingNotificationRequests(withIdentifiers:)removePendingNotificationRequests(withIdentifiers:) 方法联合国用户通知中心.

Typically, you cancel a request when conditions change and you no longer need to notify the user. For example, if the user completes a reminder, you would cancel any active requests associated with that reminder. To cancel an active notification request, call the removePendingNotificationRequests(withIdentifiers:) or removePendingNotificationRequests(withIdentifiers:) method of UNUserNotificationCenter.

因此,即使您的应用程序在后台或暂停,本地通知也会唤醒它,处理事件,将其从数据库中删除,并更新 n 下一个事件.即使无法传递通知(这不太可能,但可能),您也可以处理错过的事件,并安排下一个事件.
我希望这符合您的要求!

So, even if your app is in the background or suspended, the local notification will wake it up, handle the event, remove it from the database, and update the n next events. Even if a notification could not be delivered (which is not probably, but possible), you could handle the missed event, and schedule the next ones.
I hope this meets your requirements!

这篇关于ios - 安排无限数量的本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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