Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Stop working when phone is locked #9

Closed
st0ffern opened this issue Sep 22, 2016 · 25 comments
Closed

[iOS] Stop working when phone is locked #9

st0ffern opened this issue Sep 22, 2016 · 25 comments

Comments

@st0ffern
Copy link
Contributor

as title says

@tconroy
Copy link

tconroy commented Oct 13, 2016

is this only occurring on-device? I'm testing in the iOS simulator and it continues to work even while locked.

@ancyrweb
Copy link

ancyrweb commented Dec 8, 2016

Also happens for me on Android.

@st0ffern
Copy link
Contributor Author

st0ffern commented Dec 8, 2016

@tconroy it is on device not working. simulator is working.
If you read the Apple TOS on background processes you will see why 😉

@Rewieer what android sdk version? i got it working here.

@ancyrweb
Copy link

ancyrweb commented Dec 8, 2016

Here it's Android 5.0.
So far i've read that handlers can't keep working on sleep mode as it's running on UI Thread. It seems it's required to wake the device to keep performing services.

@aamir-munir
Copy link

Hi,

I am having same issue but it stops even iPhone is not locked, if someone have a solution please comment on this:
#23

@st0ffern
Copy link
Contributor Author

st0ffern commented Jan 27, 2017

It violates the iOS app terms, and it is not possible to run a background task in iOS.
You will need to use one of the features for background service calls.
They require special permissions and they will not work with script. they will be called with native functions.

@itcurves #23 have nothing to do with this issue 😖

@aamir-munir
Copy link

ok I am trying to understand it, sorry I post the wrong number it was #22 (sorry about that) but if you check #22 I add the log and that log shows it was running in the background for at least 2 and half minutes and stops after that.

Any other guide please that I can follow?

@st0ffern
Copy link
Contributor Author

https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

@st0ffern
Copy link
Contributor Author

This means... react-native-background-timer will not work in iOS

@aamir-munir
Copy link

@Stoffern Thank you and I already check that link and it really helped me as I lie between one of the category and test going well for now, and for my app I do not even need timer as I had a recursive function that runs automatically based on a reply and wait for at least a minute for second api call.

Thanks again for you reply.

@theonlinenerd
Copy link

hi anybody solve this? My app suspended after 3 minutes and never execute again.

any suggestions please?

@st0ffern
Copy link
Contributor Author

@theonlinenerd that is normal.. read iOS docs...

@ancyrweb
Copy link

It is normal in the sense it is to be expected from the doc point of view. But it is not normal on the programming point of view : he programmed it to keep working on background.
So, I believe he's asking how to make this work as expected ?

I'm not working on IOS, i'm sorry I can't help.

@st0ffern
Copy link
Contributor Author

Ok, here is the iOS docs explained for those who dont want to read them.

Definition: Suspended state = clicking the home button and have the app running in the background (not killed)

When a app is put into Suspended state a applicationWillTerminate will be sendt to the app. This means all running tasks will have 3 minutes to complete before app is put into freeze mode (The app will stop working 100%)

There is exeptions ref: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
Table 3-1

Every of the sections here required a specified iOS function to be called in order to run under the permissions.
In example you want background GPS running? you can tell iOS a script it should call with lat, long params and then it will have 10 seconds to execute, if it runs longer it will be killed.

react-native-background-timer does not use any of the functions described in the Table 3-1 and that is why the app will be killed after 3 minutes, no matter what you do..

@theonlinenerd
Copy link

@Stoffern I am using table 3-1 and using it as fetch now in the focus it is explained it will wake the application to fire whatever written.

If this is the case still this timer will not run ?

Thank you and will appreciate any help or direction I have to follow.

@st0ffern
Copy link
Contributor Author

It is explained on the page if you read everything under the paragraph that is about the service you use.

@rclai
Copy link

rclai commented Mar 2, 2017

Shouldn't this be in the docs?

@st0ffern
Copy link
Contributor Author

st0ffern commented Mar 2, 2017

It is in the iOS docs, the repo just claims a feature that does not work in real life.

@gshotwell
Copy link

In case anyone is interested in a workaround, this seems to work for me:

  1. Start background timer in componentWillMount()
  2. Play a silent sound during the time when you want the background timer to be active. This can be something like react-native-sound file which is playing on repeat
  3. Add the following to your info.plist file:
<key>UIBackgroundModes</key>
<array>
  <string>audio</string>
</array>

Note that you should still abide by the apple terms of service, and only do the above if you are in one of their accepted use-cases for background playing. In my case the code inside of the backgorund timer was all related to playing audio so I think I'm okay.

@bportman
Copy link

Hey I used @gshotwell's workaround above and it works great when nobody is using their device. We send location heartbeats to a server to track people's arrival and departure from a job site. Unfortunately, if the user plays audio in other apps on the device it will pause the silent background audio and lead to the app being put to sleep by the OS. Do we have a workaround that works consistently and doesn't require playing a silent audio? This is critical for my app.

@st0ffern
Copy link
Contributor Author

I find all this very hacky solutions, and what i said over a year ago.
This breaks with the iOS policy 😉

@bportman
Copy link

Completely agree - that's why I am trying to find a solution that doesn't use silent audio. Apple clearly states on their developer docs that background location services are an approved function of a background task, and my app in the app store has been approved with background modes - location services enabled. The issue seems to be (at least I suspect) that react native's built in geolocation polyfill does not comply with Apple's guidelines for running a background location task. This in spite of the fact that the react native docs seem to suggest it should work.

@shafiqshams
Copy link

Not working

@saif0347
Copy link

saif0347 commented Sep 9, 2020

@gshotwell your solution worked for me as well. Not only device lock but it also works when app is closed/minimised in iOS.

@cksiow
Copy link

cksiow commented Nov 12, 2022

In case anyone is interested in a workaround, this seems to work for me:

  1. Start background timer in componentWillMount()
  2. Play a silent sound during the time when you want the background timer to be active. This can be something like react-native-sound file which is playing on repeat
  3. Add the following to your info.plist file:
<key>UIBackgroundModes</key>
<array>
  <string>audio</string>
</array>

Note that you should still abide by the apple terms of service, and only do the above if you are in one of their accepted use-cases for background playing. In my case the code inside of the backgorund timer was all related to playing audio so I think I'm okay.

Thanks, this fix the issue, I believe because of sound play, OS will not suspend the thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests