transistorsoft / capacitor-background-geolocation

The most sophisticated background location-tracking & geofencing module with battery-conscious motion-detection intelligence for iOS and Android.
MIT License
96 stars 16 forks source link

Plugin works even on background, but it dont execute my code when in background on Android #13

Closed barart closed 6 months ago

barart commented 3 years ago

Your Environment

Latest Dependencies:

@capacitor/cli: 3.1.2 @capacitor/core: 3.1.2 @capacitor/android: 3.1.2 @capacitor/ios: 3.1.2

Installed Dependencies:

@capacitor/cli: 3.1.2 @capacitor/android: 3.1.2 @capacitor/core: 3.1.2 @capacitor/ios: 3.1.2

[success] iOS looking great! đź‘Ś [success] Android looking great! đź‘Ś

Context

Im trying to execute a code when onMotionChange and onLocationChange

Debug logs

Logs ``` I dont think logs are relevant in this case as the plugin works but it dont execute my code on background, there is no different on foreground and background logs. I could not find [onLocation] or [onMotionChange] strings on logs (foreground or background) ```
christocracy commented 3 years ago

If you're trying to do an http request in the plugin's event-handlers, you're likely experiencing the Capacitor webview failing to execute code in the background.

I suggest you take advantage of the plugin's built-in native HTTP service. See the API docs HTTP Guide for more information.

barart commented 3 years ago

Hello Chris... im not trying to make a http request, im trying to call an api as you can se on my code:

BackgroundGeolocation.onMotionChange((location) => {
      console.log('[onMotionChange]', location);
      console.log('Update Coords');
        const param = {
          lat: location.location.coords.latitude,
          lng: location.location.coords.longitude,
       }
        this.api.updateCoords(param).then((data) => {
           console.log(data);
        }).catch(error => {
          console.log(error);
        });

    });

as i wrote above, this works on ios but not on android

christocracy commented 3 years ago

im not trying to make a http request, im trying to call an api as you can se on my code:

Yes, it looks like you're trying to manage sending location data to your API, likely via HTTP request, via Javascript code.

Are you observing the plugin logs? See Wiki Debugging.

barart commented 3 years ago

Yes, as i mentioned when opening the thread, i already observed the plugin logs, there is nothing relevant or errors compared to the log with the app in foreground and the log in background, seems very similar both of them

christocracy commented 3 years ago

I suggest you print log messages in your event callbacks using BackgroundGeolocation.logger. Those will print messages to the plugin's logs, which can be captured with getLog / emailLog. That will tell you if the event-handlers are actually running.

barart commented 3 years ago

Im still debugging this, the BackgroundGeolocation.logger has been helpful, however, everything seems to work (without any code changes) as same and logger show that it "execute" my code (but it doesn't do what it should); what is strange for me, is that my code seems to start working completely when i added the BackgroundGeolocation.logger into it :/

Im still investiganting...

iOS works exactly as expected, pure beauty

mitar096 commented 3 years ago

Hello,

I have the exact same problem. @barart did you find a solution for this?

Thank you in advance

ramandeepR commented 2 years ago

Hello,

I have the exact same problem. @barart did you find a solution for this? @christocracy

Thank you in advance

mitar096 commented 2 years ago

Hello @ramandeepR,

I found a solution for this problem. Android will run the code only if it was written in Java. You must write this code in Java if you wont to do something in background. More about this you can find here: https://github.com/transistorsoft/cordova-background-geolocation-lt/wiki/Android-Headless-Mode#step-2a--plugin-version-2132

Best regards, Mitar

ramandeepR commented 2 years ago

Hello @mitar096

Thank you for reply. Can you help me with this integration? I am stuck on this issue now. May be I need to stop this only due to background location not working accurately.

Best Regards, Ramandeep

tanuj-g commented 1 year ago

Hello @christocracy I am encountering a similar problem where I need to transmit certain events (onMotionChange, onProviderChange) to my backend server. While the app is active in the background or foreground, the listeners successfully send these events. However, when the app is forcibly closed, I require immediate reception of these events on my server.

I am using plugin inbuild HTTP service to send the locations in the background and killed state. But that service only sends locations events for the IOS. they are not sending providerChange event in the background for ios

christocracy commented 1 year ago

Platform: Android

“they are not sending providerChange event in the background for ios”

This issue thread was posted related to Android.

tanuj-g commented 1 year ago

@christocracy Is it necessary to handle the events in the killed state by writing native code for both Android and iOS platforms by myself?

Is there any potential for the plugin to send all providerChange events to my specified config URL via HTTP, similar to how it sends location updates when the app is in a killed state?

Could you please incorporate this configuration into the plugin so that it can send the events using a single config? Since the native code is already implemented in the plugin, this addition would be beneficial.

christocracy commented 1 year ago

Is it necessary to handle the events in the killed state by writing native code for both Android and iOS platforms by myself?

Only Android. iOS has no concept of “headless”.

Is there any potential for the plugin to send all providerChange events to my specified config URL via HTTP

Android already does. For iOS, you must do so manually. iOS won’t fire the onProviderChange event if your app is terminated.

tanuj-g commented 1 year ago

@christocracy Could you please provide further clarification on what you mean by "manually handling the event" in the context of iOS?

My objective is to ensure that every onProviderChange event from the iOS app is sent to my server, regardless of the app's status (foreground, background, or killed state). This way, I can keep track of whether the user has granted or revoked location permissions.

For instance, if the user initially grants permission and tracking begins, but later manually revokes the location permission from the app through device settings, I would like to update my server accordingly to indicate that the user has selected incorrect permissions.

christocracy commented 1 year ago

My objective is to ensure that every onProviderChange event from the iOS app is sent to my server, regardless of the app's status (foreground, background, or killed state)

On iOS, you can’t.

github-actions[bot] commented 6 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 6 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.