Capacitor Support for Background Geolocation

Transistor Software
2 min readJun 4, 2021

Both background-geolocation and background-fetch now have pure Capacitor implementations!

See the Github repos:

The Javascript API is almost identical to the Cordova version, with just one caveat: you must use the Promise api only. For example:

// Old API with optional callbacks as method-arguments is
// no longer supported, eg: methodName(successFn)
// use Promise api, eg: methodName().then(successFn)
// OLD callback args not supported.
BackgroundGeolocation.ready(config, (state) => {
console.log('[ready] state:', state);
});
// NEW: Only Promise API is supported:
BackgroundGeolocation.ready(config).then((state) => {
console.log('[ready] state', state);
});
// Or use await in an async function
const state = await BackgroundGeolocation.ready(config);
console.log('[ready] state', state);
// OLD: callback args not supported.
BackgroundGeolocation.getState((state) => {
console.log('[getState]', state);
});
// NEW: Only Promise API is supported:
BackgroundGeolocation.getState().then((state) => {
console.log('[getState]', state);
});
// Or use await in an async function
const state = await BackgroundGeolocation.getState();
console.log('[getState]', state);

However, adding event-listeners never returned a Promise — those do accept a callback argument, just as in the Cordova version — there is no difference with adding event-listeners between the Cordova and Capacitor version.

// Adding event-listeners is the same as always:
BackgroundGeolocation.onLocation((location) => {
console.log('[onLocation]', location);
}, (error) => {
console.log('[onLocation] ERROR', error);
});
BackgroundGeolocation.onMotionChange((location) => {
console.log('[onMotionChange]', location);
});

--

--

Transistor Software

Creator of Background Geolocation SDK. Professional plugin developers — geolocation specialists. https://www.transistorsoft.com