react-native-background-geolocation: Module AppRegistry is not a registered callable module (calling startHeadlessTask)

Your Environment

  • Plugin version: 3.2.2
  • Platform: Android
  • OS version: linux
  • Device manufacturer / model: try different android devices
  • React Native version (react-native -v): 0.59.8
  • Plugin config
BackgroundGeolocation.ready({
  reset: true, // <-- true to always apply the supplied config
  // Geolocation Config
  desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
  distanceFilter: 0,
  // Activity Recognition
  stopTimeout: 1,
  // useSignificantChangesOnly: true,
  // Application config
  debug: false, // <-- enable this hear sounds for background-geolocation life-cycle.
  logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
  enableHeadless: true,
  stopOnTerminate: false,   // <-- Allow the background-service to continue tracking when user closes the app.
  startOnBoot: true,        // <-- Auto start tracking when device is powered-up.
}, (state) => {
  console.log("- BackgroundGeolocation is configured and ready!!", state.enabled);

  if (!state.enabled) {
    ////
    // 3. Start tracking!
    //
    BackgroundGeolocation.start(function() {
      console.log("BackgroundGeolocation started successfully");
    });
  }
});

////
// Define your Headless task -- simply a javascript async function to receive 
// events from BackgroundGeolocation:
//
let HeadlessTask = async (event: any) => {
  let params = event.params;
  console.log('[BackgroundGeolocation HeadlessTask] -', event.name, params);
  
  switch (event.name) {
    case 'location':
      // Use await for async tasks
      await sendLocationToServer(params)
      console.log('[BackgroundGeolocation HeadlessTask] - getCurrentPosition:', params);
      break;
  }
}

// Register your HeadlessTask with BackgroundGeolocation plugin - for android on killed mode
BackgroundGeolocation.registerHeadlessTask(HeadlessTask);

// This handler fires whenever bgGeo receives a location update - for foreground in android and ios + ios in killed mode
BackgroundGeolocation.onLocation(sendLocationToServer, errorCode => requestLogMessage(`onLoction failed with errorCode: ${errorCode}`));

Expected Behavior

  1. Locations should be send to my server event if the app is in killed mode.
  2. I should see in the JS logs the logs from the headless task

Actual Behavior

  1. Locations not sent
  2. it seems the the headless task is not invoked

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 23 (9 by maintainers)

Most upvoted comments

react-native. You cannot register headless task within your application components.

Application components do not exist in the headless state. They are not instantiated.