mauron85 / react-native-background-geolocation

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

How to enabled Headless task #521

Open aliabbasmalik8 opened 3 years ago

aliabbasmalik8 commented 3 years ago

Here is my configuration and I want to get the location when the app is terminated but not succeeded. any help will be great for me.

BackgroundGeolocation.configure({ desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY, stationaryRadius: 0, distanceFilter: 0, notificationTitle: 'Background tracking', notificationText: 'enabled', debug: false, startOnBoot: true, stopOnTerminate: false, locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER, interval: 5000, fastestInterval: 5000, activitiesInterval: 10000, stopOnStillActivity: false, });

BackgroundGeolocation.on('location', (location) => {
  console.log(location)
  // handle your locations here
  // to perform long running operation on iOS
  // you need to create background task
  BackgroundGeolocation.startTask(taskKey => {
    // execute long running task
    // eg. ajax post location
    // IMPORTANT: task has to be ended by endTask
    BackgroundGeolocation.endTask(taskKey);
  });
});

BackgroundGeolocation.headlessTask(async (event) => {
  console.log(event.name)
      var xhr = new XMLHttpRequest();
      xhr.open('POST', 'https://cf3673cf8ecb.ngrok.io/api/v1/location_tracks');
      xhr.setRequestHeader('Content-Type', 'application/json');
      xhr.send(JSON.stringify(event.params));
});

BackgroundGeolocation.on('stationary', (stationaryLocation) => {
  console.log('stationaryLocation', stationaryLocation)
  // handle stationary locations here
  Actions.sendLocation(stationaryLocation);
});

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

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

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

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');
});

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

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);

  // you don't need to check status before start (this is just the example)
  if (!status.isRunning) {
    BackgroundGeolocation.start(); //triggers start on start event
  }
});