1

When app runs in foreground, i execute successfully a POST-request using setTimeout with 20 seconds like this:

setTimeout(function() {
 /*POST request is done here*/ 
}, 20000);

Though, if in meanwhile user places the app on background, by pressing home button for example, then my post won't get fired after 20 seconds. On Android though, i checked that if 20 seconds get passed, and then i open the app(make it be in foreground), then post is executed immediately. (Haven't tested on ios to see if same thing happens). So i have the following questions:

1) Same behavior is happening on IOS side too?

2) Should i expect exactly same behavior if timeout had 15 mins timer and not 20 seconds?

3) How can i solve this problem? I need to contact to an endpoint after some time, and can't do it while app is in background with setTimeout.

Using libraries like react-native-background-task do not help in this situation, as i need to execute code after exactly 20 secs(or 10 mins etc) and not some time in future like 15 mins+

angelos_lex
  • 1,593
  • 16
  • 24

2 Answers2

0

try this library it sets time what you want to set, but remember ios won't allow the time more than 30 sec

https://github.com/ocetnik/react-native-background-timer

Ammar Tariq
  • 799
  • 2
  • 13
  • 29
0

I've had same kind of problem, and I used react-native-background-timer and it worked perfectly for Android.

meta4
  • 788
  • 1
  • 10
  • 24
  • How long is the maximum timeout that i can set? Can i add timeout after 15 minutes? On both platforms? I can't find detailed information about it, any help highly appreciated! – angelos_lex Nov 20 '18 at 18:52
  • ios won't allow more than 30sec in background unless you are backed by native code – Ammar Tariq Nov 20 '18 at 20:02
  • for android you could add any timeout as long as app is running is background – meta4 Nov 21 '18 at 09:24