Ionic-native: `@ionic-native/background-geolocation:BackgroundGeolocation.configure()` returns `Promise<any>` instead of the documented `Observable<BackgroundGeolocationResponse>`

Created on 23 Jan 2019  路  12Comments  路  Source: ionic-team/ionic-native

I'm submitting a ... (check one with "x")
[X] bug report
[ ] feature request
[ ] support request => Please do not submit support requests here, use one of these channels: https://forum.ionicframework.com/ or https://ionicworldwide.herokuapp.com/

Current behavior:
@ionic-native/background-geolocation:BackgroundGeolocation.configure() is defined as returning a value of type Promise<any>. This prevents subscribeing to the location stream as documented. Compiling the snippet taken from https://ionicframework.com/docs/native/background-geolocation/ gives the following error:

ERROR in src/app/app.component.ts(...): error TS2339: Property 'subscribe' does not exist on type 'Promise<any>'.

Expected behavior:
@ionic-native/background-geolocation:BackgroundGeolocation.configure() should be defined as returning a value of type Observable<BackgroundGeolocationResponse>, as documented.

Steps to reproduce:
N/A

Related code:

insert any relevant code here

Other information:

This issue is present in the v4 and v5 branches:

Ionic info:

Ionic:

   ionic (Ionic CLI)             : 4.7.1 (/usr/local/lib/node_modules/ionic)
   Ionic Framework               : @ionic/angular 4.0.0-rc.0
   @angular-devkit/build-angular : 0.8.9
   @angular-devkit/schematics    : 0.8.9
   @angular/cli                  : 6.2.9
   @ionic/angular-toolkit        : 1.2.2

Cordova:

   cordova (Cordova CLI) : 8.1.2 ([email protected])
   Cordova Platforms     : not available
   Cordova Plugins       : not available

System:

   Android SDK Tools : 25.3.1 (/Users/sean/Library/Android/sdk)
   NodeJS            : v10.15.0 (/usr/local/bin/node)
   npm               : 6.4.1
   OS                : macOS Mojave
   Xcode             : Xcode 10.1 Build version 10B61

Most helpful comment

I am having the problem, and I cannot make any workaround at the moment.
Tried "npm install --save @ionic-native/[email protected]" but this is not helping me the least since I then have a dependency error of not using
"@ionic-native/[email protected]" ( I am using "@ionic-native/core"@5.0.0")
Then I receive "PLUGIN_NOT_INSTALLED" error in the log

Uninstall background geolocation and use the following commands

cordova plugin add cordova-plugin-mauron85-background-geolocation@2
$ npm install --save @ionic-native/background-geolocation@3

I am having the problem, and I cannot make any workaround at the moment.
Tried "npm install --save @ionic-native/[email protected]" but this is not helping me the least since I then have a dependency error of not using
"@ionic-native/[email protected]" ( I am using "@ionic-native/core"@5.0.0")
Then I receive "PLUGIN_NOT_INSTALLED" error in the log

Uninstall background geolocation and use the following commands

cordova plugin add cordova-plugin-mauron85-background-geolocation@2
$ npm install --save @ionic-native/background-geolocation@3

All 12 comments

use "@ionic-native/background-geolocation": "^5.0.0-beta.21" , is defined with subscribe, maybe its a merge error or something.

With the code this.backgroundGeolocation.configure(config).subscribe((location:BackgroundGeolocationResponse)

if we use ngx then .subscribe is an issue, without an issue .configure becomes an issue, though we should all be using ngx at this point I assume.

import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationResponse } from '@ionic-native/background-geolocation/ngx';

Thanks @nicolasza, we're now able to compile with this workaround.

@ionic-native/background-geolocation": "^5.0.0-beta.21

where to put this.. can you please explain?
Thank you in Advance.

@thesurya9 Put it in the dependencies section of your package.json, or use npm install --save @ionic-native/[email protected] to add it to your package.json and install it in one go.

Hi, i'm using Ionic3. This workaround doesn't work. I receive an error on this constructor variable
public backgroundGeolocation: BackgroundGeolocation

The BackgroundGeolocation is not found!

Why?

Hi, i'm using Ionic3. This workaround doesn't work. I receive an error on this constructor variable
public backgroundGeolocation: BackgroundGeolocation

The BackgroundGeolocation is not found!

Why?

What you need to do is replace the standard install described here https://ionicframework.com/docs/native/background-geolocation/ with the beta version that does not have this issue.

You also need an NgZone to push the results back into ngAngular

You may also want to look up @ionic-native/diagnostic and @ionic-native/open-native-settings to make sure that the user has the correct permission settings in the app to allow tracking

npm rm --save @ionic-native/background-geolocation ;
npm install --save @ionic-native/[email protected];

You will also need to repeat this if you do an npm update;

Important -->> Remember to add /ngx or the fix will not work

And /ngx onto your references in app.modules.ts

import {NgZone } from '@angular/core';

import { Geolocation, Geoposition } from '@ionic-native/geolocation/ngx';

import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationResponse } from '@ionic-native/background-geolocation/ngx';

  constructor(

    public backgroundGeolocation: BackgroundGeolocation,
   private ngZone: NgZone,

  )

  public  _backgroundGEO_getGEO() {


    try {

      const config: BackgroundGeolocationConfig = {
        desiredAccuracy: 10,
        stationaryRadius: 20,
        distanceFilter: 30,
        debug: false,
        stopOnTerminate: false,
        interval: 300000,
        fastestInterval: 300000,
        activitiesInterval: 300000,
        startForeground: true,
        stopOnStillActivity: true,
        activityType: 'AutomotiveNavigation',
        saveBatteryOnBackground: true,
        maxLocations: 10,
      };
      try {
         this.backgroundGeolocation.configure(config).subscribe((location: BackgroundGeolocationResponse) => {

          //only track during the day
          var today = new Date().getHours();
          if (today >= 7 && today <= 22) {

            console.log("bg _backgroundGEO subscribed");

            if (location) {

              //console.log("bg location", location);
              if (location.latitude && location.longitude) {

                this.ngZone.run(() => {

                  this.lat = location.latitude;
                  this.lng = location.longitude;

                  //do something with the resutls


                });

                try {
                  this.backgroundGeolocation.finish();
                } catch (e) {
                  console.error("big hu?", e);
                }
              }
            } else {
              //console.error("No Background Location yet! " + new Date().toISOString())
            }
          }
        });

      } catch (e) {
        console.error("big hu?", e);
      }

    } catch (e) {
      console.error("big hu?", e);
    }
    try {
      this.backgroundGeolocation.start();
    } catch (e) {
      console.error("big hu?", e);
    }



  }


}

I am having the problem, and I cannot make any workaround at the moment.
Tried "npm install --save @ionic-native/[email protected]" but this is not helping me the least since I then have a dependency error of not using
"@ionic-native/[email protected]" ( I am using "@ionic-native/core"@5.0.0")
Then I receive "PLUGIN_NOT_INSTALLED" error in the log

I am having the problem, and I cannot make any workaround at the moment.
Tried "npm install --save @ionic-native/[email protected]" but this is not helping me the least since I then have a dependency error of not using
"@ionic-native/[email protected]" ( I am using "@ionic-native/core"@5.0.0")
Then I receive "PLUGIN_NOT_INSTALLED" error in the log

@5.0.0 wont work.

https://www.npmjs.com/package/@ionic-native/core/v/5.0.0-beta.1?activeTab=versions

I think the editor breaks the link,

put this"@ionic-native/background-geolocation": "^5.0.0-beta.21", directly into your package.json in "dependencies"

then under "cordova" -> "plugins" add/replace

    "cordova-plugin-mauron85-background-geolocation": {
        "GOOGLE_PLAY_SERVICES_VERSION": "11+",
        "ANDROID_SUPPORT_LIBRARY_VERSION": "23+",
        "ICON": "@mipmap/icon",
        "SMALL_ICON": "@mipmap/icon",
        "ACCOUNT_NAME": "@string/app_name",
        "ACCOUNT_LABEL": "@string/app_name",
        "ACCOUNT_TYPE": "$PACKAGE_NAME.account",
        "CONTENT_AUTHORITY": "$PACKAGE_NAME",
        "ALWAYS_USAGE_DESCRIPTION": "This App requires access to your phones gelocation ",
        "MOTION_USAGE_DESCRIPTION": "This App requires requires motion detection"
      },

then manualy delete app\plugins\cordova-plugin-mauron85-background-geolocation

In config.xml

use these settings...

    <plugin name="cordova-plugin-mauron85-background-geolocation" spec="~2.3.6">
        <variable name="GOOGLE_PLAY_SERVICES_VERSION" value="11+" />
        <variable name="ANDROID_SUPPORT_LIBRARY_VERSION" value="23+" />
        <variable name="ICON" value="@mipmap/icon" />
        <variable name="SMALL_ICON" value="@mipmap/icon" />
        <variable name="ACCOUNT_NAME" value="@string/app_name" />
        <variable name="ACCOUNT_LABEL" value="@string/app_name" />
        <variable name="ACCOUNT_TYPE" value="$PACKAGE_NAME.account" />
        <variable name="CONTENT_AUTHORITY" value="$PACKAGE_NAME" />
        <variable name="ALWAYS_USAGE_DESCRIPTION" value="This App requires geolocation tracking" />
        <variable name="MOTION_USAGE_DESCRIPTION" value="This App requires motion detection" />
    </plugin>

I am having the problem, and I cannot make any workaround at the moment.
Tried "npm install --save @ionic-native/[email protected]" but this is not helping me the least since I then have a dependency error of not using
"@ionic-native/[email protected]" ( I am using "@ionic-native/core"@5.0.0")
Then I receive "PLUGIN_NOT_INSTALLED" error in the log

Uninstall background geolocation and use the following commands

cordova plugin add cordova-plugin-mauron85-background-geolocation@2
$ npm install --save @ionic-native/background-geolocation@3

I am having the problem, and I cannot make any workaround at the moment.
Tried "npm install --save @ionic-native/[email protected]" but this is not helping me the least since I then have a dependency error of not using
"@ionic-native/[email protected]" ( I am using "@ionic-native/core"@5.0.0")
Then I receive "PLUGIN_NOT_INSTALLED" error in the log

Uninstall background geolocation and use the following commands

cordova plugin add cordova-plugin-mauron85-background-geolocation@2
$ npm install --save @ionic-native/background-geolocation@3

If you really want to use v4 and subscribe to an observable, you can still convert the promise to observable:

import { from } from 'rxjs/observable/from';
...
    Observable.from(
      this.backgroundGeolocation.configure(config),
    ).subscribe((location: BackgroundGeolocationResponse) => {
      console.log('Posting stored geolocation');
      const data = {
        source: location.provider,
        accuracy: location.accuracy,
        ts: new Date(location.time),
        lat: location.latitude,
        lng: location.longitude,
        speed: location.speed,
        user: AuthService.currentUser(),
      };
      this.restProvider
        .postGeoLocation(data)
        .then(() => {
          console.log('Pushed geo-locations');
        })
        .catch(e => console.log('Could not push geo-location: ' + e));
      this.backgroundGeolocation
        .finish()
        .then(() => {
          console.log('Finished');
        })
        .catch(e => console.log("reject (probably you're on android): " + e)); // FOR IOS ONLY
    });

@LuckyMasango thanks its worked for me but tell me 1 thing sir in my case backgroundGeolocation is fire only 1 time in foreground sometime fire sometime not fire in background tell me sir what i do for test its not working in the background why?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shaliko picture shaliko  路  3Comments

danbucholtz picture danbucholtz  路  3Comments

hobbydevs picture hobbydevs  路  3Comments

ghost picture ghost  路  3Comments

Manduro picture Manduro  路  3Comments