Open nipundixit2016 opened 2 weeks ago
When you post a block of code, you MUST syntax highlight it. I’ve told you this at least 4 times. It is not hard. You surround your code in triple-backticks (+optional language):
i have updated
.onLocation
), you do so BEFORE calling .ready(config)
, not within the Promise
resolution.// Register listeners HERE <-------------------------
BackgroundGeolocation.onLocation((location) => {
console.log('[onLocation]', location);
});
BackgroundGeolocation.ready(config).then((state) => {
// DO NOT REGISTER EVENT-LISTENERS HERE.
});
Go outside for a nice long walk of at least 1km. With debug: true
, the plugin will emit debugging sound-effects to let you it's working. To leran what these debugging sound-effects mean, read the API docs Config.debug
. The searchable API docs are conveniently linked in the README in the Table of Contents
About "wiring up event-listeners", the Example in the README clearly states the process. You need to stop "throwing crap at the wall" and carefully read and understand how this plugin works.
If you want periodic location updates (eg: about every 15 minutes) , you can do so by calling .getCurrentPosition
in your BackgroundFetch
callback.
BackgroundFetch.configure({
minimumFetchInterval: 15, // <-- every 15 minutes is the fastest you'll get.
enableHeadless: true,
stopOnTerminate: false
}, async (taskId) => {
console.log('[BackgroundFetch]', taskId);
const location = await BackgroundGeolocation.getCurrentPosition({
samples: 3 // record 3 locations and return the most accurate of the three
});
console.log('[getCurrentPosition]', location);
BackgroundFetch.finish(taskId);
}, async (taskId) => {
console.log('[BackgroundFetch] TIMEOUT:', taskId);
BackgroundFetch.finish(taskId);
});
By periodic time everything working properly but when creating release build. it not working in that build.
await saveToLocalStorage('positionsArray', JSON.stringify(positionsArray));
You don't need to store locations in localStorage
. This plugin has its own built-in SQLite database.
Those recorded locations can be retrieved via .getLocations()
and the database cleared via .destroyLocations()
.
The plugin also hosts its own built-in HTTP service. See API docs HTTP Guide for more information on all the plugin's HTTP Features.
By periodic time everything working properly but when creating release build. it not working in that build.
You were not using BackgroundGeolocation
before — you were using some other plugin named Geolocation
.
If you have a problem, see Wiki Debugging and learn how to fetch the plugin's logs via .emailLog
I am replacing Geolocation by BackgroundGeolocation hoping it works in release build.
I am replacing Geolocation by BackgroundGeolocation hoping it works in release build.
Do not "hope" -- test it yourself with a local realease
build on your own device FIRST.
With your device plugged into USB:
$ npx react-native run-android --mode=release
and when running locally, ALWAYS observe $ adb logcat *:S TSLocationManager:V
in a terminal window to view realtime logs from the plugin.
This plugin's logging system is incredibly verbose and prints absolutely everything that it's doing:
If you don't know how to use adb logcat
, learn how to
And of course you probably didn't read how to simulate Android background-fetch
tasks in a terminal window with adb shell
$ adb shell cmd jobscheduler run -f <your.application.id> 999
'''
import React, { useEffect } from 'react'; import { AppRegistry, Platform, Alert } from 'react-native'; import App from './App'; import { name as appName } from './app.json'; import BackgroundGeolocation from 'react-native-background-geolocation';
const MainApp = () => { useEffect(() => { // Define a callback for location updates const onLocation = (location) => { console.log('[onLocation] -', location); // Handle location update };
// Define a callback for errors
const onError = (error) => {
console.warn('[onError] -', error);
// Handle error
};
// Define a callback for the state change
const onStateChange = (state) => {
console.log('[onStateChange] -', state);
// Handle state changes
};
// Define a callback for geofence events
const onGeofence = (geofence) => {
console.log('[onGeofence] -', geofence);
// Handle geofence events
};
// Set up event listeners
BackgroundGeolocation.onLocation(onLocation, onError);
// BackgroundGeolocation.onError(onError);
// BackgroundGeolocation.onStateChange(onStateChange);
BackgroundGeolocation.onGeofence(onGeofence);
// Configure BackgroundGeolocation
BackgroundGeolocation.ready({
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
distanceFilter: 10, // Minimum distance (in meters) before update
stopOnTerminate: false, // Continue tracking when the app is terminated
startOnBoot: true, // Start tracking on device boot
enableHeadless: true, // Enable headless mode for background tasks
locationUpdateInterval: 60000, // Update interval in milliseconds
notificationTitle: "Background Tracking",
notificationText: "Enabled",
}).then((state) => {
if (!state.enabled) {
// Start tracking if not already enabled
BackgroundGeolocation.start();
}
}).catch((error) => {
console.error('Error configuring BackgroundGeolocation:', error);
});
return () => {
// Clean up listeners on unmount
BackgroundGeolocation.removeListeners();
};
}, []);
return
AppRegistry.registerComponent(appName, () => MainApp);
'''
The [`] backtick character is not the [‘] single-quote.
https://superuser.com/questions/254076/how-do-i-type-the-tick-and-backtick-characters-on-windows
I suggest you Google how to find the [`] backtick character for your keyboard.
Your Environment
react-native -v
):0.74.0Expected Behavior
Return location each 2min while app is foreground , background or killed state
Actual Behavior
only getting once when BackgroundGeoLoaction.start()
Steps to Reproduce
1. 2. 3. 4.
Context
Debug logs
Logs