Help get this topic noticed by sharing it on Twitter, Facebook, or email.

Tutorial - Integrating Background Mode Geo-location Cordova Plugin

I have successfully integrated plugin in my app for geolocation services to work in background while showing the icon in the top notification bar.

Step 1: Download the zip folder from the link https://github.com/pmwisdom/cordova-b...

Step 2:Import the Zip folder to Appery Resources -> Cordova Plugins


Step 3: Open your App. Make sure the Library version is v3.0 or above in App Settings -> External Resources.

Step 4: Click on App Settings-> Cordova Plugins -> Imported Cordova Plugin and check the required plugin to be added to your project.



Step 5: Go to startScreen and on DeviceReady select the new Javascript function and paste the below code.

//-----------Background Geolocation Mode
// Plugin config settings done by Tooba Atif

navigator.geolocation.getCurrentPosition(function() {
console.log("Successfully retrieved our GPS position, we can now start our background tracker.");
}, function(error) {
console.error(error);
});

//Get plugin
var bgLocationServices = window.plugins.backgroundLocationServices;

//Congfigure Plugin
bgLocationServices.configure({
//Both
desiredAccuracy: 20, // Desired Accuracy of the location updates (lower means more accurate but more battery consumption)
distanceFilter: 5, // (Meters) How far you must move from the last point to trigger a location update
debug: true, // , err);
});

//Start the Background Tracker. When you enter the background tracking will start, and stop when you enter the foreground.
bgLocationServices.start();

///later, to stop
//bgLocationServices.stop();

Step 6: Add your other app functionality respectively and test. Make sure you are moving as the plugin detects activity and then update the location only to save battery. For more information read the plugin home page.

Let me know if someone got any issues with development.
3 people like
this idea
+1
Reply
  • Hi Atif.

    Thanks for the excellent help.

    This is works for me. I have tried more than one plugins so far but none of them was suitable but this.

    This suitable for my App.

    i have a question about this plugin.

    I receive 3 "toasts" as following :

    1 " Detected Activity was ACTIVE, Start Recording"

    After few seconds one more toast appear

    2 " Detected Activity was ACTIVE, Stop Recording"

    When location update one more toast appear

    3 " We receive an location update"

    Can you guide how can i stop these toasts?

    I don't want to inform the users.

    Thanks.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • 1
    Hi Logan,

    I am glad this worked for you.

    >For removing the toasts please open your plugins zip folder and extract it.
    >Open file src->android->BackgroundLocationUpdateService.java in Notepad++ or any editor.
    >Search for the below lines and comment these out from the code.

    showDebugToast(context, "Detected Activity was STILL, Stop recording");

    showDebugToast(context, "Detected Activity was ACTIVE, Start Recording");

    >Open file BackgroundLocationServicePlugin.java and comment the below line:

    Toast.makeText(context, "We received a location update", Toast.LENGTH_SHORT).show();

    >Save the files and make the zip folder again.
    >Delete the previous plugin folder from Resources-> Cordova Plugins and import the one you just created.

    That's all :)
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • Hi Atif.

    Thanks for this great help. wow.! It works great. now toast problem is gone :D.

    I was just wondering if we don't want to show notifications for location tracking. don't let users know that we tracking location.

    Is that possible to hide or disable this notification? maybe you have done this approach for your app.

    Thanks.
    • Yes its possible.
      Haven't done that for my app but you can do it by disabling the notification n corresponding icon!

      Check plugins support page for help regarding this.

      Thanks.
    • Hi Atif.

      Thanks for your reply.

      I have generate ticket on "GitHub" and the owner of plugin said that it's not possible to remove notification.

      So I thought that i should display notification and I change the "NotificationTitle" and I removed "NotificationText" but I could not able to change its icon.

      Can you help how can I change the icon?

      Thanks.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Hello,
    with this plugin the geo-localization is made even if the smartphone is locked?

    Thanks
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited

  • Hi ,

    I have used above plugin and properly insatalled but if i lock the phone then debugg i got error in the console:

    "Plugin not installed erro why" plz help me?
    Sir in my case i have get let and lng value after phone lock and i have not any button for start and stop background geolocation so tell me enable background geolocation?
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned