flutter_background_geolocation icon indicating copy to clipboard operation
flutter_background_geolocation copied to clipboard

Getting redunt `onProviderChange` and `onConnectivityChange` callback event in headless

Open kunatastic opened this issue 2 years ago • 6 comments

Your Environment

  • Plugin version:
  • Platform: iOS or Android
  • OS version:
  • Device manufacturer / model:
  • Flutter info (flutter doctor):
  • Plugin config:
bg.BackgroundGeolocation.onProviderChange(onProviderChange);
bg.BackgroundGeolocation.onConnectivityChange(onConnectivityChange);

bg.Config(
...,
      distanceFilter: isAndroid ? 0 : 200,
      locationUpdateInterval: 600000,
      fastestLocationUpdateInterval: 60000,
...
)

  static void onProviderChange(bg.ProviderChangeEvent event) {
      print(">>> event: $event")
  }

  static void onConnectivityChange(bg.ConnectivityChangeEvent event) {
      print(">>> event: $event")
  }

Expected Behavior

I expect callback to be present when they are mean't to be.

Actual Behavior

I am getting callback redunduntly and with exact same values.

Context

I need to log and process some process on the basis of the callback.

kunatastic avatar Dec 27 '22 15:12 kunatastic

bg.BackgroundGeolocation.onProviderChange(onProviderChange); bg.BackgroundGeolocation.onConnectivityChange(onConnectivityChange);

You are not to use these in headless mode.

Your registered HeadlessTask in main.dart receives all headless events.

christocracy avatar Dec 27 '22 15:12 christocracy

I have also registered them seperately also

    bg.BackgroundGeolocation.registerHeadlessTask(
      backgroundGeolocationHeadlessTask,
    );

/// Receive events from BackgroundGeolocation in Headless state.
@pragma('vm:entry-point')
void backgroundGeolocationHeadlessTask(
  bg.HeadlessEvent headlessEvent,
) {
  switch (headlessEvent.name) {
    case bg.Event.LOCATION:
      LocationCallbackHandler.onLocation(headlessEvent.event);
      break;

    case bg.Event.CONNECTIVITYCHANGE:
      LocationCallbackHandler.onConnectivityChange(headlessEvent.event);
      break;

    case bg.Event.PROVIDERCHANGE:
      LocationCallbackHandler.onProviderChange(headlessEvent.event);
      break;

    case bg.Event.TERMINATE:
// something
      break;
  }
}

kunatastic avatar Dec 27 '22 15:12 kunatastic

  • bg.BackgroundGeolocation.registerHeadlessTask MUST be placed in main.dart
  • You do not use BackgroundGeolocation.onXXX in main.dart

christocracy avatar Dec 27 '22 15:12 christocracy

I suggest you have a look at the /example app in this repo.

christocracy avatar Dec 27 '22 15:12 christocracy

  • You do not use BackgroundGeolocation.onXXX in main.dart

Couldn't follow you here. Could you kindly elaborate a bit further.

kunatastic avatar Dec 27 '22 15:12 kunatastic

Look at the Example in the README.

Everything except .registerHeadlessTask belongs INSIDE a Flutter Widget.

christocracy avatar Dec 27 '22 15:12 christocracy

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar May 21 '24 01:05 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jun 04 '24 01:06 github-actions[bot]