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.
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!
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.
Related
I have application who is getting notifications from Outlook, it is subscribed for update,create changes in users mailboxes. I am getting notifications all day long even if the users are not active (It is their sleep time). Can I know how notifications are sent to me?
There are all sorts of background tasks (assistants), both time based and event based that wake up and do work on the system. In addition, there may be apps that the user has given permissions to operate on their mailbox that may be doing something behind the scenes. Exchange doesn't differentiate. When a change is made that matches the criteria in the subscription, a notification will be generated.
I know that this topic is a duplicate, but I need your help. It is very important for me and the other posts have no really an solution for me.
I have an app (Swift 2) where the user can save entries in core data. These entires will show in a table view.
The user has to set an reminder date (with a date picker) for each entry. This date will use for the fire date of the local notification.
Each entry get 2 local notifications
First fire date: 1 week before the chosen date, second fire date: chosen date
The problem is the Apple limitation of 64 local notifications.
The user can only save 32 entries (32 entries * 2 notifications = 64 notifications)
How can I solve this problem with the limitation?
I know that I can set "reminder" into the Apple calendar instead of local notifications. But this doesn't looks good - this shouldn't be the solution.
I know that I can check on each app start or in the method did receive notification, which notifications should set next. but for this I have to trust, that the user start the app or tap on the received notification. If he or she doesn't do this for a few days => no new notification will set and he or she doesn't get the next notification. This solution isn't very safe.
You could routinely send a push notification to devices that have not contacted the backend in some time and have the remote notification handler setup local notifications up to the limit, see application(_:didReceiveRemoteNotification:fetchCompletionHandler:).
Or, you could setup a background fetch that reschedules local notifications up to the limit using, see application(_:performFetchWithCompletionHandler:). Note that you cannot fully control how often this refresh is performed, but in my experience it is called up to a number of times a day.
When using either one of the above method, you can update local notifications even if the user never opens the apps.
I also had some kind of problem like this.
So what I did is that I planned the notifications as much as possible, and when there is room to plan more notifications then I planned more notification.
UIApplication.sharedApplication().scheduledLocalNotifications?.count
You can get the number of planned notifications. Subtract it from 64 and plan remaining notifications.
In applicationWillEnterForeground you can do this -
if UIApplication.sharedApplication().scheduledLocalNotifications?.count < 64
{
//refersh list
let nc = NSNotificationCenter.defaultCenter()
nc.postNotificationName(Constants.SCHEDULE_MORE_NOTIFICATION, object: nil)
}
So your notification list will be refreshed automatically.
I would like to know what is the best approach to do if I want to fire notifications for more than one time each day everyday.
I did some research and read that notifications for the next day cannot be fired unless the user opens the app the next day and updated the notification. Is that true? is there anyway I can do it without the need of the user opening the app everyday?
Thank you
You can schedule up to 64 local notifications. There is no limit on the time period; you can schedule them years in advance if you like.
That said, if you need some mechanism to schedule new notifications, even if the app is not running at all (e.g. because the user terminated it), you need a background mode for that. Fetch is probably the way to go here, as it doesn't need a special trigger. You could also send silent push notifications in order to wake the app, make the calculations and schedule the new notifications.
My calendar app is not connected to Event Kit which allows me to create custom repeating intervals like "every 3 days, 29 times" etc.
Now i would like to add Notifications to my Events ... but my custom repeating events are limiting me in every possible way.
I think I have checked every possible solution:
UILocalNotification
UILocalNotification is limited to 64 events and NSCalendarUnit's in-built Unit Time Intervals aren't very flexible.
1 event repeated every 2 days will fill this up very quickly
Should i reschedule 64 Notifications on every app start and beg the user to start the app regularly? Not running the app for a while will not reschedule notifications.
Event Kit (Calendars & Reminders)
I could create a calendar or Reminder list, generate single events from the custom repeating events and add notifications here. But the user can edit this, which will cause confusion.
I can't synch this back to my app.
Should i create a calendar or reminder list for Alerts and hope that the user will not touch this?
Apple Push Notification Service
Everywhere i can read: They are not reliable! There is no guarantee that push notifications will actually be delivered, even if the APNS server accepted them.
I think push notifications are not made for notifying the user at scheduled times as there is no guarantee they will arrive.
The question is what should i do, or what would you do in my case? No solution is perfect and i am hoping to find the most user friendly approach here. In case i am missing a approach, i would like to hear the alternative.
Edit:
Adding a another method for completeness.
UILocalNotification without repeats
don't offer the repeating of local notifications. The user can assign single Notifications to a event. After enabling the 65th notification, a popup will remind him that he has reached the system limit. This way you put the responsibility to the user. After reaching the limit he will be forced to focus on events that are not far in the future.
You could save all your events to core data and then every time the app is launched or when your main view appears load in all events for that week and set a scheduled local notification. Once the event is scheduled you set a flag in the entity eg hasBeenNotified.
Hope it helps.
I decided to offer local notifications without repeat functionality.
This decision was made after chatting with a few users.
Better to offer something than to offer nothing :)
Overview
I have an iOS app which sends local notifications at specific dates.
I just learned that I can only schedule 64 notifications at a given time.
There are cases when I can't schedule notifications as the 64 slots are filled.
So I store them in the database and when the user responds to a notification I check if there are any available slots and schedule the remaining notifications.
Problem
When the user doesn't respond to a notification my code is not executed so I am not able to schedule the remaining notifications.
Question
Is there a solution for this problem ?
can I execute a piece of code (house keeping) at certain times ?
is there any work around for this ?
You may not want to signal to the user there is a problem, but rather just do it in the background. If a user has 64 notifications for one app and hasn't opened the app, then they probably aren't using the app. Once a notification has fired it isn't in the array anymore. So you will have room every time a notification is fired off. They do however remain in notification centre, which you have to clear out yourself.
Its usually better to not present possible problems to the user, but rather handle them in a way that makes sense internally if that is an option. Look up the delegate methods for the appDelegate and you will most likely find ways you can handle what you are trying to do.
Thought I would make a post in case you wanted to accept the answer.
Best of luck.