Closed Saehanseul closed 4 years ago
Sadly, this library will not keep alive the app in the background by itself on iOS. I am working on an iOS 13 solutions using the new BGProcessingTaskRequest
API, but that won't completely fix the issue either. The main problem is with iOS itself and its restrictions on background tasks.
However, you can rely on other libraries like react-native-track-player
that use audio, geolocalization, etc. to keep your app alive in the background while you execute the JS from this library.
I have updated the library documentation about this subject.
You say in the main documentation on the warning part that the problem is that this library wont get the app to live forever in background.. But, will it live for at least 15-20 minutes? And, will this library allow to execute any JS code while on background as long as the app lives?
@d0xi5, it might live for at least 5 minutes. However, iOS can close it as soon as it wants. It depends on your use case.
Hi Is this issue being worked upon currently? Is there a solution for running background process in iOS reliably?
@zendevil, I am sorry, but it is not possible. Take a look at my comment https://github.com/Rapsssito/react-native-background-actions/issues/8#issuecomment-575582339 for more info.
Hi @Rapsssito , reading comment #8 you said to use "react-native-track-player" but it's for music play. Is there a way to keep working without using music player ? location only for example ?
My use case is "Tcp connection + Download files for 5 minutes". Then after 2/3 minutes I want to restart this process
Hi @Rapsssito , reading comment #8 you said to use "react-native-track-player" but it's for music play. Is there a way to keep working without using music player ? location only for example ?
My use case is "Tcp connection + Download files for 5 minutes". Then after 2/3 minutes I want to restart this process
Hey I too have some similar use case
Actually I need a background process which looks out for data from a source for certain interval of time. That too I need specifically for iOS. Will try if I can use react-native-track-player for keeping the app alive
@Osric38, check the docs for more info:
However, you can rely on other libraries like react-native-track-player that use audio, geolocalization, etc
The issue here is that iOS does not allow your app to run in the background without a very specific reason. The main problem is with iOS itself and its restrictions on background tasks. Take a look at the iOS docs about this topic.
@Rapsssito I know about this restriction, but with react-native-track-player, I will block user for reading his personnal musics, no ?
Have you already tried "react-native-background-geolocation" ?
In IOS 13, it seems background is allowed for some processing actions, have you tried this point ?
@Osric38 I have not tested it with react-native-background-geolocation. Make sure to use react-native-track-player if the sound is a real part of the app, if it is not, your app will get rejected in the App Store.
Take into account that running the React Context is a relatively heavy processing action. However, I will take a look at the possibilities offered by iOS 13 as soon as possible.
Did we ever get any progress on this?
Because I do not even think my const veryIntensiveTask = async (taskDataArguments) => {
is running on iOS. I basically need to do a GEO LOCATE and FETCH every so often even if the user is using another app like google maps.
I got one error POST TIMEOUT, Location timed out
to my BE and then when in iOS setting to tell it to just let the app always use location, but since never anything unless app is in the foreground.
@russmenum, for geolocation purposes, I highly recommend using just pure react-native-background-geolocation
, react-native-background-actions
works as a more general purpose background processing with limited functionality in iOS caused by iOS design.
@Rapsssito , I am confused about how that is different? In looking at that it looks like all the calls run in the foreground so would not work while the app is not in the foreground? Maybe moot as after install trying to use it is crashing my app.
If it will work even when iOS app is in the background or another app is in use; great, but looks like all the doc is class bassed; would you know of any functional hook-based exsamples? I need hooks
Please do something. Flutter has already had flutter_workmanage that relied on Android's WorkManager, iOS' performFetchWithCompletionHandler and iOS BGAppRefreshTask. Could you do it in React Native?
Hi @russmenum , @Ppang0405 You can keep your application working in background on IOS using : react-native-background-geolocation
In your App.js: First include library:
import BackgroundGeolocation from 'react-native-background-geolocation';
Then in main component:
componentDidMount = () => {
// This handler fires whenever bgGeo receives a location update.
BackgroundGeolocation.onLocation(this.onLocation, this.onError);
// This handler fires when movement states changes (stationary->moving; moving->stationary)
BackgroundGeolocation.onMotionChange(this.onMotionChange);
// This event fires when a change in motion activity is detected
BackgroundGeolocation.onActivityChange(this.onActivityChange);
// This event fires when the user toggles location-services authorization
BackgroundGeolocation.onProviderChange(this.onProviderChange);
// This event on heartbeat interval
BackgroundGeolocation.onHeartbeat(this.onHeartbeat.bind(this));
////
// 2. Execute #ready method (required)
//
BackgroundGeolocation.ready({
// Geolocation Config
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_VERY_LOW,
//disableLocationAuthorizationAlert: 0,
distanceFilter: 1,
// Activity Recognition
stopTimeout: 1,
// Application config
debug: false, // <-- enable this hear sounds for background-geolocation life-cycle.
logLevel: BackgroundGeolocation.LOG_LEVEL_INFO,
stopOnTerminate: true, // <-- Allow the background-service to continue tracking when user closes the app.
startOnBoot: false, // <-- Auto start tracking when device is powered-up.
// HTTP / SQLite config
url: null, //'http://yourserver.com/locations',
batchSync: false, // <-- [Default: false] Set true to sync locations to server in a single HTTP request.
autoSync: true, // <-- [Default: true] Set true to sync each location to server as it arrives.
headers: { // <-- Optional HTTP headers
"X-FOO": "bar"
},
params: { // <-- Optional HTTP params
"auth_token": "maybe_your_server_authenticates_via_token_YES?"
},
preventSuspend: true, //IOS
heartbeatInterval: 15,
//activityType: 'AutomotiveNavigation', //IOS
pauseLocationUpdates: false, //IOS
saveBatteryOnBackground: false, //IOS
maxRecordsToPersist: 2,
logMaxDays: 1,
locationAuthorizationAlert : {
cancelButton: 'auth.cancel',
settingsButton: 'auth.settings',
instructions: 'auth.instructions',
titleWhenNotEnabled: 'auth.title_when_not_enabled',
titleWhenOff: 'auth.title_when_off',
}
}, (state) => {
console.log('App - BackgroundGeolocation is configured and ready: ', state.enabled);
if (!state.enabled) {
////
// 3. Start tracking!
//
BackgroundGeolocation.start(function() {
console.log('App - Start success');
});
}
});
}
onLocation(location) {
console.log('App - BG location - ' , location.timestamp , ' - Moving: ' , location.isMoving);
}
onError(error) {
console.log('App - BG Error -' , error);
}
onActivityChange(event) {
console.log('App - BG activity change - ' , event.activity);
}
onProviderChange(provider) {
console.log('App - BG provider change - Enabled:' , provider.enabled
, ' - Status: ' , provider.status
, ' - Network: ' , provider.network);
}
onMotionChange(event) {
console.log('App - BG motion change - ' , event.timestamp , ' - Moving: ' , event.isMoving);
}
onHeartbeat(params) {
console.log('App - BG heartbeat - ' , params.location.timestamp , ' - Moving: ' , params.location.isMoving);
}
Then you can do action in handler you want.
On Android you can use : react-native-background-actions
I've tried a lot of ways, even tricked the system by using react-native-track-player and react-native-background-geolocation but all led to the same result: When I tried on iOS 14 and 15, the background tasks (react-native-background-actions, react-native-background-geolocation, and react-native-track-player) was only being implemented for 30 seconds, while on Android or iOS 12 it was working fine.
Then I've found this comment
We haven’t had the old 10-minute window for years so that is simply no longer applicable. That was reduced to 3 minutes in OS 7 and further reduced to 30 seconds in iOS 13. (There is a new concept of background tasks, for running tasks longer than 30 seconds, but the OS will run these at its own discretion, e.g. at night when the user is charging their device, so that’s not really applicable here).
So I don't think it could be possible to efficiently implement the background tasks on recent iOS versions.
In my own use case, the system would have some data to sync to the cloud and I was trying to implement background tasks to handle the case when the app goes to the background. Since it's no longer implementable, I've changed the way to approach the problem by using notification, so that I will send noti to users after an amount of time if they still have unsynced data.
P/S: I could do the trick with the track player, I've added some audio to keep the app alive in the background for over 30 seconds, and then seemed it did work, just not sure how long would it keep the app alive. But since my app is not related to playing any videos or audios, this could be rejected by Apple but if yours do have, then you can think about it and use it with your own risks.
I'm finding module which working at IOS 13. I was used react-native-backgroun-timer. but it's not working at IOS 13. I think it's because of new ios "background processing".
I wanna know about this module cover IOS 13.