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

Failure in callback after 5 minutes only Android 14 (API level 34) #114

Closed carvalhop88 closed 3 months ago

carvalhop88 commented 3 months ago

Describe the bug Firstly, all settings in capacitor.config.json are correct following community recommendations: "plugins": { "CapacitorHttp": { "enabled": true }, "android": { "useLegacyBridge": true } }

I am using the libraries:

"@capacitor-community/background-geolocation": "^1.2.17", "@capacitor/android": "5.2.2", "@capacitor/core": "^5.7.2", "@capacitor/geolocation": "^5.0.7", "@capacitor/local-notifications": "^5.0.7", Among others...

I conducted several tests with different versions of Android, and only the most current version, which is version 14 API 34 at the moment, exhibits this behavior. Sending data to a database isn't an issue; however, I've noticed that after five minutes, the callback stops working. It seems that the callback remains cached; thus, when I unlock the device and open the app, all the returns are sent at once.

I did some research on the Android website, and found that there were some changes related to background services for Android 14, but I was unable to identify what problem was occurring.

The problem occurs both on virtual devices in Android Studio and on the physical cell phone.

On other Android versions, such as 8.1, 10 and 13, the app works all day long.

In logcat: image

image

carvalhop88 commented 3 months ago

code:

`BackgroundGeolocation.addWatcher(
    {
      backgroundMessage:
        "Dont close app.",
      requestPermissions: true,
      stale: false,
      distanceFilter: 0,
    },
    async (location) => {                      <<<------------HERE IS PROBLEM

      console.log("I am live")
      console.log(new Date())
      this.coords.push(location?.longitude, location?.latitude)
      await this.sendLocation(location)
    }
  );`
diachedelic commented 3 months ago

Can you reproduce the problem using the example app? https://github.com/capacitor-community/background-geolocation/tree/master/example

diachedelic commented 3 months ago

I upgraded my Samsung A52 to Android 14, and I have not been able to reproduce the problem with the example app. To use it, add a BG watcher and pay attention to the first two numbers in each line. The first is the time the location was generated, and second is the time the location was delivered to the callback. The two timestamps should be the same, meaning that there is no delay in delivering the location.

carvalhop88 commented 3 months ago

I'm doing some testing right now, I haven't tested it with the example code yet. But I made some changes to my code that seem to have resolved it. The first change was in the capacitor configuration file, I was looking at the example code configuration file and noticed that android.useLegacyBridge is not a 'plugins' key and is actually in the main object. The second change was in the addWatcher callback, I removed async and included a 'return' in my sendLocation method.

carvalhop88 commented 3 months ago

capacitor.config.json looked like this:

{ "webDir": "dist/spa", "appId": "com.mycompany.test", "appName": "test", "plugins": { "CapacitorHttp": { "enabled": true }, "Badge": { "persist": true, "autoClear": false }, "SplashScreen": { "launchShowDuration": 2000, "launchFadeOutDuration": 1000, "backgroundColor": "#000000", "showSpinner": false, "splashFullScreen": true, "splashImmersive": true }, "PushNotifications": { "presentationOptions": ["badge", "sound", "alert"] } }, "android": { "useLegacyBridge": true } }

carvalhop88 commented 3 months ago

As I made these changes together, I'm not sure if it was because I included android.useLegacyBridge in the right place or because I made my sendLocation method synchronous.

I'm still doing some testing, but I must thank you for your help. Thanks!

carvalhop88 commented 3 months ago

The example code works fine on Android 14:

image

diachedelic commented 3 months ago

Ah yes, because the useLegacyBridge probably only takes effect in newer versions of Android.

It doesn't look like you ran the example code for very long (the L and W values are seconds since startup), but since your problem is resolved I guess it doesn't matter.