0

I'm implementing an app with an internal calendar, fetched from a remote web service. I want to send a local notification to the user when an event of interest is scheduled in the calendar, at a specific time chosen by him. Just like the iOS calendar app, when you can create an event and ask to be notified X hours/days before it happens. The main difference is that you can't create events: they are downloaded from a pre-populated remote calendar.

In Android I've solved the problem by using AlarmManager, while in iOS with Swift 3 the closest I've got to porting the same solution was via opportunistic background data fetch. The main difference between the two solutions is that background data fetch doesn't work when the app has been killed.

It would be really important for me that local notifications worked even when the app is killed. I think users expect apps notifications to work even when the app is closed, like it happens with WhatsApp or Facebook. I know that those notifications are triggered by someone writing something and therefore they are Push Notifications, but the average user just expects notifications to keep working even when the app is closed.

What would be the most elegant solution to "emulate" the behaviour of Android's AlarmManager in iOS?

Scheduling a bunch of notifications in advance hoping that the user will eventually open the app before all of them are dequeued looks a badly designed solution.

Also, delegating all the work to the server and push the notifications to the subscribed devices looks quite bad too as it requires much more work on the server side. Plus it requires a server which is able to awake itself to send push notifications, which is something that I don't have. I only have a simple webserver which answers to HTTP requests.


EDIT : The ideal solution I'm looking for isn't any of the previous 2 options, since I find them more like workarounds than actual elegant solutions to what I perceive being a fairly common problem. There has to be a third option...

Dario R.
  • 71
  • 3

1 Answers1

0

In iOS there is no way to achieve this. Looking at the documentation of application(_:didReceiveRemoteNotification:fetchCompletionHandler:), it states that

the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.

You can receive push notifications, but no code will be executed until the user launches your app. So unless you are sending remote push notifications from a server, you cannot set up new local notifications until the user opens your app.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • I have the habit of killing all of my apps once or twice a day, and I still receive Push Notifications from WhatsApp / Telegram / whatever. Maybe they won't trigger code execution, but they do get to the user. – Dario R. Jun 08 '17 at 09:20
  • You are correct, you can receive push notifications even if your app is killed, but you can't execute code in the background and hence you cannot schedule new local notifications. See my updated answer. Have a look at [this](https://stackoverflow.com/questions/8611921/is-it-possible-to-have-my-closed-app-run-code-in-ios/8611952#8611952) answer of a similar question. – Dávid Pásztor Jun 08 '17 at 09:57
  • What about this link: [Can push notifications be used to run code without notifying user?](https://stackoverflow.com/questions/11861117/can-push-notifications-be-used-to-run-code-without-notifying-user). They talk about Remote/Silent notifications – Dario R. Jun 08 '17 at 10:38