react-native-background-location-on-kill v1.0.1
Peer dependency
react-native-background-actions is a peer dependency, therefore it must be installed in the parent project to use this package.
Installation guide for Peer dependency package
To install react-native-background-actions see https://www.npmjs.com/package/react-native-background-actions.
Getting started
Installation: npm i react-native-background-location-on-kill
Import the following functions from react-native-background-location-on-kill
import { configureTask, startTracking, stopTracking } from 'react-native-background-location-on-kill';
Usage
Configure the background task in App.js
configureTask({ taskName: 'Location Tracking', taskTitle: 'Tracking Location', taskDesc: 'Your Location is being tracked', taskIcon: { name: 'ic_launcher', type: 'mipmap', }, color: '#ffffff', });
Start Background Location Service
To start the background location task, create a watchPosition function and pass in the background task.
Example watchPosition function, see https://www.npmjs.com/package/@react-native-community/geolocation for further assistance.
const watchPosition = () => { watchID = Geolocation.watchPosition( (position) => { // You will get updated location from here }, (error) => { console.log(error); // handle the error }, { enableHighAccuracy: true, timeout: 20000, maximumAge: 0, distanceFilter: 5 } // Geolocation options. ); }
Pass the above watchPosition function in the startTracking() function to start the background location service.
const location = await startTracking(watchPosition);
After starting the background location function a notification icon will appear in the notification indicating that the task is running.
Stop Background Location Service
To stop the background task use the stopTracking() function.
await stopTracking();
Notification icon will disappear when stopTracking() function is called indicating that the background location task is stopped.