You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are developing a Delivery Partner App using React Native version 0.73.6. The core functionality of our app requires constant access and tracking of the user's current location, even when the app is closed or running in the background.
To achieve this, we have integrated the react-native-background-actions library, version 4.0.1, and followed all the steps outlined in the documentation for Android.
While the app does run in the background initially, we are facing issues with the app not staying in the background for an extended period across various Android devices. The duration for which the app remains active in the background is inconsistent and varies from device to device.
Here is a summary of our issue:
The app needs to access and track the user's current location continuously.
We have used the react-native-background-actions library as per the documentation.
The app runs in the background but stops after an unpredictable amount of time on different Android devices.
Code Example:
import{useEffect,useRef,useState}from'react';import{PermissionsAndroid,Platform,StyleSheet,Text,useAnimatedValue}from'react-native';import{SafeAreaProvider}from'react-native-safe-area-context';importNavigatorfrom'./main/navigations';importBootSplashfrom"react-native-bootsplash";import{GestureHandlerRootView}from'react-native-gesture-handler';import{BottomSheetModalProvider}from'@gorhom/bottom-sheet';importBackgroundServicefrom'react-native-background-actions'import{requestLocationPermission}from'./main/components/fire_store';importAsyncStoragefrom'@react-native-async-storage/async-storage';constsleep=(time: any)=>newPromise((resolve: any)=>setTimeout(()=>resolve(),time));// You can do anything in your task such as network requests, timers and so on,// as long as it doesn't touch UI. Once your task completes (i.e. the promise is resolved),// React Native will go into "paused" mode (unless there are other tasks running,// or there is a foreground app).constveryIntensiveTask=async(taskDataArguments: any)=>{// Example of an infinite loop taskconst{ delay, current }=taskDataArguments;awaitnewPromise(async(resolve)=>{for(leti=0;BackgroundService.isRunning();i++){console.log('count value============',i);// Fetch the updated value of current from AsyncStorageconststoredCurrent=awaitAsyncStorage.getItem('currentLocation');constupdatedCurrent=storedCurrent ? parseFloat(storedCurrent) : current;awaitrequestLocationPermission();awaitBackgroundService.updateNotification({taskDesc: `My count is running ${i}, lat:${updatedCurrent}`});awaitsleep(delay);}});};constoptions={taskName: 'Example',taskTitle: 'ExampleTask title',taskDesc: 'ExampleTask description',taskIcon: {name: 'ic_launcher',type: 'mipmap',},color: '#ff00ff',linkingURI: 'yourSchemeHere://chat/jane',// See Deep Linking for more infoparameters: {delay: 1000,// 1 second current: null,// This will be updated later},};constApp=((): JSX.Element=>{const[current,setCurrent]=useState(null);useEffect(()=>{constloadCurrent=async()=>{try{conststoredCurrent=awaitAsyncStorage.getItem('currentLocation');if(storedCurrent!==null){setCurrent(parseFloat(storedCurrent));}else{setCurrent(null);}}catch(error){console.error('Failed to load current from storage:',error);}};constinterval=setInterval(loadCurrent,1000);return()=>clearInterval(interval);},[]);useEffect(()=>{constinit=async()=>{startBackgroundService();};init();},[current]);// Add current as a dependencyconststartBackgroundService=async()=>{// const allPermissionsGranted = await requestAllPermissions();// if (!allPermissionsGranted) {// Alert.alert("Permissions Required", "All permissions are required to start the background service.");// return;// }constisRunning=awaitBackgroundService.isRunning();if(!isRunning){awaitrequestAllPermissions();try{options.parameters.current=current;// Update current in optionsconsole.log('Trying to start background service');awaitBackgroundService.start(veryIntensiveTask,options);console.log('Successful start!');}catch(e){console.log('Error',e);}}else{console.log('Background service is already running');}};constrequestAllPermissions=async()=>{try{constfineLocationGranted=awaitPermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);constbackgroundLocationGranted=awaitPermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION);constnotificationGranted=awaitrequestNotificationPermission();return(fineLocationGranted===PermissionsAndroid.RESULTS.GRANTED&&backgroundLocationGranted===PermissionsAndroid.RESULTS.GRANTED&¬ificationGranted);}catch(err){console.log(err);returnfalse;}};constrequestNotificationPermission=async()=>{if(Platform.OS==='android'){// check if the platfrom is Android 13 or higherif(Platform.Version>=33){constpermission=awaitPermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS);return(permission===PermissionsAndroid.RESULTS.GRANTED)}returntrue;// Permissions are granted by default for Android versions below 13}else{returntrue;}};return(<BottomSheetModalProvider><GestureHandlerRootViewstyle={{flex: 1}}><SafeAreaProvider><Navigator/></SafeAreaProvider></GestureHandlerRootView></BottomSheetModalProvider>)})exportdefaultApp;
Integrate react-native-background-actions in a React Native app.
Follow the setup instructions for Android as per the documentation.
Start the background service to track location.
Observe the app's behavior on different Android devices.
Expected Behavior:
The app should run in the background indefinitely, consistently tracking the user's location across all Android devices.
Actual Behavior:
The app runs in the background initially but stops after an unpredictable duration, varying across different Android devices.
Additional Information:
React Native version: 0.73.6
Library version: 4.0.1
Affected platforms: Android
Devices tested: Various Android devices with different OS versions
We would greatly appreciate any guidance or solutions to ensure our app remains active in the background consistently across all devices. What other permissions or configurations do we need to keep the app running in the background all the time?
Hi,
We are developing a Delivery Partner App using React Native version 0.73.6. The core functionality of our app requires constant access and tracking of the user's current location, even when the app is closed or running in the background.
To achieve this, we have integrated the
react-native-background-actions
library, version 4.0.1, and followed all the steps outlined in the documentation for Android.While the app does run in the background initially, we are facing issues with the app not staying in the background for an extended period across various Android devices. The duration for which the app remains active in the background is inconsistent and varies from device to device.
Here is a summary of our issue:
react-native-background-actions
library as per the documentation.Code Example:
AndroidManifest.xml:
Steps to Reproduce:
react-native-background-actions
in a React Native app.Expected Behavior:
The app should run in the background indefinitely, consistently tracking the user's location across all Android devices.
Actual Behavior:
The app runs in the background initially but stops after an unpredictable duration, varying across different Android devices.
Additional Information:
We would greatly appreciate any guidance or solutions to ensure our app remains active in the background consistently across all devices. What other permissions or configurations do we need to keep the app running in the background all the time?
Thank you!
Link to the
react-native-background-actions
documentation: react-native-background-actionsPlease let me know if any further information is required. @mhmdnasser124 @mhmdnofal @Rapsssito @shergin @mdvacca @daku
The text was updated successfully, but these errors were encountered: