transistorsoft / cordova-background-geolocation-lt

The most sophisticated background location-tracking & geofencing module with battery-conscious motion-detection intelligence for iOS and Android.
http://www.transistorsoft.com/shop/products/cordova-background-geolocation
Other
655 stars 277 forks source link

Enabled is false after configure #683

Closed brainchild115 closed 5 years ago

brainchild115 commented 5 years ago

Your Environment

Expected Behavior

Expect state.enabled to be true. N

Actual Behavior

state.enabled to be false.

Steps to Reproduce

  1. Just run configure it on start up.

Context

Geofence. And your example shows that I should start geofence when state.enabled is true.

Debug logs

Not sure what to show here..

Here is platforms/android/project.properties

target=android-26
android.library.reference.1=CordovaLib
cordova.gradle.include.1=cordova-plugin-badge/bxxxxxxxxxx-badge.gradle
cordova.system.library.1=com.android.support:support-v4:27.0.0
cordova.system.library.2=com.google.gms:google-services:3.2.1
cordova.gradle.include.2=cordova-plugin-bluenet-dfu/bxxxxxxxxxx-build-extras.gradle
cordova.gradle.include.3=cordova-plugin-buildinfo/bxxxxxxxxxx-BuildInfo.gradle
cordova.gradle.include.4=cordova-plugin-firebase/bxxxxxxxxxx-build.gradle
cordova.gradle.include.5=cordova-plugin-local-notification/bxxxxxxxxxx-localnotification.gradle
cordova.system.library.3=com.google.android.gms:play-services-tagmanager:11.8.0
cordova.gradle.include.6=phonegap-plugin-barcodescanner/bxxxxxxxxxx-barcodescanner.gradle
cordova.system.library.4=com.google.firebase:firebase-core:11.8.0
cordova.system.library.5=com.google.firebase:firebase-messaging:11.8.0
cordova.system.library.6=com.google.firebase:firebase-crash:11.8.0
cordova.system.library.7=com.google.firebase:firebase-config:11.8.0
cordova.system.library.8=com.android.support:support-v4:27.0.0
cordova.gradle.include.7=cordova-android-support-gradle-release/bxxxxxxxxxx-cordova-android-support-gradle-release.gradle
cordova.gradle.include.8=cordova-plugin-background-fetch/bxxxxxxxxxx-build.gradle
cordova.system.library.9=com.google.android.gms:play-services-location:11.8.0
cordova.system.library.10=com.android.support:appcompat-v7:27.0.0
cordova.gradle.include.9=cordova-background-geolocation-lt/bxxxxxxxxxx-build.gradle
christocracy commented 5 years ago

Show me all the javascript you're using with the plugin.

brainchild115 commented 5 years ago
class Geofence extends EventEmitter2 {
  constructor() {
    super();

    this.geofencesRegistered = undefined;
    this._setup();
  }

  static instance() {
    if (typeof Geofence._instance === 'undefined') {
      Geofence._instance = new Geofence();
    }
    return Geofence._instance;
  }

  isGeofenceRegisteredByUuid(id) {
    return this._fillGeofencesRegisteredIfNotExists()
    .then(() => {
      return (id in this.geofencesRegistered);
    });
  }

  getGeofences() {
    return new Promise((resolve, reject) => {
      window.BackgroundGeolocation.getGeofences((geofences) => {
        // Each one: console.log("Geofence: ", geofence.identifier, geofence.radius, geofence.latitude, 
        //                        geofence.longitude);
        resolve(geofences);
      }, (err) => {
        console.log(`Failed to fetch geofences from server: ${err.message}`);
        reject(err);
      });
    });
  }

  addRegionIfNotExists(region) {
    console.log('addRegionIfNotExists called');
    return new Promise((resolve, reject) => {
      this.isGeofenceRegisteredByUuid(region.identifier)
      .then((registered) => {
        if (registered) {
          return new Promise(); // just to be consistent for eslint, otherwise return is just fine
        } else {
          return this._addRegion(region);
        }
      })
      .then(() => {
        resolve(); // done 
      })
      .catch((err) => {
        reject(err);
      });
    });
  }

  removeById(id) {
    return new Promise((resolve, reject) => {
      let bgGeo = window.BackgroundGeolocation;
      bgGeo.removeGeofence(id, () => {
        console.log('Geofence.remove successful for id:' + id);
        resolve();
      }, (err) => {
        console.log('Geofence.remove error: ' + JSON.stringify(err));
        reject(err);
      });
    });
  }

  removeAllGeofences() {
    return new Promise((resolve, reject) => {
      BackgroundGeolocation.removeGeofences(() => {
        console.log("Successfully removed alll geofences");
        resolve();
      }, (error) => {
        console.warn("Failed to remove geofence", error);
        reject();
      });
    });
  }

  _addRegion(region) {
    new Promise((resolve, reject) => {
      window.BackgroundGeolocation.addGeofences([region], () => {
        console.log('Add geofence-lt success');
        resolve();
      }, (err) => {
        console.log('Add geofence-lt failure:' + err.message);
        reject(err);
      });
    });
  }

  registerAddedRegions() {
  }

  start() {
    return new Promise((resolve, reject) => {
      window.BackgroundGeolocation.startGeofences((state) => {
        console.log('Geofence-only monitoring started', state.trackingMode);
        resolve();
      });
    });
  }

  // This doesn't just stop geofences, this stop all activities tracking
  stop() {
    return new Promise((resolve, reject) => {
      window.BackgroundGeolocation.stop(() => {
        console.log('Geofence stopped.');
        resolve();
      }, (err) => {
        console.log(`Geofence.stop error: ${err.message}`);
        reject(err);
      });
    });
  }

  _setup() {
    this._bind();

    console.log('window.BackgroundGeolocation.configure run');
    window.BackgroundGeolocation.configure({
      desiredAccuracy: 1000, // <-- Config params // 0, 10, 100, 1000, where 1000 is the lowest accuracy
      distanceFilter: 50,
      timeout: 40000, // 40 seconds for every location fetch
      activityRecognitionInterval: 10000, // android
      // locationUpdateInterval: 1000,// With distanceFilter: 0, Sets the desired interval for location updates, in milliseconds.
      timeout: 3000,
      geofenceInitialTriggerEntry: true,
      fastestLocationUpdateInterval: 10000,
      forceReloadOnGeofence: true,
      foregroundService: true,
    }, (state) => { // <-- Current state provided to #configure callback
      console.log('BackgroundGeolocation is configured and ready to use. State:' + JSON.stringify(state));
      /*
      if (!state.enabled) {
      } else {
      }
      */
    }, (err) => {
      console.log(`Background Geolocation failed to configure: ${err.message}`);
    });
  }

  _bind() {
    window.BackgroundGeolocation.on('geofence', (evt) => { // for crossing events
      //
      // {"location":{"event":"geofence","is_moving":false,"uuid":"3a81721f-7116-4cc9-b53c-88d4ca884399",
      //              "timestamp":"2017-12-13T06:57:35.805Z","odometer":0,
      //              "coords":{"latitude":25.0536594,"longitude":121.5996323,"accuracy":5.3,"speed":-1,"heading":-1,"altitude":-1},
      //              "activity":{"type":"still","confidence":100},"battery":{"is_charging":true,"level":0.95},
      //              "geofence":{"identifier":"3b450b2418325db2fb7e9eddf96d6f3a","action":"EXIT"},
      //              "extras":{}},"identifier":"3b450b2418325db2fb7e9eddf96d6f3a","action":"EXIT"}"
      console.log('geofence crossing event received:' + JSON.stringify(evt));

      // Make it to look like format of an older plugin:
      let obj = {};
      obj.latitude = evt.location.coords.latitude;
      obj.longitude = evt.location.coords.longitude;
      obj.radius = 200;
      obj.id = evt.location.uuid; // or is it identifier?
      obj.transitionType = (evt.location.geofence.action === 'EXIT') ? 2 : 1; // CLRegionStateInside 1, CLRegionStateOutside 2
      this.emit('regionTransition', [obj]);
    });
  }

  _fillGeofencesRegisteredIfNotExists() {
    return new Promise((resolve, reject) => {
      if (this.geofencesRegistered) {
        resolve();
      } else {
        this.getGeofences()
        .then((geofences) => {
          this.geofencesRegistered = {};
          for (const geofence of geofences) {
            this.geofencesRegistered[geofence.uuid] = geofence;
          }
        })
        .then(() => {
          resolve();
        })
        .catch((err) => {
          console.log(`_fillGeofencesRegistered error: ${err.message}`);
          reject(err);
        });
      }
    });
  }
}

// export { Geofence };
christocracy commented 5 years ago

Btw, as of 2.11.0, the plugin's API already uses promises. For example, see getGeofences. Notice how just about every API method return new Promise(...).

As for your issue, you've not configured stopOnTerminate: false (the default is true). Therefore, every time you terminate the app, the plugin stops, thus enabled: false every time you boot.

brainchild115 commented 5 years ago

Does this has to do with the license key? It is enabled: false still.

enabled: false is now the culprit for no geofence me but I don't know if that is really the case. My Galaxy S9 never can get the "location service activated" notification every time I build a production release. And every time I don't get that, geofence doesn't work.

brainchild115 commented 5 years ago

Also my console shows two license keys of the same app id. I must have pressed again before the first one can be entered. I don't know if this has anything to do with it. When does the license key gets verified? Is it during compilation?

christocracy commented 5 years ago

Show me $ adb logcat of your app booting.

When does the license key gets verified? Is it during compilation?

The plugin knows nothing of the Product Dashboard. The plugin does not authenticate with a server. Authentication occurs within encrypted library code at run time. The plugin reads your LICENSE from the AndroidManifest.xml

brainchild115 commented 5 years ago

logcatOnLaunch.txt

christocracy commented 5 years ago

When sending me logs, always configure logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE.

brainchild115 commented 5 years ago

Verbose was on, hopefully.

logOnLaunch2.txt

christocracy commented 5 years ago

I see nothing interesting.

brainchild115 commented 5 years ago

You got me thinking about the whole thing. I think I'm onto the wrong thing.

If I don't use stopOnTerminate: false then I don't need to call stop right? Our original use case is only to make sure it runs when the app is in the task list.

Currently, this is my use case: User open the apps. Put the app in the background. Now the app will register the geofence if it hasn't done so already. And a notification that says Location service activated should be received (because foregroundService is set to true).

If I am only using geofence and not other type of tracking. I only need to call startGeofences and not start right? And I don't need to stop if my stopOnTerminate is true (as by default)?

But now I don't see that notification. And everytime I don't see the notification, so far when I take the time to test it in the wild, it doesn't seem to work on Galaxy S9. So now I just want to see that notification first.

Here is the log where I don't set stopOnTerminate to false. I start the app. Put it in the background. And wait to see that notification, but it doens't come up.

LogOnLaunch3.txt

christocracy commented 5 years ago

I think you are importing an insufficient version of appcompat-v7. Paste your platforms/android/project.properties

brainchild115 commented 5 years ago
target=android-26
android.library.reference.1=CordovaLib
cordova.gradle.include.1=cordova-android-support-gradle-release/brilonglock-cordova-android-support-gradle-release.gradle
cordova.gradle.include.2=cordova-plugin-background-fetch/brilonglock-build.gradle
cordova.system.library.1=com.google.android.gms:play-services-location:11.8.0
cordova.system.library.2=com.android.support:appcompat-v7:27.0.0
cordova.gradle.include.3=cordova-background-geolocation-lt/brilonglock-build.gradle
cordova.gradle.include.4=cordova-plugin-badge/brilonglock-badge.gradle
cordova.gradle.include.5=cordova-plugin-bluenet-dfu/brilonglock-build-extras.gradle
cordova.gradle.include.6=cordova-plugin-buildinfo/brilonglock-BuildInfo.gradle
cordova.system.library.3=com.android.support:support-v4:27.0.0
cordova.system.library.4=com.android.support:support-v4:27.0.0
cordova.gradle.include.7=cordova-plugin-local-notification/brilonglock-localnotification.gradle
cordova.gradle.include.8=phonegap-plugin-barcodescanner/brilonglock-barcodescanner.gradle
cordova.system.library.5=com.android.support:support-v4:27.0.0
cordova.system.library.6=com.android.support:support-v13:26.+
cordova.system.library.7=me.leolin:ShortcutBadger:1.1.17@aar
cordova.system.library.8=com.google.firebase:firebase-messaging:11.8.0
cordova.gradle.include.9=phonegap-plugin-push/brilonglock-push.gradlea
brainchild115 commented 5 years ago

Any clues? The weird thing is I can get it work without a license key.

christocracy commented 5 years ago

Obviously this an issue:

cordova.system.library.6=com.android.support:support-v13:26.+

Since every other support lib is importing 27.0.0

brainchild115 commented 5 years ago

Hmm..somehow that creeps in.

Here is a build which does not have that issue. I ran the app with the same procedure. But I still don't get that notification.

It seems like callback to window.BackgroundGeolocation.startGeofences() doesn't get called. (Same code as I pasted already above.)

Attached is the log.

LogOnLaunch4.txt

And project.properties pasted here:

target=android-26 android.library.reference.1=CordovaLib cordova.gradle.include.1=cordova-plugin-badge/brilonglock-badge.gradle cordova.system.library.1=com.android.support:support-v4:27.0.0 cordova.system.library.2=com.google.gms:google-services:3.2.1 cordova.gradle.include.2=cordova-plugin-bluenet-dfu/brilonglock-build-extras.gradle cordova.gradle.include.3=cordova-plugin-buildinfo/brilonglock-BuildInfo.gradle cordova.gradle.include.4=cordova-plugin-firebase/brilonglock-build.gradle cordova.gradle.include.5=cordova-plugin-local-notification/brilonglock-localnotification.gradle cordova.system.library.3=com.google.android.gms:play-services-tagmanager:11.8.0 cordova.gradle.include.6=phonegap-plugin-barcodescanner/brilonglock-barcodescanner.gradle cordova.system.library.4=com.google.firebase:firebase-core:11.8.0 cordova.system.library.5=com.google.firebase:firebase-messaging:11.8.0 cordova.system.library.6=com.google.firebase:firebase-crash:11.8.0 cordova.system.library.7=com.google.firebase:firebase-config:11.8.0 cordova.system.library.8=com.android.support:support-v4:27.0.0 cordova.gradle.include.7=cordova-android-support-gradle-release/brilonglock-cordova-android-support-gradle-release.gradle cordova.gradle.include.8=cordova-plugin-background-fetch/brilonglock-build.gradle cordova.system.library.9=com.google.android.gms:play-services-location:11.8.0 cordova.system.library.10=com.android.support:appcompat-v7:27.0.0 cordova.gradle.include.9=cordova-background-geolocation-lt/brilonglock-build.gradle

christocracy commented 5 years ago

The title of this issue is "Enabled is false after configure". Is this not solved?

brainchild115 commented 5 years ago

Maybe I should have create a new issue? I don't know think that is what I want. Like I said in this chain of discussion, my main issue is that there is no Geofence event because service doesn't seem to be run. (No notification when app is in the background on Android 8.) And enabled is false doesn't seem relevant because stopOnTerminate: false doesn't appear to be what I need. I just need geofence event to trigger when it's in the list of apps running.

christocracy commented 5 years ago

I do not see in your logs that you're using LOG_LEVEL_VERBOSE.

brainchild115 commented 5 years ago

That's weird I thought I turned it on.

Here I tried it again the result of adb logcat. I made sure via console.log that callback to setLogLevel was ran. (Though any line with INFO:CONSOLE is removed manually afterward).

Here it is: LogOnLaunch5.txt

Here is project.property just to make sure things have not been changed.

target=android-26
android.library.reference.1=CordovaLib
cordova.gradle.include.1=cordova-plugin-badge/brilonglock-badge.gradle
cordova.system.library.1=com.android.support:support-v4:27.0.0
cordova.system.library.2=com.google.gms:google-services:3.2.1
cordova.gradle.include.2=cordova-plugin-bluenet-dfu/brilonglock-build-extras.gradle
cordova.gradle.include.3=cordova-plugin-buildinfo/brilonglock-BuildInfo.gradle
cordova.gradle.include.4=cordova-plugin-firebase/brilonglock-build.gradle
cordova.gradle.include.5=cordova-plugin-local-notification/brilonglock-localnotification.gradle
cordova.system.library.3=com.google.android.gms:play-services-tagmanager:11.8.0
cordova.gradle.include.6=phonegap-plugin-barcodescanner/brilonglock-barcodescanner.gradle
cordova.system.library.4=com.google.firebase:firebase-core:11.8.0
cordova.system.library.5=com.google.firebase:firebase-messaging:11.8.0
cordova.system.library.6=com.google.firebase:firebase-crash:11.8.0
cordova.system.library.7=com.google.firebase:firebase-config:11.8.0
cordova.system.library.8=com.android.support:support-v4:27.0.0
cordova.gradle.include.7=cordova-android-support-gradle-release/brilonglock-cordova-android-support-gradle-release.gradle
cordova.gradle.include.8=cordova-plugin-background-fetch/brilonglock-build.gradle
cordova.system.library.9=com.google.android.gms:play-services-location:11.8.0
cordova.system.library.10=com.android.support:appcompat-v7:27.0.0
cordova.gradle.include.9=cordova-background-geolocation-lt/brilonglock-build.gradle
brainchild115 commented 5 years ago

Any pointers?

christocracy commented 5 years ago

No idea:

show me those logs.

brainchild115 commented 5 years ago

log.txt

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
target=android-26
android.library.reference.1=CordovaLib
cordova.gradle.include.1=cordova-plugin-badge/brilonglock-badge.gradle
cordova.system.library.1=com.android.support:support-v4:27.0.0
cordova.system.library.2=com.google.gms:google-services:3.2.1
cordova.gradle.include.2=cordova-plugin-bluenet-dfu/brilonglock-build-extras.gradle
cordova.gradle.include.3=cordova-plugin-buildinfo/brilonglock-BuildInfo.gradle
cordova.gradle.include.4=cordova-plugin-firebase/brilonglock-build.gradle
cordova.gradle.include.5=cordova-plugin-local-notification/brilonglock-localnotification.gradle
cordova.system.library.3=com.google.android.gms:play-services-tagmanager:11.8.0
cordova.gradle.include.6=phonegap-plugin-barcodescanner/brilonglock-barcodescanner.gradle
cordova.system.library.4=com.google.firebase:firebase-core:11.8.0
cordova.system.library.5=com.google.firebase:firebase-messaging:11.8.0
cordova.system.library.6=com.google.firebase:firebase-crash:11.8.0
cordova.system.library.7=com.google.firebase:firebase-config:11.8.0
cordova.system.library.8=com.android.support:support-v4:27.0.0
cordova.gradle.include.7=cordova-android-support-gradle-release/brilonglock-cordova-android-support-gradle-release.gradle
cordova.gradle.include.8=cordova-plugin-background-fetch/brilonglock-build.gradle
cordova.system.library.9=com.google.android.gms:play-services-location:11.8.0
cordova.system.library.10=com.android.support:appcompat-v7:27.0.0
cordova.gradle.include.9=cordova-background-geolocation-lt/brilonglock-build.gradle
christocracy commented 5 years ago

Are you building for DEBUG or RELEASE?

brainchild115 commented 5 years ago

If I recall correctly the above is from debug. I think it may be some sort of timing issue, right now it also doesn't work when building for debug.

brainchild115 commented 5 years ago

Hi Chris, any clue to this one?

christocracy commented 5 years ago

Can you zip your project and email it to me? I'll build it locally and see for myself. chris@transistorsoft.com