2.2.4AndroidAndroid 6.0, API 23Android Studio emulator - Nexus 5 API 23 2 (Android 6.0, API 23)6.3.12.0.0-rc.1 (2016-10-13)Installed platforms:
android 5.2.2
ios 4.1.1
let config = {
desiredAccuracy: 0,
stationaryRadius: 20,
distanceFilter: 10,
debug: true,
interval: 2000
};
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);
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
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
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);
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);
Unable to use plug-in without getting errors on either iOS or Android.
Logs include console.log messages from within app.
Android without BackgroundGeolocation.finish-1478157603179.txt
Android with BackgroundGeolocation.finish-1478157089114.txt
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.
Most helpful comment
You can check if the device is running ios before calling finish.