question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JS errors with BackgroundGeolocation.finish() on Android with Ionic 2

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
JackLewisGitcommented, Dec 20, 2016

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

import { Platform } from 'ionic-angular'

if( !this.platform.is( 'android' ) ) { 
    BackgroundGeolocation.finish(); 
}
1reaction
shaneparsonscommented, Jun 7, 2017

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Native: tried calling BackgroundGeolocation.finish, but the ...
The problem is that the ionic-native declarations are not up to date with the plugin. finish() has been replaced with endTask().
Read more >
Ionic background geolocation issue - Stack Overflow
I want that if the app is removed from the background, the app background geolocation service should work. Any Help? background · ionic2...
Read more >
BackgroundGeolocation plugin not installed ionic V3
Hello ,i'm facing a strange ERROR when i try to fire the backgroundGeolocation.configure it throws app-scripts] plugin is not installed.
Read more >
cordova-background-geolocation-lt - npm
The most sophisticated background location-tracking & geofencing module with battery-conscious motion-detection intelligence for iOS and Android ...
Read more >
Ionic 5 Complete guide on Geolocation, permission and auto ...
Part 2— Ionic 5 with Angular and Cordova (Pending) ... can run on both iOS and Android (and browser!), that too with the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found