-
Notifications
You must be signed in to change notification settings - Fork 425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Please provide me the code in React Native for both iOS and Android. #590 #2156
Comments
Edit your issue and enter the required info: Plugin version: |
I have updated it. Please check. |
|
I have updated it. Please check. |
Meaningless. open your package.json and look! It’s not hard to determine the version of any dependency you’re using. |
Also.
provide the Config you’re sending to |
|
Should I provide all my code to you? |
That version of THIS PLUG-IN does not exist. https://github.com/transistorsoft/react-native-background-geolocation/blob/master/CHANGELOG.md |
^4.17.1 |
I have updated it. Please check. |
Search api docs (linked in the table of contents of the readme here) “Config.locationTemplate”.
This is meta-data: search api docs “Config.params” to learn how to append your own arbitrary meta-data.
The plug-in does not do “reverse geocoding”. The plug-in provides no mechanism for querying an address from latitude/longitude. That sort of thing is best done on the application server in a background job. |
Please provide me the code in React Native for both iOS and Android. My API endpoint is: https://bfsi.staypinc.com/api/admin/add/dummy/data The API request should look like this: |
See api docs Config.locationTemplate. the comprehensive and searchable api docs are linked in the Table of Contents at the top of the readme. |
I'm using my API: https://bfsi.staypinc.com/api/admin/add/dummy/data, and I'm sending the latitude and longitude I receive from your BackgroundGeolocation to my API. They are being stored in my database. However, when I kill the app, I receive null values. I need the code for that. |
The plug-in does not send “null values”. show me the JSON your server receives showing “null values”. |
{ |
Should I provide you with my code? |
That is not data from the plug-in. That is your own representation of data you received. The plug-in does not send “null values”. I suggest you have a close look at your server code. |
this is my app.js file code const App = () => { // Function to send the location manually to the API
}; useEffect(() => {
}, []); // Start/stop BackgroundGeolocation based on the enabled state // Initialize Background Fetch
}, []); return ( export default App; index.js // Headless task to handle background location fetch when the app is terminated try {
} catch (error) { // Must call finish on the task AppRegistry.registerComponent(appName, () => App); |
Please learn to syntax highlight “fenced code blocks”: |
import React, { useEffect, useState } from 'react';
import { Switch, Text, View } from 'react-native';
import BackgroundGeolocation from 'react-native-background-geolocation';
import BackgroundFetch from 'react-native-background-fetch';
const App = () => {
const [enabled, setEnabled] = useState(false);
const [location, setLocation] = useState('');
// Function to send the location manually to the API
const callApiWithLocation = (loc) => {
const coords = loc.coords;
const latitude = coords?.latitude;
const longitude = coords?.longitude;
console.log('latitude', latitude);
console.log('longitude', longitude);
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
const raw = JSON.stringify({
"latitude": latitude.toString(),
"longitude": longitude.toString(),
"device": 'android',
"address": 'TEST'
});
const requestOptions = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow"
};
fetch("https://bfsi.staypinc.com/api/admin/add/dummy/data", requestOptions)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.error('Error:', error));
};
useEffect(() => {
// Subscribe to BackgroundGeolocation events
const onLocation = BackgroundGeolocation.onLocation((loc) => {
console.log('[onLocation]', loc);
setLocation(JSON.stringify(loc, null, 2));
callApiWithLocation(loc); // Call the API when location is updated
});
const onMotionChange = BackgroundGeolocation.onMotionChange((event) => {
console.log('[onMotionChange]', event);
});
const onActivityChange = BackgroundGeolocation.onActivityChange((event) => {
console.log('[onActivityChange]', event);
});
const onProviderChange = BackgroundGeolocation.onProviderChange((event) => {
console.log('[onProviderChange]', event);
});
// Initialize BackgroundGeolocation
BackgroundGeolocation.ready({
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
distanceFilter: 10,
stopTimeout: 5,
debug: true,
logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
stopOnTerminate: false, // Keep tracking when the app is terminated
startOnBoot: true, // Start tracking when the device is booted
heartbeatInterval: 60, // Send heartbeat every 60 seconds
enableHeadless: true, // Allow running in headless mode (when app is terminated)
foregroundService: true, // Keep service running in the foreground
autoSync: true, // Automatically send location data to the server
batchSync: false, // Don't batch locations for syncing
maxDaysToPersist: 1, // Persist data for 1 day
url: 'https://bfsi.staypinc.com/api/admin/add/dummy/data', // API endpoint for location updates
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_TOKEN"
},
params: {
"device_id": "your-device-id"
}
}).then(state => {
setEnabled(state.enabled);
console.log("- BackgroundGeolocation is configured and ready: ", state.enabled);
});
return () => {
onLocation.remove();
onMotionChange.remove();
onActivityChange.remove();
onProviderChange.remove();
};
}, []);
// Start/stop BackgroundGeolocation based on the enabled state
useEffect(() => {
if (enabled) {
BackgroundGeolocation.start();
} else {
BackgroundGeolocation.stop();
setLocation('');
}
}, [enabled]);
// Initialize Background Fetch
useEffect(() => {
BackgroundFetch.configure({
minimumFetchInterval: 15, // Fetch every 15 minutes
stopOnTerminate: false, // Continue fetching even after app is terminated
startOnBoot: true, // Start fetching when the device is booted
enableHeadless: true // Ensure it works when the app is killed
}, async (taskId) => {
console.log("[BackgroundFetch] taskId:", taskId);
// Fetch the latest location manually
BackgroundGeolocation.getCurrentPosition({
persist: true,
samples: 1,
timeout: 30
}).then(loc => {
console.log("[BackgroundFetch] Location:", loc);
callApiWithLocation(loc); // Call your API
});
BackgroundFetch.finish(taskId);
}, (error) => {
console.log("[BackgroundFetch] failed to start:", error);
});
}, []);
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Click to enable BackgroundGeolocation</Text>
<Switch value={enabled} onValueChange={setEnabled} />
<Text style={{ fontFamily: 'monospace', fontSize: 12 }}>{location}</Text>
</View>
);
};
export default App;
|
You seem to be doing your own HTTP requests in addition to providing the plug-in an I suggest you do error checking on the data you receive from the plug-in before executing your http requests. If you’re doing your own http request, i suggest you remove the |
Btw, do you realize that the HeadlessTask is an Android-only mechanism? There’s no such thing as headless iOS. |
I am currently only testing on an Android device, not iOS. |
The plug-in is not responsible for invalid data of your own custom http requests. I suggest you error-check the data you’re sending before you send it. |
But when I start the app, I am receiving the correct latitude and longitude, and I am passing it to my API. However, when the app is put into kill mode, I receive null values. I needed a solution for this. Please check my code in two files: app.js and index.js. Please provide me with the updated working code. |
I suggest you observe the plugin logs in |
Your Environment
react-native -v
): React Native 0.71.xPASTE_YOUR_CODE_HERE
Expected Behavior
Please provide me the code in React Native for both iOS and Android.
My API endpoint is: https://bfsi.staypinc.com/api/admin/add/dummy/data
The API request should look like this:
{
"latitude": "30.34567",
"longitude": "10.90781",
"device": "ios",
"address": "TALKAT TEST TEST"
}
I need to send latitude and longitude to my API when the app is in the killed state, background, or foreground. I only need the code for this functionality.
Actual Behavior
Steps to Reproduce
Context
Debug logs
Logs
The text was updated successfully, but these errors were encountered: