capacitor-community / background-geolocation

A Capacitor plugin that sends you geolocation updates, even while the app is in the background.
MIT License
177 stars 54 forks source link

Fixed the Capacitor 4 after 5 minutes stop working #107

Closed Sanish777 closed 9 months ago

Sanish777 commented 9 months ago

The background tracking works for some minutes like (4- 5 minutes) and app stops tracking the location.

diachedelic commented 9 months ago

You need the legacy bridge: https://github.com/capacitor-community/background-geolocation#android.

t19d commented 9 months ago

I've tried with several changes and this code (for me) was the only one which fixed the 5 minutes stop working in Capacitor v5. I tried using "useLegacyBridge: true" in capacitor.config.ts but it doesn't fix my problem. Why not can this solution be used/recommended?

diachedelic commented 9 months ago

Here is the reason that I don't like this hack: https://github.com/capacitor-community/background-geolocation/issues/89#issuecomment-1455421492

I'm surprised that the legacy bridge is not working for you. Can you show me your generated capacitor.config.json that is copied to android/app/src/main/assets/public?

diachedelic commented 9 months ago

Sorry, I meant android/app/src/main/assets.

diachedelic commented 9 months ago

See https://github.com/ionic-team/capacitor/issues/6234#issuecomment-1735716334 for a discussion of the root problem.

Sanish777 commented 9 months ago

Have tried this solution not worked for me so, I have created the pull request. Maybe its vary due to capacitor or ionic version that are being used.

diachedelic commented 9 months ago

Could you please both tell me the versions of everything you are using? Android OS, Capacitor, plugin, etc?

t19d commented 9 months ago

Here is the reason that I don't like this hack: #89 (comment)

I'm surprised that the legacy bridge is not working for you. Can you show me your generated capacitor.config.json that is copied to android/app/src/main/assets/public?

{ "appId": "x.y.z", "appName": "X Y Z", "webDir": "www", "bundledWebRuntime": false, "plugins": { "LocalNotifications": { "iconColor": "#FF0000" } }, "android": { "useLegacyBridge": true }, "cordova": { "preferences": { "android:usesCleartextTraffic": "true", "android:requestLegacyExternalStorage": "true", "android.permission.WRITE_EXTERNAL_STORAGE": "true", "android.permission.READ_EXTERNAL_STORAGE": "true", "android.permission.SCHEDULE_EXACT_ALARM": "true", "android.permission.POST_NOTIFICATIONS": "true", "android.permission.ACCESS_COARSE_LOCATION": "true", "android.permission.ACCESS_FINE_LOCATION": "true", "android.permission.ACCESS_BACKGROUND_LOCATION": "true", "android.permission.FOREGROUND_SERVICE": "true" } } }

diachedelic commented 9 months ago

Could you please provide your Capacitor and Android OS versions?

t19d commented 9 months ago

In my package.json I have the following versions (I'm also using Angular version "16.2.6"):

      "@capacitor/android": "5.4.1",
      "@capacitor/app": "5.0.6",

The Android version where I am testing it is Android 13 (API 33) and Android 14 (API 34).

diachedelic commented 9 months ago

What models are your Androids? Do you get the problem on both Android 13 and Android 14?

t19d commented 9 months ago

I'm using Android Studio emulators (Pixel_3a)

diachedelic commented 9 months ago

Interesting. My guess is that this problem is specific to the emulator. If you discover that this is a problem on a real device we can investigate further.

t19d commented 9 months ago

I've been testing with two different physical Android devices and with different versions:

With both I have had the same unsatisfactory result. The same thing happens, after 5 minutes, the requests remain in a pending state and are not sent to the Back.

diachedelic commented 9 months ago

Could you please try running the example app (https://github.com/capacitor-community/background-geolocation/tree/master/example) on these devices, to see if you can reproduce the problem?

diachedelic commented 9 months ago

If requests are queued, the numbers at the start of each status line will fall out of sync - they are the time the location was generated and the time it was delivered to the JavaScript.

t19d commented 9 months ago

I have tested it under the same conditions (with emulator and physical device) and the same "error" occurs. After 5 minutes the request stops executing (gives an error or satisfactory response) and the request remains in the "pending" state (seen in the Network section of the devtools). On this occasion I have simply used the example code and a fetch to make the request.

alexandermccormick commented 9 months ago

I hate to be the outlier here, but I'm not getting an errors in my app or the example. Here is the process I took with the example:

I then left for a 15 minute break, and came back to events still firing while the app was still minimized. All of the times were still synced.

Just a reminder to make sure your are using the useLegacyBridge: true option under the android settings in your capacitor config, and that you are starting your "background" task prior to the app getting minimized. Think of the addWatcher method as your "main" function running in the background, that's your main loop.

@t19d OnePlus and Xiaomi are both very aggressive towards apps. In my experience, those phones don't care and will terminate most apps after 5 minutes unless they are actively playing audio. This is not something that can be resolved on our side of the phone, this is something the phone manufacturer enforces.

Google actually has a spot to report phone specific issues like this. It might not seem like it matters, but just showing further interest in solving this issue could go a long way.

As always, if anyone having issues would like to share an example repo with how you're trying to utilize this plugin (or even your project itself at this point), I would be more than happy to take a look. I know it sounds like more work, but if there is an actual issue with this plugin, only your code will show us.

*Background is quoted because this is technically a Foreground Service

diachedelic commented 9 months ago

After 5 minutes the request stops executing (gives an error or satisfactory response) and the request remains in the "pending" state (seen in the Network section of the devtools).

This makes me think your problem is that HTTP requests, triggered as a result of location updates, are being throttled after 5 minutes. Here is the solution: https://github.com/capacitor-community/background-geolocation/issues/14#issuecomment-1293574693.

Thanks for checking the example app, @alexandermccormick .

diachedelic commented 9 months ago

I have updated the documentation to mention this pitfall: https://github.com/capacitor-community/background-geolocation#android.

mohamadnagi commented 7 months ago

I added android.useLegacyBridge = true but still having the same issue.