ios - Schedule unlimited number of local notifications

半腔热情 提交于 2020-05-29 10:13:55

问题


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.

Events are stored in 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]
)

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

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.

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.

info

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.


回答1:


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

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.

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:

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.

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!




回答2:


Have you tried the background fetch feature? Official document link here.

And in my opinion, it would be better to use remote notification, just setup a simple server to store the users' data.



来源:https://stackoverflow.com/questions/61031931/ios-schedule-unlimited-number-of-local-notifications

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