ocetnik / react-native-background-timer

Emit event periodically (even when app is in the background)
MIT License
1.61k stars 225 forks source link

the task is dying out #467

Open SokolovRU opened 1 year ago

SokolovRU commented 1 year ago

maybe I'm testing it incorrectly, but when I launch the application and look at the console, the task works, but when I minimize the application, it stops running within a minute, even less.

dragorwyin commented 1 year ago

Have same issue, returning to foreground restores task again.

punov commented 1 year ago

Have the same problem, Android, "react": "^18.2.0", "react-native": "^0.70.6", "react-native-background-timer": "^2.4.1",

Interval stops after 1-2 minutes of running in the background.

punov commented 1 year ago

I was able to fix it by providing more permissions to the Application,

"Apps" > <Select your app> > Remove permissions if app is unused turned OFF
"Apps" > <Select your app> > Battery > Unrestricted 

I can assume it will not suit some of your needs, but if you have to make your app unstoppable, it can save your day.

chj-damon commented 1 year ago

@punov you mean change those settings on your phone?

robertsonasc commented 1 year ago

@punov that work for me too, thanks man

Flobeney commented 1 year ago

I have the same problem but for iOS 16.2, and using the following versions :

"react": "18.2.0",
"react-native": "0.71.2",
"react-native-background-timer": "^2.4.1",

On iOS, I can't access those permissions directly, do you know how I can fix that ? Maybe by requesting equivalent permission directly to the user ? I was thinking about "Background app refresh" but my app doesn't even show up there

lauhon commented 1 year ago

Same here (at least on iOs) and the same struggle with react-native-background-actions

To me it seems this is just normal behaviour, since the ios part relies on beginBackgroundTaskWithname. As it states there the background tasks have an assigned time which they get to run, after that time the process is stalled.

@ocetnik Would it be possible to use "normal" Background Tasks in iOs to prevent the process to be stopped so soon?

Im willing to contribute to make this happen, I just don't know if Background Tasks will perform as desired

abakers commented 11 months ago

We solved this on Android by asking the user to turn off the battery optimisation (as this is the issue).

Check if the user has battery optimisation turned on:

const isIgnoring = await BackgroundGeolocation.deviceSettings.isIgnoringBatteryOptimizations();

If isIgnoring is false, ask the user to turn it off (using an alert or modal or whatever you like)

const request = await BackgroundGeolocation.deviceSettings.showIgnoreBatteryOptimizations();

And in the onPress:

BackgroundGeolocation.deviceSettings.show(request)

reference: https://transistorsoft.github.io/react-native-background-geolocation/interfaces/devicesettings.html