Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background Geolocation does not work under iOS #444

Closed
HamzaLJ opened this issue Aug 22, 2017 · 42 comments
Closed

Background Geolocation does not work under iOS #444

HamzaLJ opened this issue Aug 22, 2017 · 42 comments
Labels

Comments

@HamzaLJ
Copy link

HamzaLJ commented Aug 22, 2017

I am using Ionic framework to develop my app, I've followed the documentation, everything works fine on Android; however on iOS it shows the blue bar that says that the app is tracking the current location on the background (only for the first 40 seconds), then that blue bar disappears and the app stop tracking the current location.

Your Environment

  • Plugin version: cordova-plugin-mauron85-background-geolocation 2.2.5 "CDVBackgroundGeolocation"
  • Platform: iOS
  • OS version: iOS 10
  • Device manufacturer / model: iPhone 6
  • Cordova version (cordova -v): 7.0.1
  • Cordova platform version (cordova platform ls): ios 4.4.0
  • Plugin config

Expected Behavior

It should track the user current location in iOS even if the app goes to the background.

Actual Behavior

When going to the background, it shows the blue bar that says that the app is taking the current location on the background (only for the first 40 seconds), then that blue bar disappears and stop tracking the current location.

On the other hand, I have the same code working perfectly on Android devices, so I guess that there is an issue with the iOS.

Steps to Reproduce

Just follow the documentation on the following link : https://ionicframework.com/docs/native/background-geolocation/

Then run the app under iOS

Context

Track drivers location and update the database of the current driver location each 10 seconds.

@christocracy
Copy link
Member

This is not the Mauron85 version. Mauron85 is a fork from over 3 years ago of my original open source project.

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 22, 2017

So I need now to install the latest using cordova plugin add cordova-background-geolocation-lt ? Even purchase the license to make it work ?

@christocracy
Copy link
Member

christocracy commented Aug 22, 2017 via email

@christocracy
Copy link
Member

As you can see, responses to issues is quite rapid here.

You'll be lucky to get any response at all from Mauron85.

@christocracy
Copy link
Member

I see you re-posted your issue over at Mauron85.

You'll get no help there. You're on your own.

"You get what you pay for"

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 23, 2017

Well, I can't afford paying 300$ as I am currently a student and I'll be using the plugin for my capstone project

@christocracy
Copy link
Member

If it just for a project and you're not actually submitting to play-store, you can run the Android version in DEBUG builds.

iOS is free

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 23, 2017

I know, but I am required to submit it to the playStore and let people use it and also collect their feedback about it. Not sure, if there exists a special discount for students

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

I've installed the plugin in iOS after removing the mauron85 version and it didn't worked at all, here is the code :

          this.platform.ready().then((ready) => {
          this.configureBackgroundGeolocation(this);
          });

            this.configureBackgroundGeolocation() {
            // 1. Get a reference to the plugin
            this.bgGeo = (<any>window).BackgroundGeolocation;

            // 2. Listen to events
            this.bgGeo.on('location', this.onLocation.bind(this));
            this.bgGeo.on('motionchange', this.onMotionChange.bind(this));


            // 3. Configure it.
            this.bgGeo.configure({
              debug: false,
              desiredAccuracy: 0,
              distanceFilter: 10,
              autoSync: true
            }, (state) => {
              // 4. Start the plugin.
              this.bgGeo.start();
            });
          }

          onLocation(location, taskId) {
             console.log('- location: ', location);
             this.bgGeo.finish(taskId);
           }
           onMotionChange(isMoving, location, taskId) {
             console.log('- motionchange: ', isMoving, location);
             this.bgGeo.finish(taskId);
           }

When I go to the background even that blue bar does not show up, am I doing something wrong?

@christocracy
Copy link
Member

Logs.

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

When the app goes to the background I don't receive any logs, which means that possibly the plugin does not fire when being on the background

@christocracy
Copy link
Member

Why would you use debug: false when something isn't working??

See logLevel

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

I'll change it now to debug: true and let you know what happens

@christocracy
Copy link
Member

The blue bar isn't supposed to show.

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

changed it to debug: true didn't changed anything

@christocracy
Copy link
Member

You'll get no logs without setting logLevel: bgGeo.LOG_LEVEL_VERBOSE

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

Where should I set it ?

@christocracy
Copy link
Member

Did you read the docs for logLevel?

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

I've done the following:

  this.bgGeo.configure({
       debug: true,
       desiredAccuracy: 0,
       distanceFilter: 10,
       logLevel: this.bgGeo.LOG_LEVEL_VERBOSE,
       autoSync: true
     }, (state) => {
       // 4. Start the plugin.
       this.bgGeo.start();
     });`

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

Yes I did.
Even after adding logLevel: this.bgGeo.LOG_LEVEL_VERBOSE I still don't get any logs

@christocracy
Copy link
Member

Then the plugin is not installed.

$ cordova plugins

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

cordova-background-geolocation-lt 2.8.2 "BackgroundGeolocation"
cordova-plugin-actionsheet 2.3.3 "ActionSheet"
cordova-plugin-background-fetch 5.0.0 "CDVBackgroundFetch"
cordova-plugin-cocoalumberjack 0.0.4 "CocoaLumberjack"
cordova-plugin-compat 1.1.0 "Compat"
cordova-plugin-console 1.0.7 "Console"
cordova-plugin-device 1.1.6 "Device"
cordova-plugin-geolocation 2.4.3 "Geolocation"
cordova-plugin-nativeaudio 3.0.9 "Cordova Native Audio"
cordova-plugin-nativestorage 2.2.2 "NativeStorage"
cordova-plugin-network-information 1.3.3 "Network Information"
cordova-plugin-secure-storage 2.6.8 "SecureStorage"
cordova-plugin-splashscreen 4.0.3 "Splashscreen"
cordova-plugin-statusbar 2.2.3 "StatusBar"
cordova-plugin-whitelist 1.3.2 "Whitelist"
cordova.plugins.diagnostic 3.6.5 "Diagnostic"
ionic-plugin-keyboard 2.2.1 "Keyboard"
phonegap-plugin-push 2.0.0 "PushPlugin"
uk.co.workingedge.phonegap.plugin.launchnavigator 3.2.2 "Launch Navigator"

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

As you can see, it's already installed...

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

Seems that this.configureBackgroundGeolocation(this); never fires

@christocracy
Copy link
Member

That would be your problem.

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

Weird, I've placed the following this.configureBackgroundGeolocation(this); in the exact place where mauron85 was. In the case of mauron85 it was firing without problem, but now cordova-background-geolocation-lt does not fire up

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

I've done some changes to be like the following : this.platform.ready().then(this.configureBackgroundGeolocation.bind(this));

It seems that it fired.

Here is what I got :

CDVPlugin class CDVBackgroundGeolocation (pluginName: BackgroundGeolocation) does not exist.
2017-08-24 14:51:54.779030+0100 Onadi Driver[827:126850] ERROR: Plugin 'BackgroundGeolocation' not found, or is not a CDVPlugin. Check your plugin mapping in config.xml.
2017-08-24 14:51:54.779194+0100 Onadi Driver[827:126850] -[CDVCommandQueue executePending] [Line 142] FAILED pluginJSON = ["BackgroundGeolocation760199973","BackgroundGeolocation","addLocationListener",[]]
2017-08-24 14:51:54.779325+0100 Onadi Driver[827:126850] CDVPlugin class CDVBackgroundGeolocation (pluginName: BackgroundGeolocation) does not exist.
2017-08-24 14:51:54.779366+0100 Onadi Driver[827:126850] ERROR: Plugin 'BackgroundGeolocation' not found, or is not a CDVPlugin. Check your plugin mapping in config.xml.
2017-08-24 14:51:54.779421+0100 Onadi Driver[827:126850] -[CDVCommandQueue executePending] [Line 142] FAILED pluginJSON = ["BackgroundGeolocation760199974","BackgroundGeolocation","addMotionChangeListener",[]]
2017-08-24 14:51:54.779484+0100 Onadi Driver[827:126850] CDVPlugin class CDVBackgroundGeolocation (pluginName: BackgroundGeolocation) does not exist.
2017-08-24 14:51:54.779522+0100 Onadi Driver[827:126850] ERROR: Plugin 'BackgroundGeolocation' not found, or is not a CDVPlugin. Check your plugin mapping in config.xml.
2017-08-24 14:51:54.779631+0100 Onadi Driver[827:126850] -[CDVCommandQueue executePending] [Line 142] FAILED pluginJSON = ["BackgroundGeolocation760199975","BackgroundGeolocation","configure",[{"logLevel":5,"autoSync":true,"debug":true,"desiredAccuracy":0,"distanceFilter":10}]]

@christocracy
Copy link
Member

The plugin is not installed properly. Cordova gets confused sometimes.

In your XCode project, you should see CDVBackgroundGeolocation in the /Plugins folder:

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

I didn't found CDVBackgroundGeolocation in the /Plugins folder

I'll re-install the plugin

@christocracy
Copy link
Member

christocracy commented Aug 24, 2017

It's Cordova's responsibility to place those files into the XCode project. Sometimes it fails. You probably uninstalled Mauron85 version after installing mine. The two plugins have identically named ios files CDVBackgroundGeolocation.m/.h, since Mauron85 version is a fork of this project.

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

Now, it works fine only on foreground.

I get this :

ℹ️-[LocationDAO purge:] 1
🔵-[TSLocationManager setPace:] 0
✅-[BackgroundTaskManager createBackgroundTask] 10
🔴-[TSLocationManager stopUpdatingLocation]
2017-08-24 15:09:50.875319+0100 Onadi Driver[871:132422] THREAD WARNING: ['BackgroundGeolocation'] took '58.497803' ms. Plugin should use a background thread.
🎾-[TSLocationManager requestLocation] Acquiring motionchange position 0
✅-[BackgroundTaskManager stopBackgroundTask:]_block_invoke 9 OF (
9,
10
)
🎾-[TSLocationManager startMonitoringBackgroundFetch] BackgroundFetch: ON
2017-08-24 15:09:50.960471+0100 Onadi Driver[871:132422] - location: [object Object]
2017-08-24 15:09:50.962325+0100 Onadi Driver[871:132422] - location: [object Object]
2017-08-24 15:09:50.964551+0100 Onadi Driver[871:132422] - location: [object Object]
2017-08-24 15:09:50.964664+0100 Onadi Driver[871:132422] - location: [object Object]
2017-08-24 15:09:50.964754+0100 Onadi Driver[871:132422] started configureBackgroundGeolocationForIOS [object Object]
⚠️-[TSLocationManager start] Already started (ignored)
2017-08-24 15:09:51.082960+0100 Onadi Driver[871:132422] Upadte current driver location 1414
2017-08-24 15:09:51.083114+0100 Onadi Driver[871:132422] loca [object Object]
2017-08-24 15:09:51.083183+0100 Onadi Driver[871:132422] updateCurrentDriverLocation fired, drv st : true
2017-08-24 15:09:51.083413+0100 Onadi Driver[871:132422] this.driverStatus is true and this.driverOnWay is false
2017-08-24 15:09:51.083731+0100 Onadi Driver[871:132422] rm 2 0
2017-08-24 15:09:51.083932+0100 Onadi Driver[871:132422] update location now
2017-08-24 15:09:51.863261+0100 Onadi Driver[871:132422] **http observable
🔵-[BackgroundTaskManager startPreventSuspendTimer:] BG time remaining: 179 | Total tasks: 1
🔵-[TSLocationManager onSuspend:] enabled? 1)
🔵-[TSLocationManager setPace:] 0
✅-[BackgroundTaskManager createBackgroundTask] 14
🔴-[TSLocationManager stopUpdatingLocation]
🎾-[TSLocationManager requestLocation] Acquiring motionchange position 0
✅-[BackgroundTaskManager stopBackgroundTask:]_block_invoke 10 OF (
10,
14
)
ℹ️-[TSDBLogger db_save] Log committed
2017-08-24 15:09:55.520870+0100 Onadi Driver[871:132422] - location: [object Object]
2017-08-24 15:09:55.521406+0100 Onadi Driver[871:132422] - location: [object Object]
2017-08-24 15:09:55.521756+0100 Onadi Driver[871:132422] - location: [object Object]
2017-08-24 15:09:55.522204+0100 Onadi Driver[871:132422] - location: [object Object]
🔵-[BackgroundTaskManager startPreventSuspendTimer:] BG time remaining: 179 | Total tasks: 1
2017-08-24 15:09:58.935817+0100 Onadi Driver[871:132422] inside currentDriverLocation()
2017-08-24 15:09:58.936980+0100 Onadi Driver[871:132422] fired configureBackgroundGeolocationForIOS
ℹ️-[TSLocationManager on:success:failure:] location
ℹ️-[TSLocationManager on:success:failure:] motionchange

When going to the background, I get the following and stop updating the location

ℹ️-[LocationDAO purge:] 1

╔═══════════════════════════════════════════════════════════
║ -[TSLocationManager stop]
╚═══════════════════════════════════════════════════════════
🔴-[TSGeofenceManager stopMonitoringGeofences]
🔴-[SOMotionDetector stopDetection] OFF
🔴-[TSLocationManager stopUpdatingLocation]
2017-08-24 15:09:59.000041+0100 Onadi Driver[871:132422] [TSBackgroundFetch removeListener]: TSLocationManager
2017-08-24 15:09:59.008064+0100 Onadi Driver[871:132470] CoreLocation: Sending an un-cached message without first clearing the previously cached value
2017-08-24 15:09:59.012594+0100 Onadi Driver[871:132422] THREAD WARNING: ['BackgroundGeolocation'] took '72.111084' ms. Plugin should use a background thread.
🔴-[TSLocationManager stopMonitoringBackgroundFetch] BackgroundFetch: OFF
🔴-[TSLocationManager stopMonitoringSignificantLocationChanges]
✅-[BackgroundTaskManager stopBackgroundTask:]_block_invoke 14 OF (
14
)

@christocracy
Copy link
Member

Background & Terminated are two completely different states. Are you terminating the app?

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

Not at all, only going to the background (for example opening Google maps, etc...)

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

Receiving the following :

🔴-[TSGeofenceManager stopMonitoringGeofences]
🔴-[SOMotionDetector stopDetection] OFF
🔴-[TSLocationManager stopUpdatingLocation]
2017-08-24 15:16:55.330214+0100 Onadi Driver[871:132422] THREAD WARNING: ['BackgroundGeolocation'] took '60.975586' ms. Plugin should use a background thread.
🔴-[TSLocationManager stopMonitoringBackgroundFetch] BackgroundFetch: OFF
🔴-[TSLocationManager stopMonitoringSignificantLocationChanges]

Also, I am getting a notification "Stopped on terminate"

@christocracy
Copy link
Member

Your app is being terminated, either by you or the OS. That's not the plugin's fault.

If you don't want tracking to cease when user or OS terminates your app, configure stopOnTerminate: false

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

I've solved it by accessing to the app settings and checked "Always" to enable background tracking even if the app is in the background.
However, I am already setting a function that should trigger on an interval of 10 seconds but I receive a notification that says "Background fetch received" sometimes after 10 min or sometimes after 15min..
Any idea on what could be wrong?

@christocracy
Copy link
Member

christocracy commented Aug 24, 2017

You do not need to use cordova-plugin-geolocation with this plugin. In fact, it's a bad idea, since cordova-plugin-geolocation requests When In Use permissions while this plugin requests Always.

Whichever plugin requests location first, that's the authorization request which will be show to the user.

This plugin has its own #getCurrentPosition method.

However, I am already setting a function that should trigger on an interval of 10 seconds

When an iOS app is in the background without locations-services on, your app is suspended. If you're hoping to do a setInterval with Javascript while your app is in the background and the device is stationary, that's not going to happen.

a notification that says "Background fetch received" sometimes after 10 min or sometimes after 15min..

This plugin requires my cordova-plugin-background-fetch plugin as a dependency. Read the docs for that plugin. This plugin is already included in your app and available to you at window.BackgroundFetch. Feel free to implement a callback as documented there. The plugin uses this plugin internally for its own purposes.

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

Good then, I guess that I need to remove cordova-plugin-geolocation but can I still get the current position when the user is on the foreground ?

However, for the cordova-plugin-background-fetch

It says that :

iOS Background Fetch is basically an API which wakes up your app about every 15 minutes

As of my understanding, it'll fetch each 15minutes, but I am willing to execute my function each 10 seconds. It's either my understanding is wrong or it's not possible to execute the function in background each 10 seconds when working with iOS

@christocracy
Copy link
Member

but can I still get the current position when the user is on the foreground ?

Yes, as I said above: "this plugin has its own getCurrentPosition method"

but I am willing to execute my function each 10 seconds. It's either my understanding is wrong

Your understanding is wrong. As I said above: "When an iOS app is in the background without locations-services on, your app is suspended. If you're hoping to do a setInterval with Javascript while your app is in the background and the device is stationary, that's not going to happen."

@christocracy
Copy link
Member

Read the plugin's Philosophy of Operation

@HamzaLJ
Copy link
Author

HamzaLJ commented Aug 24, 2017

Got it, basically. As of my understanding background fetch will happen only when the device is moving, getting a new user location and not in stationary.
Please correct me if I am wrong.

@christocracy
Copy link
Member

christocracy commented Aug 24, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants