React-native-background-geolocation: Stop send location to server in a fews hours

Created on 10 Oct 2017  Â·  3Comments  Â·  Source: transistorsoft/react-native-background-geolocation

Your Environment

  • Plugin version: 2.9.4
  • Platform: Android
  • OS version: 5.1.1
  • Device manufacturer / model: Redmi 3
  • React Native version (react-native -v): 0.46.4
  • Plugin config
global.watchLocation = function(){
  BackgroundGeolocation.configure({
    desiredAccuracy: 0,
    distanceFilter: 50,
    foregroundService: true,
    notificationPriority: BackgroundGeolocation.NOTIFICATION_PRIORITY_MAX,
    desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
    forceReloadOnBoot:true,
    forceReloadOnSchedule:true,
    stopOnTerminate: false,
    startOnBoot:true,
    deferTime:10,
    notificationText: "Location service at trip",
    locationUpdateInterval: 3000,
     distanceFilter: 0,     
  }, function(state) {
    console.log('- BackgroundGeolocation configured and ready', state);
      global.registerBackgroundJob();
    if (!state.enabled) { 
        BackgroundGeolocation.start();
    }
  }); 
}

global.registerBackgroundJob = function(){
  BackgroundJob.register({
    jobKey: 'watchLocation',
    job: () => global.sendPosition()
  });

  BackgroundJob.schedule({
    jobKey: 'watchLocation',
    period: 3000,
    exact: true,
    allowExecutionInForeground: true,
    timeout: 150000,
  });
}

global.sendPosition = function(){
  global.getItem(global.ACCESS_TOKEN, function(s,token){
    if(s){
      BackgroundGeolocation.getCurrentPosition((pos) => {
        console.log(token, pos);
        var param = {
           APIKEY: token,
           Lat: pos.coords.latitude,
           Lng: pos.coords.longitude,
           Speed: pos.coords.speed,
           Time: moment(new Date()).format('YYYY-MM-DD HH:mm:ss') 
        }
        var dataParam = param;
        param = global.convertDataURL(param);
        global.setTimeOut(fetch(global.base_url+'api/sendtripcoordinate', {
          method: 'POST',
          headers: global.requestHeader,
          body: param
        }))
        .then((response) => {
          return response.json();
        })
        .then((data) => {
          if(data.error){
            commonAlert(data.error ? data.error: "Please check your internet connection", "Warning");
          }
          else{
            global.getItem('tempCoordinate', (status,value) => {
              if(status && value){
                  let result = JSON.parse(value);
                  result.map(function(i){
                    var data = global.convertDataURL(i);
                     global.setTimeOut(fetch(global.base_url+'api/sendtripcoordinate', {
                          method: 'POST',
                          headers: global.requestHeader,
                          body: data
                     }))
                     .then((response) => {return response.json();})
                     .then((data)=>{
                        console.log("berhasil konek simpan data dari localstorage ke server",data);
                      })
                  });
                  global.removeItem('tempCoordinate', s => {});
              }
            })
          }
        })
        .catch((error) => {
          //commonAlert(error ? error: "Please check your internet connection", "Warning");
          console.log("fail to server coordinate, save to localstorage");
            var inputData = [];
            inputData.push(dataParam);
            global.getItem('tempCoordinate', (status,value) => {
              if(status && value){
                  let result = JSON.parse(value);
                  result.map(function(i){
                    inputData.push(i);
                  });
                  console.log(inputData);
              }
              global.setItem('tempCoordinate', JSON.stringify(inputData), s => {});
            })
        })
      },(err) => {
          console.log("Error dari geoLocation",err)
      }, global.geoLocationOption);

    }
  });
}

Expected Behavior

  1. Keep send location for a long time, until user press button finish trip in apps
  2. If lost connection in mobile phone user, then location saved to local storage. and if user connect internet, data location from local storage will sent to server.

Actual Behavior

  1. stop get location when using more 3-4 hours

Debug logs

Most helpful comment

Add you aware the plugin has its own database and http service? The plugin persists every location to its own SQLite database until its http service receives a 20x response from your server or you manually #destroyLocations.

You only need to provide the plugin an optional #url, #params, #headers.

The plugin was designed to track first-responders in disaster zones where network is assumed to be destroyed. The plugin is designed for long-term tracking as if life depended on it. When the plugin senses a valid http connection, it can automatically upload its stored locations from its database to your configured server.

You do not need to implement your own client persistence mechanism or http service.

If you are experienced unusual behavior, the plugin logs everything it does to its log database. You have not provided any logs, so there’s little to speculate about until you do. See the “Debugging” section of WIKI.

All 3 comments

Add you aware the plugin has its own database and http service? The plugin persists every location to its own SQLite database until its http service receives a 20x response from your server or you manually #destroyLocations.

You only need to provide the plugin an optional #url, #params, #headers.

The plugin was designed to track first-responders in disaster zones where network is assumed to be destroyed. The plugin is designed for long-term tracking as if life depended on it. When the plugin senses a valid http connection, it can automatically upload its stored locations from its database to your configured server.

You do not need to implement your own client persistence mechanism or http service.

If you are experienced unusual behavior, the plugin logs everything it does to its log database. You have not provided any logs, so there’s little to speculate about until you do. See the “Debugging” section of WIKI.

i got it.
it's done.

how to set if is_moving = false, data not sent to the server ?

how to set if is_moving = false

I don't understand your question.

When is_moving: false, the plugin stops recording locations. If the device isn't moving, the plugin turns OFF location-services and stops recording locations.

Read the Philosophy of Operation

Was this page helpful?
0 / 5 - 0 ratings