transistorsoft / flutter_background_geolocation

Sophisticated, battery-conscious background-geolocation & geofencing with motion-detection
https://www.transistorsoft.com/shop/products/flutter-background-geolocation
Other
648 stars 239 forks source link

IOS not recording location at background #340

Closed shikha1992 closed 4 years ago

shikha1992 commented 4 years ago

Your Environment

  bg.BackgroundGeolocation.ready(bg.Config(
      desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH,
      distanceFilter: 10.0,
      stopOnTerminate: false,
      startOnBoot: true,
     allowIdenticalLocations: true,
      notificationTitle: "Greenplay",
      notificationText: "Fetching location",
      debug: false,
      enableHeadless: true,
    foregroundService: true,
//      locationUpdateInterval: 300,
      forceReloadOnMotionChange: true ,
      logLevel: bg.Config.LOG_LEVEL_VERBOSE,
      fastestLocationUpdateInterval: 1,
      isMoving: true,
  )).then((bg.State state) {
    if (!state.enabled) {
      if (PrefsSingleton.prefs.getString(PreferenceNames.token)  != null) {
        bg.BackgroundGeolocation.start();
        bg.BackgroundGeolocation.changePace(true);
      }else{
//        bg.BackgroundGeolocation.stop();
      }
    }
  });

//register
bg.BackgroundGeolocation.registerHeadlessTask(
        backgroundGeolocationHeadlessTask);
/// Receive events from BackgroundGeolocation in Headless state.
void backgroundGeolocationHeadlessTask(bg.HeadlessEvent headlessEvent) async {
  print('πŸ“¬ --> $headlessEvent');
  var vv = headlessEvent.name;
  print('πŸ“¬ --> $vv');
  PrefsSingleton.prefs = await SharedPreferences.getInstance();

  switch(headlessEvent.name) {
    case bg.Event.TERMINATE:
      break;
    case bg.Event.HEARTBEAT:
      break;
    case bg.Event.LOCATION:
      bg.Location location = headlessEvent.event;
      print(location);
      String session = PrefsSingleton.prefs.getString(PreferenceNames.isSession);
      if(session == null){
        session = "Automatic";
      }

      if (PrefsSingleton.prefs.getString(PreferenceNames.token)  != null && session == "Automatic") {
        if(location.activity.type.toLowerCase() != 'still'){
          _updateDataBase(location, PrefsSingleton.prefs);
        }
      }

      var connectivityResult = await (Connectivity().checkConnectivity());
      if (connectivityResult == ConnectivityResult.mobile) {
        // I am connected to a mobile network.
      } else if (connectivityResult == ConnectivityResult.wifi) {
        // I am connected to a wifi network.
      }
      accelerometerEvents.listen((AccelerometerEvent event) {

      });

      break;
    case bg.Event.MOTIONCHANGE:
      bg.Location location = headlessEvent.event;
      print(location);
      print('aaaa111');
      break;
    case bg.Event.GEOFENCE:
      bg.GeofenceEvent geofenceEvent = headlessEvent.event;
      print(geofenceEvent);
      break;
    case bg.Event.GEOFENCESCHANGE:
      bg.GeofencesChangeEvent event = headlessEvent.event;
      print(event);
      break;
    case bg.Event.SCHEDULE:
      bg.State state = headlessEvent.event;
      print(state);
      break;
    case bg.Event.ACTIVITYCHANGE:
      bg.ActivityChangeEvent event = headlessEvent.event;
      print(event);
      bg.ActivityChangeEvent location = headlessEvent.event;
      print("act_change: $location");
      break;
    case bg.Event.HTTP:
      bg.HttpEvent response = headlessEvent.event;
      print(response);
      break;
    case bg.Event.POWERSAVECHANGE:
      bool enabled = headlessEvent.event;
      print(enabled);
      break;
    case bg.Event.CONNECTIVITYCHANGE:
      bg.ConnectivityChangeEvent event = headlessEvent.event;
      print(event);
      break;
    case bg.Event.ENABLEDCHANGE:
      bool enabled = headlessEvent.event;
      print(enabled);
      break;
    case bg.Event.AUTHORIZATION:
      bg.AuthorizationEvent event = headlessEvent.event;
      print(event);
      bg.BackgroundGeolocation.setConfig(bg.Config(
          url: "${ENV.TRACKER_HOST}/api/locations"
      ));
      break;
  }
}

info.plist

<dict>
    <key>BACKGROUND_GEOLOCATION_ENCRYPTION_PASSWORD</key>
    <string>transistorsoft</string>
    <key>BGTaskSchedulerPermittedIdentifiers</key>
    <array>
        <string>com.transistorsoft.fetch</string>
    </array>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>GreenPlay</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>$(MARKETING_VERSION)</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>com.googleusercontent.apps.336786091897-aubrenep3tmcskfdjbglhnebnjg9g09p</string>
            </array>
        </dict>
    </array>
    <key>CFBundleVersion</key>
    <string>$(CURRENT_PROJECT_VERSION)</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    <key>NSAppleMusicUsageDescription</key>
    <string>Greenplay would like to access your music</string>
    <key>NSCalendarsUsageDescription</key>
    <string>Greenplay would like to access your calender</string>
    <key>NSContactsUsageDescription</key>
    <string>Greenplay would like to access your device contacts</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Greenplay wants to know your location to know how much distance you have covered for    challenge</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Greenplay wants to know your location to know how much distance you have covered for challenge</string>
    <key>NSMotionUsageDescription</key>
    <string>Greenplay wants to detect your motion to calculate your session</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>Greenplay would like to access your gallery</string>
    <key>NSCameraUsageDescription</key>
    <string>Greenplay would like to access your camera</string>
    <key>NSMicrophoneUsageDescription</key>
    <string>Greenplay would like to access your microphone</string>
    <key>NSSpeechRecognitionUsageDescription</key>
    <string>Greenplay would like to access your device speech recognition</string>
    <key>UIBackgroundModes</key>
    <array>
        <string>fetch</string>
        <string>location</string>
        <string>processing</string>
    </array>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
</dict>

Expected Behavior

Actual Behavior

Steps to Reproduce

1. 2. 3. 4.

Context

Debug logs

Logs ``` PASTE_YOUR_LOGS_HERE ```
christocracy commented 4 years ago

You need to await these calls. All plugin methods run asynchronously. There's no way to predict which of these methods executes first.

bg.BackgroundGeolocation.start();
bg.BackgroundGeolocation.changePace(true);

bg.BackgroundGeolocation.registerHeadlessTask( backgroundGeolocationHeadlessTask);

Headless-task is an Android-only mechanism. It has nothing to do with iOS.

I suggest you test your app in the simulator with debug: true, simulating location with Freeway Drive.

shikha1992 commented 4 years ago

You need to await these calls. All plugin methods run asynchronously. There's no way to predict which of these methods executes first.

bg.BackgroundGeolocation.start();
bg.BackgroundGeolocation.changePace(true);

bg.BackgroundGeolocation.registerHeadlessTask( backgroundGeolocationHeadlessTask);

Headless-task is an Android-only mechanism. It has nothing to do with iOS.

I suggest you test your app in the simulator with debug: true, simulating location with Freeway Drive.

So I am calling my update function inside headless task only. If it does not works for ios, then where should I call my update for ios? right now I am using this function to get latitude and update at my server from headless method case bg.Event.LOCATION:

christocracy commented 4 years ago

When iOS detects the device is moving and your app is currently terminated, the OS will automatically relaunch your app in the background, just as if launched from the home-screen. All traditional plugin events will fire (eg: onLocation, onMotionChange, etc.

christocracy commented 4 years ago

There's a reason this Wiki is called Android Headless Mode -- because Headless mode is Android only.

shikha1992 commented 4 years ago

I am not using below method, for ios it means I have to use this?

bg.BackgroundGeolocation.onLocation((bg.Location location) { print('[location] - $location'); });

christocracy commented 4 years ago

The onLocation event is fired with each recorded location while both iOS & Android app is running in the foreground or background.

shikha1992 commented 4 years ago

headless event is working fine but onlocationChange is not getting fire at background

christocracy commented 4 years ago

but onlocationChange is not getting fire at background

There's no such event called onLocationChange. It's called onLocation

shikha1992 commented 4 years ago

but onlocationChange is not getting fire at background

There's no such event called onLocationChange. It's called onLocation

My bad . I am talking about this method bg.BackgroundGeolocation.onLocation((bg.Location location) {

christocracy commented 4 years ago

What makes you think onLocation is "not being called" in the background?

shikha1992 commented 4 years ago

headless events are getting fired at background and I am able to see logs but not onLocation. I do not see logs inside this method. When at foreground its working okay. Can you please help what I might be missing here?

christocracy commented 4 years ago

I do not have this problem.

void _onLocation(bg.Location location) {
    print("[location] - $location");
}
$ adb logcat *:S flutter:V

07-27 11:30:42.881 32134  1611 I flutter : [location] - [Location {odometer: 108191.203125, activity: {confidence: 100, type: running}, extras: {}, event: motionchange, timestampMeta: {elapsedRealtime: 268416410, time: 1595863842730, systemTime: 1595863842807, systemClockElaspsedRealtime: 268416487}, battery: {level: 0.93, is_charging: true}, uuid: 29c102e6-8906-42b5-acd0-2ba943cb4ac8, coords: {altitude: 45.9, heading: 181.11, latitude: 45.5188987, accuracy: 25.2, speed: 0.1, longitude: -73.6005841}, is_moving: true, timestamp: 2020-07-27T15:30:42.730Z}]

Can you please help what I might be missing here?

No idea.

shikha1992 commented 4 years ago
void main() async{
  await SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown])
      .then((_){
    runApp(StoreProvider(
        store: store, child: Application(store))
    );
  }
  );

//start here
 bg.BackgroundGeolocation.ready(bg.Config(
    desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH,
    distanceFilter: 10.0,
    stopOnTerminate: false,
    startOnBoot: true,
    allowIdenticalLocations: true,
    notificationTitle: "Greenplay",
    notificationText: "Fetching location",
    debug: false,
    enableHeadless: true,
    foregroundService: true,
    forceReloadOnMotionChange: true,
    logLevel: bg.Config.LOG_LEVEL_VERBOSE,
    fastestLocationUpdateInterval: 1,
    isMoving: true,
//    disableElasticity: true,
  )).then((bg.State state) {
    if (!state.enabled) {
      if (PrefsSingleton.prefs.getString(PreferenceNames.token) != null) {
        bg.BackgroundGeolocation.start();
        bg.BackgroundGeolocation.changePace(true);
      } else {
//        bg.BackgroundGeolocation.stop();
      }
    }
  });

//android
 bg.BackgroundGeolocation.registerHeadlessTask(
        backgroundGeolocationHeadlessTask);

//ios
bg.BackgroundGeolocation.onLocation((bg.Location location) {
    print('[location] - $location');
    var hh = GetDeviceType.getDeviceType();
    print("hh:... $hh");
    if (GetDeviceType.getDeviceType() == "ios") {
     //do smoething
    }
  });

}
shikha1992 commented 4 years ago
I/TSLocationManager(13809): [c.t.l.scheduler.ScheduleEvent onOneShot] 
I/TSLocationManager(13809): ╔═════════════════════════════════════════════
I/TSLocationManager(13809): β•‘ ⏰ OneShot event fired: TERMINATE_EVENT
I/TSLocationManager(13809): ╠═════════════════════════════════════════════
D/TSLocationManager(13809): [c.t.l.a.BackgroundGeolocation isMainActivityActive] NO
D/TSLocationManager(13809): [c.t.f.b.HeadlessTask onHeadlessEvent] πŸ’€ [HeadlessTask terminate]
D/TSLocationManager(13809): [c.t.f.b.HeadlessTask dispatch] [HeadlessTask] waiting for client to initialize
D/TSLocationManager(13809): [c.t.f.b.HeadlessTask onMethodCall] $ initialized
I/flutter (13809): πŸ“¬ --> [HeadlessEvent name: terminate, event: [State enabled: true, isMoving: true, trackingMode: 1, desiredAccuracy: -1, distanceFilter: 10.0, odometer: 39.54228591918945, schedulerEnabled: false, foregroundService: true]]
I/flutter (13809): πŸ“¬ --> terminate
I/flutter (13809): [getCurrentPosition] Headless: [HeadlessEvent name: terminate, event: [State enabled: true, isMoving: true, trackingMode: 1, desiredAccuracy: -1, distanceFilter: 10.0, odometer: 39.54228591918945, schedulerEnabled: false, foregroundService: true]]
I/TSLocationManager(13809): [c.t.l.s.ActivityRecognitionService a] 
I/TSLocationManager(13809): ╔═════════════════════════════════════════════
I/TSLocationManager(13809): β•‘ Motion Transition Result
I/TSLocationManager(13809): ╠═════════════════════════════════════════════
I/TSLocationManager(13809): β•Ÿβ”€ πŸ”΄  EXIT: still
I/TSLocationManager(13809): β•Ÿβ”€ 🎾  ENTER: walking
I/TSLocationManager(13809): β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
D/TSLocationManager(13809): [c.t.l.a.BackgroundGeolocation isMainActivityActive] NO
D/TSLocationManager(13809): [c.t.f.b.HeadlessTask onHeadlessEvent] πŸ’€ [HeadlessTask activitychange]
I/TSLocationManager(13809): [c.t.l.s.TSScheduleManager cancelOneShot] 
I/TSLocationManager(13809):   ⏰ Cancel OneShot: STOP_TIMEOUT
I/flutter (13809): πŸ“¬ --> [HeadlessEvent name: activitychange, event: [ActivityChangeEvent walking (100%)]]
I/flutter (13809): πŸ“¬ --> activitychange
I/flutter (13809): [ActivityChangeEvent walking (100%)]
I/flutter (13809): act_change: [ActivityChangeEvent walking (100%)]
D/TSLocationManager(13809): [c.t.l.s.ActivityRecognitionService onDestroy] 
D/TSLocationManager(13809):   πŸ”΄  ActivityRecognitionService destroyed
I/TSLocationManager(13809): [c.t.l.s.ActivityRecognitionService a] 
I/TSLocationManager(13809): ╔═════════════════════════════════════════════
I/TSLocationManager(13809): β•‘ Motion Transition Result
I/TSLocationManager(13809): ╠═════════════════════════════════════════════
I/TSLocationManager(13809): β•Ÿβ”€ πŸ”΄  EXIT: walking
I/TSLocationManager(13809): β•Ÿβ”€ 🎾  ENTER: still
I/TSLocationManager(13809): β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
D/TSLocationManager(13809): [c.t.l.a.BackgroundGeolocation isMainActivityActive] NO
D/TSLocationManager(13809): [c.t.f.b.HeadlessTask onHeadlessEvent] πŸ’€ [HeadlessTask activitychange]
I/TSLocationManager(13809): [c.t.l.s.TSScheduleManager oneShot] 
I/TSLocationManager(13809):   ⏰ Scheduled OneShot: STOP_TIMEOUT in 300000ms (jobID: 2059034116)
I/TSLocationManager(13809): [c.t.l.l.TSLocationManager removeLocationUpdates] 
I/TSLocationManager(13809):   πŸ”΄  Location-services: OFF
I/TSLocationManager(13809): [c.t.l.l.TSLocationManager requestLocationUpdates] 
I/TSLocationManager(13809):   🎾  Location-services: ON
D/TSLocationManager(13809): [c.t.l.adapter.TSConfig translateDesiredAccuracy] translateDesiredAccuracy (true): -1
I/flutter (13809): πŸ“¬ --> [HeadlessEvent name: activitychange, event: [ActivityChangeEvent still (100%)]]
I/flutter (13809): πŸ“¬ --> activitychange
I/flutter (13809): [ActivityChangeEvent still (100%)]
I/flutter (13809): act_change: [ActivityChangeEvent still (100%)]
D/TSLocationManager(13809): [c.t.l.s.ActivityRecognitionService onDestroy] 
D/TSLocationManager(13809):   πŸ”΄  ActivityRecognitionService destroyed
D/TSLocationManager(13809): [c.t.l.service.TrackingService b] 
D/TSLocationManager(13809): ╔═════════════════════════════════════════════
D/TSLocationManager(13809): β•‘ TrackingService: LocationResult
D/TSLocationManager(13809): ╠═════════════════════════════════════════════
D/TSLocationManager(13809): β•Ÿβ”€ πŸ“  Location[fused 31******,77****** hAcc=5 et=+4d4h48m40s153ms alt=269.55430338997394 vel=4.3992934E-12 bear=180.0 vAcc=10 sAcc=??? bAcc=???], age: 198ms, time: 1595863785000
D/TSLocationManager(13809): [c.t.l.service.TrackingService b] Received stoppedAt location
I/TSLocationManager(13809): [c.t.l.service.TrackingService b] 
I/TSLocationManager(13809):   ℹ️  Distance from stoppedAtLocation: -9.8
D/TSLocationManager(13809): [c.t.l.l.TSLocationManager onLocationResult] 
D/TSLocationManager(13809): ╔═════════════════════════════════════════════
D/TSLocationManager(13809): β•‘ Process LocationResult
D/TSLocationManager(13809): ╠═════════════════════════════════════════════
D/TSLocationManager(13809): [c.t.l.l.TSLocationManager locationIsInvalid] Distance from last location: 5.4094343, apparent speed: 0.02331311
D/TSLocationManager(13809): [c.t.l.l.TSLocationManager calculateMedianAccuracy] Median accuracy: 37.445
D/TSLocationManager(13809): [c.t.l.a.BackgroundGeolocation isMainActivityActive] NO
D/TSLocationManager(13809): [c.t.f.b.HeadlessTask onHeadlessEvent] πŸ’€ [HeadlessTask location]
I/TSLocationManager(13809): [c.t.l.data.sqlite.b persist] 
I/TSLocationManager(13809):   βœ…  INSERT: 8ef3943f-e7d1-47b4-b27e-66aa91bf32ef
I/flutter (13809): πŸ“¬ --> [HeadlessEvent name: location, event: [Location {is_moving: true, uuid: 8ef3943f-e7d1-47b4-b27e-66aa91bf32ef, timestamp: 2020-07-27T15:29:45.000Z, odometer: 39.5, coords: {latitude: 30.7042127, longitude: 76.7081062, accuracy: 4.9, speed: 0, heading: 180, altitude: 269.6}, activity: {type: still, confidence: 100}, battery: {is_charging: true, level: 0.33}, extras: {}}]]
I/flutter (13809): πŸ“¬ --> location
I/flutter (13809): [Location {is_moving: true, uuid: 8ef3943f-e7d1-47b4-b27e-66aa91bf32ef, timestamp: 2020-07-27T15:29:45.000Z, odometer: 39.5, coords: {latitude: 30.7042127, longitude: 76.7081062, accuracy: 4.9, speed: 0, heading: 180, altitude: 269.6}, activity: {type: still, confidence: 100}, battery: {is_charging: true, level: 0.33}, extras: {}}]
I/zygote64(13809): Do full code cache collection, code=501KB, data=295KB
I/zygote64(13809): After code cache collection, code=468KB, data=251KB
I/TSLocationManager(13809): [c.t.l.scheduler.ScheduleEvent onOneShot] 
I/TSLocationManager(13809): ╔═════════════════════════════════════════════
I/TSLocationManager(13809): β•‘ ⏰ OneShot event fired: STOP_TIMEOUT
I/TSLocationManager(13809): ╠═════════════════════════════════════════════
D/TSLocationManager(13809): [c.t.l.adapter.TSConfig d] ℹ️   Persist config, dirty: [isMoving]
I/TSLocationManager(13809): [c.t.l.l.TSLocationManager removeLocationUpdates] 
I/TSLocationManager(13809):   πŸ”΄  Location-services: OFF
I/TSLocationManager(13809): [c.t.l.service.HeartbeatService c] 
I/TSLocationManager(13809):   πŸ”΄  Stop heartbeat
D/TSLocationManager(13809): [c.t.locationmanager.util.b b] 
D/TSLocationManager(13809):   ℹ️  LocationAuthorization: Permission granted
I/TSLocationManager(13809): [c.t.l.service.TrackingService a] 
I/TSLocationManager(13809):   πŸ”΅  setPace: true β†’ false
I/TSLocationManager(13809): [c.t.l.s.LocationRequestService a] 
I/TSLocationManager(13809):   ℹ️  Location availability: true
I/TSLocationManager(13809): [c.t.l.s.LocationRequestService b] 
I/TSLocationManager(13809): ╔═════════════════════════════════════════════
I/TSLocationManager(13809): β•‘ motionchange LocationResult: 4
I/TSLocationManager(13809): ╠═════════════════════════════════════════════
I/TSLocationManager(13809): β•Ÿβ”€ πŸ“  Location[fused 31******,77****** hAcc=5 et=+4d4h53m41s118ms alt=269.55430338997394 vel=7.613017E-17 bear=288.68527 vAcc=10 sAcc=??? bAcc=???], age: 148ms, time: 1595864086000
I/TSLocationManager(13809): [c.t.l.l.TSLocationManager onSingleLocationResult] 
I/TSLocationManager(13809):   πŸ”΅  Acquired motionchange position, isMoving: false
D/TSLocationManager(13809): [c.t.l.l.TSLocationManager calculateMedianAccuracy] Median accuracy: 37.445
D/TSLocationManager(13809): [c.t.l.s.LocationRequestService onDestroy] 
D/TSLocationManager(13809): [c.t.l.s.LocationRequestService b] SingleLocationRequest 4 isFinished? true
D/TSLocationManager(13809): [c.t.l.a.BackgroundGeolocation isMainActivityActive] NO
D/TSLocationManager(13809): [c.t.f.b.HeadlessTask onHeadlessEvent] πŸ’€ [HeadlessTask location]
I/TSLocationManager(13809): [c.t.l.s.ActivityRecognitionService b] 
I/TSLocationManager(13809):   🎾  Start motion-activity updates
D/TSLocationManager(13809): [c.t.l.g.TSGeofenceManager startMonitoringStationaryRegion] 
D/TSLocationManager(13809):   🎾  Start monitoring stationary region (radius: 150.0m 30.7042127,76.7081062 hAcc=4.9)
I/TSLocationManager(13809): [c.t.l.data.sqlite.b persist] 
I/TSLocationManager(13809):   βœ…  INSERT: 74f6c649-bd34-4a83-9a85-1729b4bbe934
D/TSLocationManager(13809): [c.t.l.a.BackgroundGeolocation isMainActivityActive] NO
D/TSLocationManager(13809): [c.t.f.b.HeadlessTask onHeadlessEvent] πŸ’€ [HeadlessTask motionchange]
I/flutter (13809): πŸ“¬ --> [HeadlessEvent name: location, event: [Location {event: motionchange, is_moving: false, uuid: 74f6c649-bd34-4a83-9a85-1729b4bbe934, timestamp: 2020-07-27T15:34:46.000Z, odometer: 39.5, coords: {latitude: 30.7042127, longitude: 76.7081062, accuracy: 4.9, speed: 0, heading: 288.69, altitude: 269.6}, activity: {type: still, confidence: 100}, battery: {is_charging: true, level: 0.33}, extras: {}}]]
I/flutter (13809): πŸ“¬ --> location
I/flutter (13809): [Location {event: motionchange, is_moving: false, uuid: 74f6c649-bd34-4a83-9a85-1729b4bbe934, timestamp: 2020-07-27T15:34:46.000Z, odometer: 39.5, coords: {latitude: 30.7042127, longitude: 76.7081062, accuracy: 4.9, speed: 0, heading: 288.69, altitude: 269.6}, activity: {type: still, confidence: 100}, battery: {is_charging: true, level: 0.33}, extras: {}}]
I/TSLocationManager(13809): [c.t.l.service.TrackingService i] 
I/TSLocationManager(13809): ╔═════════════════════════════════════════════
I/TSLocationManager(13809): β•‘ TrackingService motionchange: false
I/TSLocationManager(13809): ╠═════════════════════════════════════════════
I/flutter (13809): πŸ“¬ --> [HeadlessEvent name: motionchange, event: [Location {event: motionchange, is_moving: false, uuid: 74f6c649-bd34-4a83-9a85-1729b4bbe934, timestamp: 2020-07-27T15:34:46.000Z, odometer: 39.5, coords: {latitude: 30.7042127, longitude: 76.7081062, accuracy: 4.9, speed: 0, heading: 288.69, altitude: 269.6}, activity: {type: still, confidence: 100}, battery: {is_charging: true, level: 0.33}, extras: {}}]]
I/TSLocationManager(13809): [c.t.l.s.TSScheduleManager cancelOneShot] 
I/TSLocationManager(13809):   ⏰ Cancel OneShot: STOP_TIMEOUT
I/flutter (13809): πŸ“¬ --> motionchange
I/flutter (13809): [Location {event: motionchange, is_moving: false, uuid: 74f6c649-bd34-4a83-9a85-1729b4bbe934, timestamp: 2020-07-27T15:34:46.000Z, odometer: 39.5, coords: {latitude: 30.7042127, longitude: 76.7081062, accuracy: 4.9, speed: 0, heading: 288.69, altitude: 269.6}, activity: {type: still, confidence: 100}, battery: {is_charging: true, level: 0.33}, extras: {}}]
I/flutter (13809): aaaa111
D/TSLocationManager(13809): [c.t.l.service.TrackingService onDestroy] 
D/TSLocationManager(13809):   πŸ”΄  TrackingService destroyed
D/TSLocationManager(13809): [c.t.l.s.ActivityRecognitionService a] 
D/TSLocationManager(13809):   🚘 ️DetectedActivity [type=STILL, confidence=100]
I/TSLocationManager(13809): [c.t.l.s.ActivityRecognitionService a] 
I/TSLocationManager(13809): ╔═════════════════════════════════════════════
I/TSLocationManager(13809): β•‘ Motion Transition Result
I/TSLocationManager(13809): ╠═════════════════════════════════════════════
I/TSLocationManager(13809): β•Ÿβ”€ 🎾  ENTER: still
I/TSLocationManager(13809): β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
D/TSLocationManager(13809): [c.t.l.a.BackgroundGeolocation isMainActivityActive] NO
D/TSLocationManager(13809): [c.t.f.b.HeadlessTask onHeadlessEvent] πŸ’€ [HeadlessTask activitychange]
I/flutter (13809): πŸ“¬ --> [HeadlessEvent name: activitychange, event: [ActivityChangeEvent still (100%)]]
I/flutter (13809): πŸ“¬ --> activitychange
I/flutter (13809): [ActivityChangeEvent still (100%)]
I/flutter (13809): act_change: [ActivityChangeEvent still (100%)]
D/TSLocationManager(13809): [c.t.l.s.ActivityRecognitionService onDestroy] 
D/TSLocationManager(13809):   πŸ”΄  ActivityRecognitionService destroyed
shikha1992 commented 4 years ago
void main() async{
  await SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown])
      .then((_){
    runApp(StoreProvider(
        store: store, child: Application(store))
    );
  }
  );

//start here
 bg.BackgroundGeolocation.ready(bg.Config(
    desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH,
    distanceFilter: 10.0,
    stopOnTerminate: false,
    startOnBoot: true,
    allowIdenticalLocations: true,
    notificationTitle: "Greenplay",
    notificationText: "Fetching location",
    debug: false,
    enableHeadless: true,
    foregroundService: true,
    forceReloadOnMotionChange: true,
    logLevel: bg.Config.LOG_LEVEL_VERBOSE,
    fastestLocationUpdateInterval: 1,
    isMoving: true,
//    disableElasticity: true,
  )).then((bg.State state) {
    if (!state.enabled) {
      if (PrefsSingleton.prefs.getString(PreferenceNames.token) != null) {
        bg.BackgroundGeolocation.start();
        bg.BackgroundGeolocation.changePace(true);
      } else {
//        bg.BackgroundGeolocation.stop();
      }
    }
  });

//android
 bg.BackgroundGeolocation.registerHeadlessTask(
        backgroundGeolocationHeadlessTask);

//ios
bg.BackgroundGeolocation.onLocation((bg.Location location) {
    print('[location] - $location');
    var hh = GetDeviceType.getDeviceType();
    print("hh:... $hh");
    if (GetDeviceType.getDeviceType() == "ios") {
     //do smoething
    }
  });

}

see this as well please

christocracy commented 4 years ago

I/flutter (13809): πŸ“¬ --> [HeadlessEvent name: terminate, event

Why would you expect onLocation event to fire when your app is terminated?

christocracy commented 4 years ago

Also, all this code is supposed to exist within your Flutter Application, not in main.dart:

//start here
 bg.BackgroundGeolocation.ready(bg.Config(
    desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH,
    distanceFilter: 10.0,
    stopOnTerminate: false,
    startOnBoot: true,
    allowIdenticalLocations: true,
    notificationTitle: "Greenplay",
    notificationText: "Fetching location",
    debug: false,
    enableHeadless: true,
    foregroundService: true,
    forceReloadOnMotionChange: true,
    logLevel: bg.Config.LOG_LEVEL_VERBOSE,
    fastestLocationUpdateInterval: 1,
    isMoving: true,
//    disableElasticity: true,
  )).then((bg.State state) {
    if (!state.enabled) {
      if (PrefsSingleton.prefs.getString(PreferenceNames.token) != null) {
        bg.BackgroundGeolocation.start();
        bg.BackgroundGeolocation.changePace(true);
      } else {
//        bg.BackgroundGeolocation.stop();
      }
    }
  });

//ios
bg.BackgroundGeolocation.onLocation((bg.Location location) {
    print('[location] - $location');
    var hh = GetDeviceType.getDeviceType();
    print("hh:... $hh");
    if (GetDeviceType.getDeviceType() == "ios") {
     //do smoething
    }
  });

}
shikha1992 commented 4 years ago

I/flutter (13809): mailbox_with_mail --> [HeadlessEvent name: terminate, event

Why would you expect onLocation event to fire when your app is terminated?

but terminated is called when user clears it from background or exit fully from app. Can't we get location fetched after this?

shikha1992 commented 4 years ago

Also, all this code is supposed to exist within your Flutter Application, not in main.dart:

//start here
 bg.BackgroundGeolocation.ready(bg.Config(
    desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH,
    distanceFilter: 10.0,
    stopOnTerminate: false,
    startOnBoot: true,
    allowIdenticalLocations: true,
    notificationTitle: "Greenplay",
    notificationText: "Fetching location",
    debug: false,
    enableHeadless: true,
    foregroundService: true,
    forceReloadOnMotionChange: true,
    logLevel: bg.Config.LOG_LEVEL_VERBOSE,
    fastestLocationUpdateInterval: 1,
    isMoving: true,
//    disableElasticity: true,
  )).then((bg.State state) {
    if (!state.enabled) {
      if (PrefsSingleton.prefs.getString(PreferenceNames.token) != null) {
        bg.BackgroundGeolocation.start();
        bg.BackgroundGeolocation.changePace(true);
      } else {
//        bg.BackgroundGeolocation.stop();
      }
    }
  });

//ios
bg.BackgroundGeolocation.onLocation((bg.Location location) {
    print('[location] - $location');
    var hh = GetDeviceType.getDeviceType();
    print("hh:... $hh");
    if (GetDeviceType.getDeviceType() == "ios") {
     //do smoething
    }
  });

}

This was actually added first inside app. But did not work after termination(in android). So i keep it in main.dart and registered headless in same only and it worked

shikha1992 commented 4 years ago

When I press back button and exit app, "terminated" event is called in which android is working fine for headless but onLocation used for ios stops. What can be look around? Please help me with this, as this is the only part which is left before release of my app.

christocracy commented 4 years ago

Read api docs Config.stopOnTerminate

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and I will leave this open.

stale[bot] commented 4 years ago

Closing this issue after a prolonged period of inactivity. Fell free to reopen this issue, if this still affecting you.