I have tried to get the location when the app is in background or the screen is locked/closed it works good on the development when i take the build and run the application the task that i have added is not executing. How to solve this issue i have given my code below.
ReactNativeForegroundService.register({id: 1244});
const addTask = () => {
ReactNativeForegroundService.add_task(
() => {
console.log('task executed');
getLocation();
},
{
delay: 15000,
onLoop: true,
taskId: 1244,
},
);
};
const startForegroundService = () => {
addTask();
ReactNativeForegroundService.update({
id: 1244,
title: 'Sales 10X',
message: 'Sales 10X is running in background for live tracking ',
icon: 'ic_launcher',
});
};
const stopForegroundService = () => {
ReactNativeForegroundService.stop(1244);
};
const appState = useRef(AppState.currentState);
const appStateSubscription = useRef(null);
const handleAppStateChange = nextAppState => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === 'active'
) {
console.log(
'App has come to the foreground! Stopping foreground service.',
);
stopForegroundService();
} else if (nextAppState === 'background') {
console.log('App is in the background! Starting foreground service.');
startForegroundService();
}
appState.current = nextAppState;
console.log('AppState', appState.current);
};
useEffect(() => {
let data = userDetailData?.userDetails;
if (
data &&
data?.permission_list &&
data?.permission_list?.includes('usertracking')
) {
console.log('change in app state');
appStateSubscription.current = AppState.addEventListener(
'change',
handleAppStateChange,
);
}
return () => {
appStateSubscription.current?.remove();
ReactNativeForegroundService.stop(1244);
// RNLocation.stopUpdatingLocation();
};
}, [userDetailData]);
I have tried to get the location when the app is in background or the screen is locked/closed it works good on the development when i take the build and run the application the task that i have added is not executing. How to solve this issue i have given my code below.