darron1217 / react-native-background-geolocation

Background and foreground geolocation plugin for React Native. Tracks user when app is running in background.
Apache License 2.0
33 stars 38 forks source link

Cannot use postTemplate to send information to provided url #9

Open dakotatyrrell opened 3 years ago

dakotatyrrell commented 3 years ago

Your Environment

BackgroundGeolocation.configure({
  desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
  stationaryRadius: 50,
  distanceFilter: 500,
  notificationsEnabled: false,
  debug: false,
  startOnBoot: true,
  stopOnTerminate: false,
  locationProvider: BackgroundGeolocation.DISTANCE_FILTER_PROVIDER,
  interval: 10000,
  fastestInterval: 5000,
  activitiesInterval: 10000,
  stopOnStillActivity: false,
  maxLocations: 10,
  syncThreshold: 5,
  url: 'https://example.com', // Actual website here in full project
  httpHeaders: {
    'Accept': 'application/json',
    'Content-Type': 'multipart/form-data'
  },
  // customize post properties
  postTemplate: {
    Email: 'fakeemail@example.com',
    UserID: 'fakeID',
    PushKey: 'fakePushKey',
    latitude: '@latitude',
    longitude: '@longitude',
    i_Meta_Data: 1
  }
});

BackgroundGeolocation.on('location', (location) => {
  BackgroundGeolocation.startTask(taskKey => {
    console.log('ON location: ', location);
    BackgroundGeolocation.endTask(taskKey);
  });
});

BackgroundGeolocation.on('stationary', (stationaryLocation) => {
  // handle stationary locations here
  console.log('[INFO] BackgroundGeolocation service is stationary');
});

BackgroundGeolocation.on('error', (error) => {
  console.log('[ERROR] BackgroundGeolocation error:', error);
});

BackgroundGeolocation.on('start', () => {
  console.log('[INFO] BackgroundGeolocation service has been started');
  BackgroundGeolocation.startTask(taskKey => {
    console.log('ON location: ', location);
    BackgroundGeolocation.endTask(taskKey);
  });
});

BackgroundGeolocation.on('stop', () => {
  console.log('[INFO] BackgroundGeolocation service has been stopped');
  // BackgroundGeolocation.start();
});

BackgroundGeolocation.on('authorization', (status) => {
  console.log('[INFO] BackgroundGeolocation authorization status: ' + status);
  if (status !== BackgroundGeolocation.AUTHORIZED) {
    // we need to set delay or otherwise alert may not be shown
    setTimeout(() =>
      Alert.alert('App requires location tracking permission', 'Would you like to open app settings?', [
        { text: 'Yes', onPress: () => BackgroundGeolocation.showAppSettings() },
        { text: 'No', onPress: () => console.log('No Pressed'), style: 'cancel' }
      ]), 1000);
  }
});

BackgroundGeolocation.on('background', () => {
  console.log('[INFO] App is in background');
  this.setState({
    isBackground: true,
  });
});

BackgroundGeolocation.on('foreground', () => {
  console.log('[INFO] App is in foreground');
  this.setState({
    isBackground: false,
  });
});

BackgroundGeolocation.on('abort_requested', () => {
  console.log('[INFO] Server responded with 285 Updates Not Required');
});

BackgroundGeolocation.on('http_authorization', () => {
  console.log('[INFO] App needs to authorize the http requests');
});

BackgroundGeolocation.checkStatus(status => {
  console.log('[INFO] BackgroundGeolocation service is running', status.isRunning);
  console.log('[INFO] BackgroundGeolocation services enabled', status.locationServicesEnabled);
  console.log('[INFO] BackgroundGeolocation auth status: ' + status.authorization);
});

Expected Behavior

The postTemplate should be sent to the url provided in the configuration of BackgroundGeolocation.

Actual Behavior

There is no indication that the postTemplate information is being sent to the provided url on the app's side. The back end does not claim to have received any information from the app.

Possible Fix

Steps to Reproduce

  1. Define a BackgroundGeolocation configuration as shown above.
  2. Run the task based on location updates using BackgroundGeolocation.on('location')
  3. Check for an indication that information was sent on both the front end and the back end.

Context

Trying to send location information to a back-end, where it is then sent into a database. While it is possible to send information through BackgroundGeolocation.on('location'), this does not allow for the sending of location information when the app is terminated.

Debug logs

darron1217 commented 3 years ago

This plugin seems having an issue with iOS. Soon I'll investigate into it.