1

I have an iOS app that periodically needs to do some work in the background. I was planning to use FCM to send notifications every 15 minutes and have my app do some work when they arrive. Preferably the notification should not be shown to the user, but the most important thing is that the background work can run for 10-20 seconds.

I have looked into BackgroundTasks to avoid sending the notifications, but these tasks are not reliable and are sometimes run only once or twice a day.

When it comes to push notifications, I could either send a "classic" one that generates a notification, or a background notification. The problem with the latter is that it will be delivered max 3 times per hour (source). Maybe a classic notification with a NotificationExtension is the best approach? I am not sure how much time the system will grant the app though, and if it works when the app is in the background.

So my question is: What is the best, most reliable way to perform background work on an iOS app every 15 minutes?

km11
  • 528
  • 3
  • 12
  • 1
    The real question is *why* you need to run these tasks. If the user is using your app then you can do all the work you need. If they aren't using your app, either perform the work the next time they open it or do it on a server. – Paulw11 Jul 13 '23 at 09:32
  • @Paulw11 It would be preferable to do it on the device, since the logic is already implemented this way. The corresponding Android app also does the work on the device. – km11 Jul 13 '23 at 09:35
  • But why does it have to be done every 15 minutes? Why can't you just do the work the next time the app is opened. Apple doesn't make background processing difficult just for fun. They do it because it is better for battery life and to discourage developers from trying to run tasks on a mobile device that shouldn't. (And just because you *can* do it on Android, doesn't mean that you *should* – Paulw11 Jul 13 '23 at 09:45

1 Answers1

0

In a nutshell there isn't any way of scheduling iOS apps to run periodically.

A background notification won't work reliably, because the app won't get launched if it's in a terminated state (also if the batter is low, the OS will not deliver it to the app or hold onto it for a few hours before doing so).

You can't use a user notification because what user wants to be bombarded with a notification every 15 minutes?

You can't use a notification extension for the same reason as above (bothering the user) unless you request a special entitlement from Apple which the ability to suppress the notification associated with the push. But a notification extension doesn't launch the app, so your housekeeping code would have to be capable of running in the extension independently of the app.

You can't schedule the app to repeatedly run in the background. With a background task you can't control when or how often it will run, if indeed it runs at all. Background scheduling is at the discretion of the OS. If it does run, it'll probably be during a quiet period during the night. If the user hasn't interacted with your app for several days the OS won't schedule it to run at all. You certainly cannot schedule background execution with a timer like you could with Android for example.

iOS simply just doesn't provide you with the ability to do what you want.

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378