capacitor-background-geolocation: Cannot build App after applying the Android setup. Http POST requests won't work

Your Environment

  • Plugin version: @transistorsoft/capacitor-background-geolocation": “^4.3.0”
  • Platform: Android
  • Device OS version: Android 10
  • Device manufacturer / model: Samsunsg / Galaxy A71
  • XCode version: -
  • Capacitor info (` 💊 Capacitor Doctor 💊

Latest Dependencies:

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

Installed Dependencies:

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

[success] iOS looking great! 👌`)

  • Plugin config provided to #ready:
//plugin config
  BackgroundGeolocation.ready({
    reset: false,
    debug: false,
    logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
    disableLocationAuthorizationAlert: true,
    distanceFilter: 10,
    notification: {
      priority: BackgroundGeolocation.NOTIFICATION_PRIORITY_HIGH
    },
    autoSync: true,
    stopOnTerminate: false,
    startOnBoot: true
  }, (state) => {
    if (!state.enabled) {
      BackgroundGeolocation.startGeofences();
      LocalNotifications.requestPermissions().then((permission) => { permission });
    } else {
      console.log("Im having a problem here maybe not ready");
    }
  });

//the post request im trying to execute
 this.httpNative.sendRequest(
       "https://enterurlhere"
      {
        method: 'post',
        data:  {
           //some data
        },
        headers: {
          "Content-Type": "application/json",
          "Accept": "application/json",
          "Access-Control-Allow-Origin": "*",
        },
        responseType: "json",
      }).then(data => {
        data.status = 0;//success
        console.log("success ");
        return data;
      }).catch(error => {
        if (error.status == 401) {
          error.status = -1;
          console.log("validate session")
        } else {
          error.status = 1;//error
          console.log("error");
        }
        return error;
      });

Expected Behavior

The app should be able to communicate with the API and handle the responses in order for the user to login, logout, change password,… in our app

Actual Behavior

The app is unable to execute any POST requests and stucks after applying the Android setup. After the build the app is able to open and execute GET requests. Whenever i try to execute a POST request the app returns the following error in my Console:

ERROR Error: Uncaught (in promise): SyntaxError: Unexpected token U in JSON at position 0

Steps to Reproduce

  1. Install capacitor-background-geolocation plugin
  2. Install HTTP native plugin https://ionicframework.com/docs/native/http
  3. Implement a simple http post request as shown above.
  4. Follow the Android setup
  5. Build the app
  6. Try to run a post request
  7. Check the console tab for errors

Context

Debug logs

Logs
ERROR Error: Uncaught (in promise): SyntaxError: Unexpected token U in JSON at position 0
SyntaxError: Unexpected token U in JSON at position 0
    at JSON.parse (<anonymous>)
    at GeneralmethodsService.<anonymous> (/main-es2015.js:101)
    at Generator.next (<anonymous>)
    at /vendor-es2015.js:66673
    at new ZoneAwarePromise (/polyfills-es2015.js:1075)
    at __awaiter (/vendor-es2015.js:66669)
    at /main-es2015.js:89
    at ZoneDelegate.invoke (/polyfills-es2015.js:479)
    at Object.onInvoke (/vendor-es2015.js:57245)
    at ZoneDelegate.invoke (/polyfills-es2015.js:478)
    at resolvePromise (/polyfills-es2015.js:913)
    at /polyfills-es2015.js:820
    at rejected (/vendor-es2015.js:66671)
    at ZoneDelegate.invoke (/polyfills-es2015.js:479)
    at Object.onInvoke (/vendor-es2015.js:57245)
    at ZoneDelegate.invoke (/polyfills-es2015.js:478)
    at Zone.run (/polyfills-es2015.js:238)
    at /polyfills-es2015.js:972
    at ZoneDelegate.invokeTask (/polyfills-es2015.js:514)
    at Object.onInvokeTask (/vendor-es2015.js:57233)
defaultErrorLogger @ /vendor-es2015.js:34142 

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 34 (16 by maintainers)

Most upvoted comments

If you are in need to do http requests in the background you will need a native http plugin. Currently only the Capacitor http plugin is able to do that after 5 minutes in background since Android 10. But the UI thread is paused, so no JS code is executed after 5 minutes in background. You will need to do background tasks with background fetch then to execute pre-defined time limited tasks.