cordova-plugin-background-geolocation: Not working alpha on ionic-android
- Plugin version: 3.0.0-alpha.37
- Platform: Android
- OS version: Ubuntu 16.04LTS
- Device manufacturer and model: Motorola Moto G
- Cordova version (
cordova -v
): 7.1.0 - Cordova platform version (
cordova platform ls
): android 6.3.0 - Plugin configuration options:
{notificationTitle: 'AP - Interno',
notificationText: 'Em execução',
desiredAccuracy: 10,
stationaryRadius: 50,
distanceFilter: 50,
debug: false, // enable this hear sounds for background-geolocation life-cycle.
stopOnTerminate: false, // enable this to clear background location settings when the app terminates
startOnBoot: true,
locationProvider: 0, /** ANDROID_DISTANCE_FILTER_PROVIDER: 0, ANDROID_ACTIVITY_PROVIDER: 1 */
interval: 10000,
fastestInterval: 5000,
activitiesInterval: 10000}
Context
Need upgrade from 2.x for 3.x why 2.x is not working has expected.
Expected Behavior
Init the plugin
Actual Behavior
The console is showing:
Native: tried calling BackgroundGeolocation.configure, but the BackgroundGeolocation plugin is not installed.
Install the BackgroundGeolocation plugin: ‘ionic cordova plugin add cordova-plugin-mauron85-background-geolocation’
When i run ionic cordova run android, however the plugin is installed. In package.json i see "cordova-plugin-mauron85-background-geolocation": "^3.0.0-alpha.37"
and in config.xml <plugin name="cordova-plugin-mauron85-background-geolocation" spec="^3.0.0-alpha.37">
Possible Fix
Steps to Reproduce
Install an fresh ionic aplication in latest version, install plugin and try run backgroundGeolocation.configure(config).subscribe
Context
This bug break my update to 3.x branch
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 25 (1 by maintainers)
ok, I’ll try to explain as clearly as I can
Cordova plugins
A cordova plugin is a Javascript wrapper around native functions. It offers Javascript APIs that can be used in any Javascript code, with or without framework.
Ionic-native
Ionic-native is a TypeScript wrapper around cordova plugins. In other words, it’s an additional layer. So, when you call an ionic-native Typescript method, this method in turn calls the corresponding cordova Javascript method. The benefits of this additional layer are:
import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationResponse } from '@ionic-native/background-geolocation'
, those objects that you import define their attributes and methods, which helps you during developmentWhy the Ionic-native wrapper doesn’t work with v3.x
very simply, because you changed the name of your global object, from
backgroundGeolocation
toBackgroundGeolocation
so, when you call the ionic-native wrapper, it in turn callsbackgroundGeolocation
, which doesn’t exist anymore; and that’s why it tells you that you didn’t install the pluginSo, how can I use the cordova plugin in my Ionic application?
Well, ionic-native is absolutely not a mandatory thing to use a cordova plugin; it’s just an additional layer that helps people who develop in TypeScript. So, very simply, you can totally use a cordova plugin in an Ionic application just by following the documentation from that plugin!
Note also that ionic-native can absolutely be used in non-Ionic applications. Again, it’s just a TypeScript wrapper, it’s called “Ionic” because it’s the same team behind it, but that’s it.
Hope I was clear!
Hi @cvaliere
(<any>window).BackgroundGeolocation;
is indeed working. I just had to remove/add the android platform again. Thanks a lot!Yeah that’s one of my issues. You cannot use subscribe since it isn’t returning an observable. If you look at the documentation, it’s all plain javascript. We dont get the other methods provided by Ionic’s native wrapper. Unless Ionic updates their native library to use v3, we won’t get the same functionality as v2.3.6
Oh thankk you so much I got it !
Hi,
Just declare the global variable
declare var BackgroundGeolocation: any;
and then use it e.g.BackgroundGeolocation.configure(options);
That works for me.I’ve found a way to work. I need remove the import (needed from version 2) and change the way of use the class. I never saw like this syntax, for me it’s new. Maybe help you to fix them.
See the gist with the hack i’ve need do to this work: https://gist.github.com/rafwell/d1ef287e253134550049f72895d1fb5f
And the old way: https://gist.github.com/rafwell/c8eb0b3b877f76091cf61cab2f36641f