Cordova-plugin-background-geolocation: JS errors with BackgroundGeolocation.finish() on Android with Ionic 2

Created on 3 Nov 2016  路  5Comments  路  Source: mauron85/cordova-plugin-background-geolocation

Your Environment

  • Plugin version: 2.2.4
  • Platform: Android
  • OS version: Android 6.0, API 23
  • Device manufacturer and model: Android Studio emulator - Nexus 5 API 23 2 (Android 6.0, API 23)
  • Cordova version: 6.3.1
  • Ionic version: Ionic 2 project using Ionic 2.0.0-rc.1 (2016-10-13)
  • Cordova platform version:
Installed platforms:
  android 5.2.2
  ios 4.1.1
  • Plugin configuration options:
    let config = {
       desiredAccuracy: 0,
       stationaryRadius: 20,
       distanceFilter: 10,
       debug: true,
       interval: 2000
    };
  • Link to your project:
    Recreated Ionic 2 background geolocation project from Josh Morony

Context

Building Ionic2 app with background geolocation. Was able to recreate the bug using Josh Morony's instructions on how to add bg-geo to Ionic 2 apps. It appears the BackgroundGeolocation.finish() method throws errors on Android. When I added it to the success callback function as per docs (so that iOS doesn't kill background processes) it throws errors on Android.

This solution causes error on Android, but OK for iOS:

BackgroundGeolocation.configure(
      (location) => {
          console.log('Background location update: ' + location.latitude + ' ' + location.longitude);
          // Code to run update inside Angular's zone
          BackgroundGeolocation.finish();  
      },
      (error) => {
          console.error('Background location error', error);
      }, config);

This solution OK for Android, but iOS kills background process:

BackgroundGeolocation.configure(
      (location) => {
           console.log('Background location update: ' + location.latitude + ' ' + location.longitude);
           // Code to run update inside Angular's zone
           // no call to BackgroundGeolocation.finish()
      },
      (error) => {
           console.error('Background location error', error);
      }, config);

Expected Behavior

When BackgroundGeolocation.finish() is included in callback, no errors received:

Background location update: 45.39999833333333 -119.4  
Background location zone.run()  
Calling BackgroundGeolocation.finish()  
// NO ERROR HERE  
Foreground location update: 45.3999983 -119.4 

Actual Behavior

When BackgroundGeolocation.finish() is included in callback, we get following error:

Background location update: 45.89999833333333 -119.09999833333335  
Background location zone.run()  
Calling BackgroundGeolocation.finish()  
Unhandled Promise rejection: Invalid action ; Zone: <root> ; Task: Promise.then ; Value: Invalid action undefined  
Error: Uncaught (in promise): Invalid action  
    at s (file:///android_asset/www/build/polyfills.js:3:8546)  
    at file:///android_asset/www/build/polyfills.js:3:8296  
    at Object.cordova.callbackFromNative (file:///android_asset/www/cordova.js:295:52)  
    at processMessage (file:///android_asset/www/cordova.js:1081:17)  
    at processMessages (file:///android_asset/www/cordova.js:1104:9)  
    at t.invoke (file:///android_asset/www/build/polyfills.js:3:13400)  
    at e.run (file:///android_asset/www/build/polyfills.js:3:10787)  
    at file:///android_asset/www/build/polyfills.js:3:8889  
    at t.invokeTask (file:///android_asset/www/build/polyfills.js:3:14029)  
    at e.runTask (file:///android_asset/www/build/polyfills.js:3:11389)  
Foreground location update: 45.1 -119.0999983

Steps to Reproduce


  1. Create Ionic 2 project per article from Josh Morony
  2. Per article, don't include BackgroundGeolocation.finish();, project will run fine on Android, but iOS will kill background process
BackgroundGeolocation.configure(
      (location) => {
           console.log('Background location update: ' + location.latitude + ' ' + location.longitude);
           // Code to run update inside Angular's zone
           // NO CALL to BackgroundGeolocation.finish()
      },
      (error) => {
           console.error('Background location error', error);
      }, config);
  1. Per plug-in docs, include BackgroundGeolocation.finish();, project will run fine on iOS, but will get errors on Android
BackgroundGeolocation.configure(
      (location) => {
          console.log('Background location update: ' + location.latitude + ' ' + location.longitude);
          // Code to run update inside Angular's zone
          BackgroundGeolocation.finish();  
      },
      (error) => {
          console.error('Background location error', error);
      }, config);

Context


Unable to use plug-in without getting errors on either iOS or Android.

Debug logs


Logs include console.log messages from within app.

Android without BackgroundGeolocation.finish-1478157603179.txt
Android with BackgroundGeolocation.finish-1478157089114.txt

android bug enhancement ionic

Most helpful comment

You can check if the device is running ios before calling finish.

import { Platform } from 'ionic-angular'

if( !this.platform.is( 'android' ) ) { 
    BackgroundGeolocation.finish(); 
}

All 5 comments

Not sure how this error is possible, since I'm not using Promises. This can be potentially fixed by implementing dummy finish method for android.

You can check if the device is running ios before calling finish.

import { Platform } from 'ionic-angular'

if( !this.platform.is( 'android' ) ) { 
    BackgroundGeolocation.finish(); 
}

Thanks for the tip @JackLewisGit

Just to be clear...the finish() method is only needed for iOS?

@bionicbrad Yes, finish() is only for iOS as you can see in the docs. Therefore, use the following:

if (this.platform.is('ios')) { 
    BackgroundGeolocation.finish(); 
}

Also, make sure to use this.backgroundGeolocation.stop(); to stop the tracking, and not the finish method.

P.S. I've learned to never expect any of Josh's tutorials to work right off the bat, as there are usually plugin changes he doesn't account / update for.

Finish method was replaced with startTask/endTask in version 3.x, so closing this one.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adrieldubemarante picture adrieldubemarante  路  4Comments

wrunkel picture wrunkel  路  6Comments

vlafranca picture vlafranca  路  6Comments

maurojones picture maurojones  路  3Comments

Shakti-Sharma picture Shakti-Sharma  路  3Comments