capacitor-background-geolocation
Capacitor plugin for enabling background geolocation service
Maintainer | GitHub |
---|---|
Damiano Brunori | seididieci |
This plugin actually works only in android. It creates a foreground service (with a notification on android) to keep your app getting location updates.
Using npm:
npm install capacitor-background-geolocation
Sync native files:
npx cap sync
method | info | platform |
---|---|---|
initialize |
initialize/start service and configure | web/android |
start |
starts the service getting location updates | web/android |
stop |
stops the service getting location updates | web/android |
goForeground |
bring the service on foreground (showing a notification) | android |
stopForeground |
bring the service back to the bacground | android |
import { Plugins } from '@capacitor/core';
const { BackgroundGeolocation } = Plugins;
import {
BgLocationEvent,
BgGeolocationAccuracy,
} from 'capacitor-background-geolocation';
BackgroundGeolocation.addListener('onLocation', (location: BgLocationEvent) => {
console.log('Got new location', location);
// Put your logic here.
});
BackgroundGeolocation.addListener('onPermissions', (data: BgPermissions) => {
console.log('BGLocation permissions:', location);
// Start geolocation if user granted location permisiions
if (data.fineLocation)
BackgroundGeolocation.start();
});
BackgroundGeolocation.initialize({
notificationText: 'Your app is running, tap to open.',
notificationTitle: 'App Running',
updateInteval: 10000,
requestedAccuracy: BgGeolocationAccuracy.HIGH_ACCURACY,
// Small icon has to be in 'drawable' resources of your app
// if you does not provide one (or it is not found) a fallback icon will be used.
smallIcon: 'ic_small_icon',
// Start getting location updates right away. You can set this to false or not set at all (se below).
startImmediately: true,
});
// After user accept permissions the handler above will start the service
BackgroundGeolocation.requestPermissions();
BackgroundGeolocation.stop();
// Force the service to run in foreground
// It will show the android notification also when your app is up and running
BackgroundGeolocation.goFroreground();
// Restore the service to run in backgroud/default mode: the android notification will be shown only when your app goes in background.
BackgroundGeolocation.stopForeground();
Keep in mind that the plugin will send the service in the foreground when your APP is going into background until you stop the service or quit the APP.
Remember to add this plugin to your app main acctivity:
import com.getcapacitor.community.bglocation.BackgroundGeolocation; //.... // Initializes the Bridge this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{ // Additional plugins you've installed go here // <Your other plugins if any> add(BackgroundGeolocation.class); }});The
AndroidManifest.xml
should be automaticcaly filled with the right permissions through acap sync
command but sometimes it doesn't...
Check that there areFOREGROUND_SERVICE
andACCESS_FINE_LOCATION
permissions. in your App Manifest.
This plugin is not yet implemented iOS side, if someone wants to help I will appreciate it...
capacitor-background-geolocation is 100% free and open-source, under the MIT license. Use it however you want.