react-native-background-fetch icon indicating copy to clipboard operation
react-native-background-fetch copied to clipboard

Question: is it possible to keep background tasks alive in iOS after app termination?

Open ASchwad opened this issue 6 years ago • 19 comments

Is it possible to keep background tasks alive in iOS after app termination? I saw the headless JS configuration options for android, but haven't seen similiar options for iOS.

I gave it a try and did not qet it work so far (after the app is terminated, while it is in foreground or minimized, it works fine)

Your Environment

  • plugin version: "^3.0.6",
  • Platform: iOS
  • OS version: 13.5.1
  • Device manufacturer / model: iPhone 7
  • React Native version (react-native -v): "^0.61.5",

Expected Behavior

Expected task to keep running after app termination on iOS

Actual Behavior

Task is terminated aswell

Thanks a lot for your work and dedication @christocracy

ASchwad avatar Oct 24 '20 14:10 ASchwad

Oops: I responded as if this were a react-native-background-geolocation question. Ignore previous statement.

For background-fetch, if the user terminates the app, iOS completely halts all events. There is no such thing as stopOnTerminate: false for background-fetch on iOS.

is it possible to keep background tasks alive in iOS after app termination

No. It's impossible.

christocracy avatar Oct 24 '20 14:10 christocracy

For background-fetch, if the user terminates the app, iOS completely halts all events.

@christocracy i'm having trouble finding documentation to support this. Could you please link to the docs you're referencing? 🙏

mvayngrib avatar Apr 20 '21 12:04 mvayngrib

@mvayngrib See the top of the README

  • ⚠️ When your app is terminated, iOS no longer fires events — There is no such thing as stopOnTerminate: false for iOS.

christocracy avatar Apr 20 '21 12:04 christocracy

Also see this thread at Apple forum

Will performFetchWithCompletionHandler be called if the app has been terminated?

The response is by an Apple Engineer eskimo:

christocracy avatar Apr 20 '21 12:04 christocracy

@christocracy thanks for the quick and helpful response! Are you aware of an alternative to BGRefreshTask that does open the app after termination?

mvayngrib avatar Apr 20 '21 12:04 mvayngrib

that does open the app after termination?

There is none. If there were, I'd have done it.

What are you try to do with your periodic task?

christocracy avatar Apr 20 '21 12:04 christocracy

@christocracy we're evaluating a feature that requires the app to occasionally wake up to detect something, say at least once a week

mvayngrib avatar Apr 20 '21 13:04 mvayngrib

Your only option may be scheduling a local notification to catch the user's attention. Perhaps with this RN plugin (see fireDate).

Only if the user actually clicks your notification will the app launch and your code run.

christocracy avatar Apr 20 '21 13:04 christocracy

@christocracy ic, thanks for the suggestion. Unfortunately, the use case requires the app to check something before it decides whether it's worth bothering the user

mvayngrib avatar Apr 20 '21 13:04 mvayngrib

Then you are out of luck. I've been doing this a long time, since iOS 7. iOS used to allow background-fetch and push notifications to re-awaken a user-terminated app. That all changed around iOS 10. It used to be that your server could send a "silent push notification" (a push notification with only a JSON payload) to a terminated device and iOS would re-launch the app to service the notification. This is no longer the case for several years now.

christocracy avatar Apr 20 '21 13:04 christocracy

on iOS, if I understand correctly, the closest you can get to running code in response to some thing that can wake up apps is sending an actual visible push notification and having a notification extension. That extension may be able to do something - I think you get a chance to run a little code there. One thing it may do is kill the notification if some condition is not met? I have no idea what restrictions notification extensions run under - this is just a brainstorm-level thought since Notifee was playing with notification extensions recently in order to allow custom presentation via a JS API since background / silent push notifications have the problems described above

https://notifee.app/react-native/docs/ios/remote-notification-support

none of this may work, in general I design features around this and assume I need to be interesting enough to the user that they actually use my app or I don't get to execute code, but we're all adults and I understand there are potential legit use cases for anything

mikehardy avatar Apr 20 '21 13:04 mikehardy

There are of course APIs capable of re-launching a user-terminated app, some of which I take advantage of in my react-native-background-geolocation.

  • Geofencing: If a geofence fires from a terminated app, iOS will re-launch that app to service the event. Requires Always location authorization.
  • Significant Location Changes: An API that will record a location about every 1000 meters. Just like Geofences, iOS will re-launch a terminated app to service a location event. Requires Always location authorization.

christocracy avatar Apr 20 '21 13:04 christocracy

is sending an actual visible push notification and having a notification extension

But still, I don't think iOS will re-launch a terminated app until the user actually clicks the notification, right?

christocracy avatar Apr 20 '21 13:04 christocracy

@christocracy thanks for sharing! re: silent push notifications, i see some people claim it still works on iOS 14, though it's more throttled than before. Is there something in the Apple docs that says explicitly that they won't wake a terminated app?

mvayngrib avatar Apr 20 '21 13:04 mvayngrib

silent push will not work if user force closed the app, I maintain react-native-firebase (super popular source of silent push notifications...) and notifee (presents notifications, and came from react-native-firebase notification code). This is a source of so many dashed developer hopes.

But still, I don't think iOS will re-launch a terminated app until the user actually clicks the notification, right?

A good question, it needs a real test. My assumption (danger!) is that push notifications with an actual user-visible notification will show up. Silent push (in iOS terms "content-available", really) no. But you've got me doubting more and I wasn't sure to start.

mikehardy avatar Apr 20 '21 13:04 mikehardy

According to the above conversation, I know we can't run the background process in iOS when the user terminates the app, but is there any way to do that?

My use case is like this: I've a react-native mobile app for smartwatch. now I'm getting data from a smartwatch from Bluetooth. now I need to get data in every 15 min from watch when app is terminated and send those data to API. Is it possible?

JatinBhuva avatar Jun 19 '24 12:06 JatinBhuva

I know we can't…but is there any way to do that?

No. iOS halts their BackgroundTask api when the user terminates the app. This is a choice by the OS. There is no magic, secret way.

christocracy avatar Jun 19 '24 12:06 christocracy

Anyone used Life360 app? Somehow it is getting access of location after app is killed.

tec-anDro-DeV avatar Sep 18 '26 11:09 tec-anDro-DeV

Somehow it is getting access of location after app is killed.

Have you tried my background-geolocation plugin?

https://github.com/transistorsoft/react-native-background-geolocation

christocracy avatar Sep 18 '26 14:09 christocracy