react-native-background-geolocation
react-native-background-geolocation copied to clipboard
Background Geolocation Issue on iOS
YOUR ENVIRONMENT: 1.Plugin version:"4.8.1 && 4.0.1" ---->react-native-background-geolocation :4.8.1 ---->react-native-background-fetch:4.0.1 2.Platform: iOS 3.OS version:11.4 4.Device manufacturer / model: iPhone7,iIPhone:11,iPhone :12mini 5.React Native version (react-native -v):"0.64.4" 6.Plugin config:react-native-background-geolocation && react-native-background-fetch.
CODE:
import BackgroundGeolocation from 'react-native-background-geolocation';
import BackgroundFetch from 'react-native-background-fetch';
const initBackgroundFetch = async () => {
// BackgroundFetch event handler.
const onEvent = async taskId => {
//console.log('[BackgroundFetch] task: ', taskId);
// Do your background work...
//await this.addEvent(taskId);
switch (taskId) {
case 'com.hm.happisales':
showNotification({
notification: {
body: 'Please Turn on GPS in your mobile',
title: 'GPS OFF',
},
});
break;
default:
console.log('Default fetch task');
}
// IMPORTANT: You must signal to the OS that your task is complete.
BackgroundFetch.finish(taskId);
};
// Timeout callback is executed when your Task has exceeded its allowed running-time.
// You must stop what you're doing immediately BackgroundFetch.finish(taskId)
const onTimeout = async taskId => {
//console.warn('[BackgroundFetch] TIMEOUT task: ', taskId);
BackgroundFetch.finish(taskId);
};
// Initialize BackgroundFetch only once when component mounts.
let status = await BackgroundFetch.configure(
{
stopOnTerminate: false,
startOnBoot: true,
enableHeadless: true,
minimumFetchInterval: 15,
},
onEvent,
onTimeout,
);
//console.log('[BackgroundFetch] configure status: ', status);
};
const StartLocationTracking = async () => {
interval = (await GetFetchLocationInterval()) || 60;
const token = await GetAuthToken();
// to initial setup for background task
initBackgroundFetch();
//-23:59
let schedule_date_time = Moment(new Date()).format('yyyy-MM-DD HH:mm');
schedule_date_time = schedule_date_time + '-23:59';
console.log(schedule_date_time);
//console.log('[interval ====> ] ', updateInterval);
// 1. Subscribe to events.
BackgroundGeolocation.onLocation(location => {
//console.log('[onLocation ===>]', location);
if (location !== null) {
location.extras.gps = gpsStatus === true ? 1 : 0;
[[location.extras.net](http://location.extras.net/)](http://location.extras.net/) = netStatus === true ? 1 : 0;
}
});
BackgroundGeolocation.onMotionChange(event => {
});
// This handler fires on HTTP responses
BackgroundGeolocation.onHttp(response => {
});
BackgroundGeolocation.onActivityChange(event => {
//console.log('[onActivityChange]', event);
});
BackgroundGeolocation.onProviderChange(event => {
//&& AppState.currentState.match(/background|inactive/)
if (!event.gps) {
if (AppState.currentState.match(/background|inactive/)) {
showNotificationinBackground();
} else {
showNotification({
notification: {
body: 'Please Turn on GPS in your mobile',
title: 'GPS OFF',
},
});
}
}
gpsStatus = event.gps;
netStatus = event.network;
});
BackgroundGeolocation.onSchedule(async state => {
let enabled = state.enabled;
//console.log('[onSchedule] - enabled? ', enabled);
if (!enabled) {
PutCheckInTime('');
PutCheckInData('');
await updateCheckout();
AnalyticsHandler('Auto Checkout Successfully', {
});
if (AppState.currentState.match(/background|inactive/)) {
//BackHandler.exitApp();
RNExitApp.exitApp();
} else {
//BackHandler.exitApp();
RNExitApp.exitApp();
}
}
});
BackgroundGeolocation.onHeartbeat(event => {
//console.log('[onHeartbeat] ', event);
});
/// 2. ready the plugin.
BackgroundGeolocation.ready({
schedule: [schedule_date_time],
scheduleUseAlarmManager: true,
// Geolocation Config
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
disableElasticity: true,
distanceFilter: 0,
locationUpdateInterval: interval * 1000,
debug: false, // <-- enable this hear sounds for background-geolocation life-cycle.
logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
stopOnTerminate: false, // <-- Allow the background-service to continue tracking when user closes the app.
enableHeadless: true,
startOnBoot: true, // <-- Auto start tracking when device is powered-up.
// HTTP / SQLite config
url: 'https://api2.happisales.com/beacon/devicestatv2',
autoSync: true, // <-- [Default: true] Set true to sync each location to server as it arrives.
headers: {
// <-- Optional HTTP headers
authorization: Bearer ${token},
},
extras: {
net: netStatus === true ? 1 : 0,
gps: gpsStatus === true ? 1 : 0,
},
notification: {
title: 'Live Tracking',
text: 'Running',
priority: BackgroundGeolocation.NOTIFICATION_PRIORITY_HIGH,
},
backgroundPermissionRationale: {
message:
'In order to track your activity in the background, please enable {backgroundPermissionOptionLabel} location permission',
},
foregroundService: true,
// for ios configuration
stationaryRadius: 25,
showsBackgroundLocationIndicator: true,
heartbeatInterval: 60,
preventSuspend: true,
disableStopDetection: true,
pausesLocationUpdatesAutomatically: false,
allowIdenticalLocations: true,
// general
stopOnStationary: true,
isMoving: true,
}).then(state => {
console.log(
'- BackgroundGeolocation is configured and ready: ===> ',
state.enabled,
);
if (!state.enabled) {
BackgroundGeolocation.startSchedule();
} else {
//BackgroundGeolocation.stopSchedule();
}
});
};
const StopLocationTracking = () => {
BackgroundGeolocation.ready({}).then(state => {
console.log(
'- BackgroundGeolocation is configured and ready: @@@@ ',
state.enabled,
);
if (state.enabled) {
BackgroundGeolocation.stopSchedule();
BackgroundGeolocation.stop();
//BackgroundGeolocation.removeAllListeners();
}
});
};
EXPECTED BEHAVIOUR: We need to fetch the background location for iOS. When the app is in the foreground , background states and app is closed , we want to fetch the background location at 500 meters of radius. These are the scenarios in which we want to work.
ACTUAL BEHAVIOUR: For me, App is in the foreground It will fetch the background location. when the app is in background and closed. It will not fetch the background location.i will not fetch the location at any certain meter radius. actually we want work certain range of radius.
STEPS TO REPRODUCE: 1.When the app in foreground..it will be fetching background location. 2.When the app in background.it will not be fetching background location at any certain meter of radius. 3.When the app force closed .it will not be fetching background location at any certain meter of radius.
CONTEXT: When the app is in background . It will not fetch the background location at certain meter of radius.
DEBUG LOGS. Logs:🔴-[TSLocationManager stopUpdatingLocation] 2023-07-26 13:32:05.089377+0530 happisalesreactnative[7051:3283490] ℹ️-[TSDBLogger db_save] Log committed
How can we contact you?so Please share the mobile number.we are in emergency stage.so kindly resolve this issue as soon as possible.
Tech support is strictly handled through GitHub issues.
do you have a question about what I wrote in #1776?
How is this issue any different than the one you posted 3 hours ago #1776?
Yes,I have doubt on that #1776. you are saying fetch the background location at 200 meter of radius. i can understand but we can not able to fetch the any background location at any certain meter of radius.that is the issue for me.
I suggest you test in the iOS simulator. Simulate location with “Freeway Drive”.
when the device moves ~200 meters, tracking will automatically engage, regardless if app is in background or app terminated.
you must make sure that your app code calls .ready(config) each and every time your app boots.
Okay once i will try.let you know
I’ve been field testing iOS almost every day for the last 9 years. It never fails to track me.
Again I’m facing the issue.when I force close the app and again i open the app.background location is not fetching i moved 300 meter of radius.after force closing the app it will not fetch the background location again i moved 300 meter of radius.these two scenario are not working for me.so please explain me clearly for that scenario .
Your code in the original post is a mess, with all tabs/spaces removed. I cannot read that. Edit your post and fix your code tabs.
I suggest you try installing and field-testing my demo app. Do not modify the code. Just clone the repo and install it on your device. Then go outside and field-test it to compare performance with your own app.
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.