Swift: Creating a repeating local notification between a set time each day - ios

I am trying to create a repeating local notification that will notify the user every hour between a set time every week day (e.g. 9-5 monday-friday) but after searching cant find any documentation on how to implement this.

If you want only 5/7 it can be a problem.
When you schedule a local notification you can also set a calendar unit as -repeatInterval property. This is cool because there is an per-app limit (maybe 64) of maximum number of notification that you can schedule, thus creating one you can fire it each day at the same time.
If you need to create a different kind of repetition is easy to reach that limit, to avoid that you can recreate further notifications each time the user open the app, or if you implement interactive notifications, each time the user interact with one.
Or but I've never tried you can create a notification for each day of the week NSWeekCalendarUnit(except sat and sun) an set the repetition to weekday for each, in this way you will only spend 5 notifications, for infinite repetition.

Related

Repeating Local Notifications at specific intervals

Is it possible to have a notification repeat at specific intervals? For example, Remind me to do something on Monday to Friday (from 9:00am to 6:00pm)?
You can schedule local notifications for the specific time, date, weekday, and also specify to be repeated or not.
Here is a comprehensive document provided by Apple that describes how to do it.
If I'm not mistaken, You mean to schedule notification for a continuous period of time, If so, It does not make sense and it's not possible.
But you can schedule multiple notifications for different times of a weekday based on your need.

iOS do scheduled operation in background or when app active

I have CoreData model which I want to update at 12 AM. So it's kind of an even when the app can recognize that a new day is coming and at 12 AM change some things in data models.
Initially, the idea was:
Prepare a single function that returns updated data. So before the function returns smth I every time check for the time (NSDate interval between two dates) and then update data model (if it's a new day). But the architecture not so simple for this purpose and it will take some time to prepare for a single point where I can get updated data, also it takes some time in background to update CoreData model which also adds some expenses to this task.
Is this ok solution to use some timer which will update data at 12 AM, I don't care about consistency in this case, but I don't like a timer which is checking every single second is 12 AM already or not. Is there some push notification update or some scheduler manager in iOS which can update data for me. One more time I just want to update the data layer and I don't care about consistency in UI. If consistency matter for sure then I would like to follow initial ide with a single point of retrieving data.
So I probably need some scheduler manager for this purpose or rewrite code of how I get the data.
There is no way to execute a function at regular intervals, even when the app is backgrounded/killed by the user.
The most reliable solution for executing a function at regular intervals even when the app is backgrounded is to use push notifications scheduled for the specific time intervals (midnight each day in your case), which would wake up the app and let it update its data. However, this solution has its downsides, since you need a server to send the push notification from and the users device needs to be connected to the internet. Also, push notifications don't wake up the app in case the user manually killed it.
For your particular problem, the best solution would be to refactor your code in a way that you have a single function that can be used to retrieve data and hence this function could ensure the data is updated in case a certain time interval has passed since the last update.
You might want to look into BGProcessingTask. You won't have granular control over when you're granted CPU time but you can set the interval you'd prefer that the task execute. Ultimately, when you run and how often is up to the system.
I would recommend checking out the new BackgroundTasks Framework Apple releases for iOS 13.
https://developer.apple.com/documentation/backgroundtasks
While you are not guaranteed at specific time if you have a window (12 am - 3 am) scheduling background tasks may be sufficient for you.

Local Notifications limit for Calendar app

Im building a Calendar app, where you can schedule events, and you get Notified by UserNotification.
The Problem is that i recently read that you can only have 64 scheduled events. But what if the user has more than 64 events? I know repeated notifications are counted as one.
Does this limit count for all notification types (Timer,Calendar,Loacation)?
How would u solve this issue? Since i don't use a server, i cant make push/remote notifications.
Looking forward to ur answers!
Thanks in advance!
After the 64th event, you can try to save the ones after that and schedule them once the number of current scheduled event is less than 64. You should take into account the event's schedule time to avoid missing event with sooner start date.
EDIT
Since your app allows user to schedule future events, it makes sense to use CoreData to persist data. For each event they created, you can create an entity with following attributes:
event name
event start date
isScheduled boolean
This should be fairly simple. After that, whenever the app starts, you can fetch the events and schedule the ones with closest start date. This way, you don't have to schedule an event too far ahead.
If you want to check the number of scheduled events, you can do
UIApplicaiton.shared.scheduledLocalNotifications?.count
This method works but it is deprecated so you might want to use
UNUserNotificationCenter.getPendingNotificationRequests

Auto-detect time of day

I'm writing a daily to do list app, and want the app to update certain elements with each new day. This isn't a problem if the user closes the app one day and then reopens it the next -- I just compare days in the startup methods.
However, this is a problem if the user happens to be using the app exactly at midnight. At midnight, daily task elements need to reset themselves automatically for the new day.
Here's my issue -- I could have a constant thread running in the background, always accessing [NSDate date] and checking for a new day. But I feel like there must be a better way, especially because this only has to happen once per day (and then, only if the user is using the app at midnight).
Thank you in advance!
In applicationDidFinishLaunching: and applicationWillEnterForeground:, setup an NSTimer scheduled to trigger at midnight. Also, register for UIApplicationSignificantTimeChangeNotification to reschedule the timer for time zone changes etc.

Can I change the message in a repeating UILocalNotification?

I have a UILocalNotification that repeats every hour and I would like to change the message when it repeats so it is different, or rotates between 3 messages.
Does anyone know of a way to do this?
Also, it appears I cannot customize my repeating local notifications beyond once every minute, hour, day, etc. What I'd like is a notification every 15 minutes. I suspect that can't be done though... unless you know a way?
Many thanks for assistance you can offer.
If you wanna change the message you can acces to the scheduledLocalNotifications array in your app delegate and modify the message, but to do that you need the app to be running. I think it's better to simply schedule different notifications with the different messages you want to show.
About the repeating interval the simple answers is no, you can't create your own repeating intervals. This is one of the many limitations that UILocalNotificationhas. Many apps (included mine) had solve the problem creating a queue of notifications. I explain that topic here

Resources