react-native-background-geolocation icon indicating copy to clipboard operation
react-native-background-geolocation copied to clipboard

Background geofencing not working after app is closed or phone is restarted! Even after configuring it corretly.

Open apricopt opened this issue 3 years ago • 5 comments

Your Environment

  • Plugin version: ^4.0.3
  • Platform: iOS or Android : Android
  • OS version: 9 (onwards)
  • Device manufacturer / model: Nexus
  • React Native version (react-native -v): 0.64.0
  • Plugin config
BackgroundGeolocation.ready(
    {
      desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
      distanceFilter: 10,
      reset: true,
      stopTimeout: 1,
      debug: false, // <-- enable this hear sounds for background-geolocation life-cycle.
      persistMode: BackgroundGeolocation.PERSIST_MODE_ALL,
      logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
}

Expected Behavior

Even after configuring it correctly it is not working in background. it stops working I need to it to work even after user closes the app or user restart the app

Actual Behavior

It stops working soon as user close the app or switch to another app.

Steps to Reproduce

  1. Start the app.
  2. Start Geofencing using any event
  3. Close the app
  4. the app will stop detecting position and triggering event based on provided geofences

Context

I am trying to create emergency app which will trigger event based on provided geofences it must work in the background to fulfill its purpose.

apricopt avatar Jun 15 '22 17:06 apricopt

See API docs:

  • Config.stopOnTerminate
  • Config.startOnBoot
  • Config.enabledHeadless

christocracy avatar Jun 15 '22 19:06 christocracy

Also see Wiki Debugging and learn to observe the plugin logs. You MUST observe the plugin's extensive logging in order to successfully implement this plugin. If you don't, you're like a blind man wandering a dark forest searching for treasure that you'll never find.

christocracy avatar Jun 15 '22 19:06 christocracy

Had you been watching the logs, you might have seen something like this:

TSLocationManager:   ⚠️  Tracking initiated in background with stopOnTerminate: true.  Launch refused.

Or this:

TSLocationManager: ╔═════════════════════════════════════════════
TSLocationManager: ║ MainActivity was destroyed
TSLocationManager: ╠═════════════════════════════════════════════
TSLocationManager: ╟─ stopOnTerminate: true
TSLocationManager: ╟─ enabled: false

christocracy avatar Jun 15 '22 20:06 christocracy

@christocracy I am also facing the same issue. onLocation event didn't call after relaunching the app, but it is working fine in the background and even the app is terminated.

Config:

 BackgroundGeolocation.ready({
            // Geolocation Config
            desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
            stationaryRadius: 10,
            distanceFilter: 0,
            locationUpdateInterval: 2500,  
            locationAuthorizationRequest: 'Always',
            // Activity Recognition
            stopTimeout: 10,
            foregroundService: true, //Set true to make the pg
            debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
            logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
            stopOnTerminate: false,   // <-- Allow the background-service to continue tracking when user closes the app.
            startOnBoot: true,        // <-- Auto start tracking when device is powered-up.
            reset: true,
            enableHeadless: true,
            forceReloadOnBoot:true, //Force launch your terminated App after a device reboot or application update.
            backgroundPermissionRationale: {
              title: this.updateCompanyName(I18n.t(BACKGROUND_LOACTION_CONSENT)),
              message: I18n.t(BACKGROUND_LOACTION_CONSENT_MSG),
              positiveAction: I18n.t(CHANGE_TO)+'"{backgroundPermissionOptionLabel}"',
              negativeAction: I18n.t(CANCEL)
            },
            url:url,
            httpTimeout: httpTimeout,
            batchSync:true,
            params: params,
            headers: headers,
            maxRecordsToPersist:maxRecordsToPersist,
            showsBackgroundLocationIndicator: true
          }, function (state) {
            if (!state.enabled) {
                BackgroundGeolocation.start(function () {
                });
            }
        });

Logs:

Screenshot 2022-08-16 at 3 12 58 PM

Any help will be greatly appricaited

tusharmutreja200 avatar Aug 16 '22 09:08 tusharmutreja200

@tusharmutreja200 See Wiki Debugging and learn to fetch plugin logs via .emailLog method. A tiny screenshot of logs is unhelpful.

Also, this issue is titled "Background geofencing not working after app is closed or phone is restarted". You don't seem to be reporting a problem with geofencing. You said "onLocation event didn't call" (and you've not even shown where / when you're subscribing to that listener -- you've only posted your .ready() call.

christocracy avatar Aug 17 '22 15:08 christocracy