Skip to content

events such as onGeofence, onLocation, and onMotionChange not getting triggered sometimes in alive state and never triggers in killed state in both Android and iOS.  #1088

@poorvasha-pss

Description

@poorvasha-pss

Your Environment

  • Plugin version: 4.11.1

  • Platform: iOS and Android

  • OS version: Androi(12) adn iOS(16.4)

  • Device manufacturer / model: Android (Redmi Note 10 (13.0.4)) and iOS (iPhone XR)

  • Flutter info (flutter doctor): image

  • Plugin config:

 // 1.  Listen to events (See docs for all 13 available events).
    bg.BackgroundGeolocation.onMotionChange(_onMotionChange);
    bg.BackgroundGeolocation.onActivityChange(_onActivityChange);
    bg.BackgroundGeolocation.onConnectivityChange(_onConnectivityChange);
    bg.BackgroundGeolocation.onGeofence(_onGeofence);

    bg.TransistorAuthorizationToken token =
        await bg.TransistorAuthorizationToken.findOrCreate(
            orgname, username, 'https://tracker.transistorsoft.com');

    // 2.  Configure the plugin
    bg.BackgroundGeolocation.ready(bg.Config(
            //schedule: ,
            reset:
                false, // <-- lets the Settings screen drive the config rather than re-applying each boot.
            // Convenience option to automatically configure the SDK to post to Transistor Demo server.
            transistorAuthorizationToken: token,
            // Logging & Debug
            debug: false,
            logLevel: bg.Config.LOG_LEVEL_VERBOSE,
            // Geolocation options
            desiredAccuracy: bg.Config.DESIRED_ACCURACY_NAVIGATION,
            distanceFilter: 10.0,
            // Activity recognition options
            stopTimeout: 5,
            backgroundPermissionRationale: bg.PermissionRationale(
                title:
                    "Allow {applicationName} to access this device's location even when the app is closed or not in use.",
                message:
                    "This app collects location data to enable recording your trips to work and calculate distance-travelled.",
                positiveAction: 'Change to "{backgroundPermissionOptionLabel}"',
                negativeAction: 'Cancel'),
            // HTTP & Persistence
            autoSync: true,
            // Application options
            stopOnTerminate: false,
            startOnBoot: true,
            enableHeadless: true,
            heartbeatInterval: 60))
        .then((bg.State state) async {
      print('[ready] ${state.toMap()}');
      print('[didDeviceReboot] ${state.didDeviceReboot}');
      _enabled = state.enabled;
      _isMoving = state.isMoving ??= false;
      bg.BackgroundGeolocation.start();
      bg.BackgroundGeolocation.changePace(_isMoving);
      bg.BackgroundGeolocation.startGeofences();
      if (state.schedule != null && state.schedule!.isNotEmpty) {
        bg.BackgroundGeolocation.startSchedule();
      }
      CommonFunction.giveSuccessToast("Start Geofence Tracking");
    }).catchError((error) {
      print('[ready] ERROR: $error');
    });

Expected Behavior

The _onGeofence event should trigger whenever user enter or exit the geofence radius in both app alive and killed state

Actual Behavior

  1. If user enter or exit the geofence radius, sometimes the _onGeofence event is triggering but sometimes it is not, the way we have tested is through walking
  2. The events are never triggered when the app is killed,

we have added the backgroundGeolocationHeadlessTask and registered that in main.dart and also configured _configureBackgroundFetch()

void _configureBackgroundFetch() async {
    BackgroundFetch.configure(
        BackgroundFetchConfig(
            minimumFetchInterval: 15,
            startOnBoot: true,
            stopOnTerminate: false,
            enableHeadless: true,
            requiresStorageNotLow: false,
            requiresBatteryNotLow: false,
            requiresCharging: false,
            requiresDeviceIdle: false,
            requiredNetworkType: NetworkType.NONE), (String taskId) async {
      print("[BackgroundFetch] received event $taskId");
      if (taskId == 'flutter_background_fetch') {
        try {
          // Fetch current position
          var location = await bg.BackgroundGeolocation.getCurrentPosition(
              samples: 1,
              extras: {"event": "background-fetch", "headless": false});
          print("[location] $location");
        } catch (error) {
          print("[location] ERROR: $error");
        }
      }

      // Test scheduling a custom-task in fetch event.
      BackgroundFetch.scheduleTask(TaskConfig(
          taskId: "com.transistorsoft.customtask",
          delay: 5000,
          periodic: false,
          forceAlarmManager: true,
          stopOnTerminate: false,
          enableHeadless: true));
      BackgroundFetch.finish(taskId);
    });
    CommonFunction.giveSuccessToast("Background fetch configured");
  }

Steps to Reproduce

  1. Add desired geofences
  2. Start Walking
  3. walk by entering and exiting from the given geofence radius
  4. The event should trigger both in live and killed state of the app

Context

We are trying to auto logging in user based on the given geofence radius, if user enter the given geofence radius we will be sending local notification that "you have entered the location" and we will do an api call to add timelog in Database, if user exit, as same as enter.

Debug logs

Logs
PASTE_YOUR_LOGS_HERE

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions