13

I'm currently doing a hybrid app using ionic/cordova. The app needs functionality where it pings our backend with its location every 1 minute or so and the backend API will answer if anything interesting is nearby. If the answer is yes the app will queue a local notification which hopefully will make the user open the app. This functionality is needed when the app is in background mode and even when the phone is locked. The app needs to be able to be deployed to both app store, google play and eventually windows phone.

I'm currently using a combination of these three plugins:

https://www.npmjs.com/package/cordova-plugin-geolocation - for location https://github.com/katzer/cordova-plugin-background-mode - for bg mode https://github.com/katzer/cordova-plugin-local-notifications - for local notifications

This currently works on Android when the device is not locked (so it works in foreground and background mode) but when the device is locked it is unable to get the GPS coordinates.

My code currently looks like this:

        // Enable background worker          
        (cordova as any).plugins.backgroundMode.enable();

   intervalPromise = $interval(intervalWork, 30000, 0, false);

    function intervalWork() {
        $log.log('Trying to fetch pos');

        var options = { maximumAge: 30000, timeout: 30000, enableHighAccuracy: false };

        navigator.geolocation.getCurrentPosition(success,
            err,
            options);
    }

    function success(pos) {
        $log.log("lat: " + pos.coords.latitude + " long: " + pos.coords.longitude);

        var Checkin = $resource(ApiDataEndpoint.url + 'checkin/:lat/:lng/', {});

        var res= Checkin.get({ lat: pos.coords.latitude, lng: pos.coords.longitude });

               if (res) { 

                $cordovaLocalNotification.schedule({
                    id: 1,
                    title: 'test',
                    text: 'test',
                }).then(function(result) {
                    $log.log("ok");
                });
            };
         }

So... my questions are:

1) How to get the solution to work when my device is locked (the getCurrentPosition is called even when device is locked but returns timeout)?

2) Is it possible to get this solution to work on iOS?

3) Will an app made this way be approved in google play and app store?

4) If the project is doomed what are my alternatives?

I really need help on this one!

kolli
  • 1,260
  • 2
  • 13
  • 23
iCediCe
  • 1,672
  • 1
  • 15
  • 32
  • I can't answer your questions about whether or not the app will be approved, but I guess that if you can get it to work on Android, it will most likely work on Ios too. If this won't work, you'll probably have to build a nativa app. I guess you could contact the creators of the app, maybe they can tell you whether this is possible or not. – Jur Clerkx Mar 21 '16 at 09:07

3 Answers3

13

So I currently have an app that addresses all the issues you listed above and here's the plugin I'm using:

https://github.com/mauron85/cordova-plugin-background-geolocation

  1. The plugin makes use of watchPosition() not getCurrentPosition() as this one takes too long to constantly ping the device and consumes more battery power.

  2. This will definitely work for Android & iOS but IMHO it plays nicer with Android than the latter, as far as precision and the keep alive functionality.

  3. I got it into Google Play no problem, Apple does allow this plugin, there are a number of apps using this plugin in the Apple store but Apple will probably initially reject it and ask the apps intention of background usage, you will then have to make an appeal as for what the app is doing in the background and make sure that it doesn't run indefinitely (this was my experience).

    a. You're going to also want to make sure you point out to the Apple peeps that there is a way for the User to turn the background geolocation tracking off. I'm assuming there is? That's their main issue with the usage of the plugin.

Good luck.

Community
  • 1
  • 1
Faraji Anderson
  • 653
  • 6
  • 18
  • Great answer. I will try going for this solution. – iCediCe Mar 27 '16 at 20:28
  • I dont now if this thread is active but can you simply add this plugin to existing cordova plugin and it will run in the background? – larry chambers Dec 13 '18 at 23:38
  • @larrychambers I'm not sure what is meant by 'existing cordova plugin' but this does run in the background and it specifically pings geolocation. If you're just looking for a generic background process try: https://github.com/katzer/cordova-plugin-background-mode – Faraji Anderson Dec 14 '18 at 20:02
  • No, I am using the cordova-plugin-geolocation and it works pretty well. The background one said it can be used along side this I think, but how do I add it and still use my same code? Is this possible? – larry chambers Dec 14 '18 at 21:01
  • @larrychambers To clarify you want to combine `cordova-plugin-background-mode` to `cordova-plugin-background-geolocation`? I'm not sure what you two plugins you want to combine? – Faraji Anderson Dec 14 '18 at 21:05
  • I want to use the background-geolocation, what im asking is do I have to redo what I have already done if I add the new one? – larry chambers Dec 14 '18 at 21:41
  • @larrychambers no I think not. You should be able to upgrade with no issue. – Faraji Anderson Dec 14 '18 at 22:49
  • @Faraji Anderson i have tried but got error in the console :"Plugin not installed error" in the console tell me why got this error? – Kapil Soni May 02 '19 at 07:34
  • There is a paid version of this same thing which was what mauron85 forked that has had some serious development since then. https://github.com/christocracy/cordova-plugin-background-geolocation – Michael Fever Jun 04 '19 at 20:11
  • @Faraji Anderson: sir its not working for me properly actually issue is that its not working in background but in foreground mode its fire only 1 time please not fire continuous please tell me sir how to fix? – Kapil Soni Aug 23 '19 at 06:29
  • 1
    The plugin from mauron85 hasn't been updated in several years and doesn't work with the latest version of cordova-android. There's now a new one that's based on it which can be used instead: https://github.com/haylltd/cordova-background-geolocation-plugin#readme – jamsandwich Jul 13 '23 at 22:14
3

This plugin has a great guide for how to use a meteor server and cordova to do what you need:

zeroasterisk/meteor-cordova-geolocation-background

It configures automatically with both android and iOS. For windows phone I don't know.

  1. Meteor configures this Plugin in Cordova (you have to configure)
  2. Meteor configures this Plugin in Cordova (you have to configure)
  3. Meteor can trigger the Background service to get Geolocation (GPS) details
  4. The Cordova Background service periodically POSTs it's data to the Meteor server (not to the client Cordova instance)
  5. Meteor Server can update a Collection (or anything else)
  6. Meteor Client syncs with the server
Mark Ryan
  • 188
  • 1
  • 11
2

I encountered the exact same issue, using the same plugins that you used. It turns out that it's a device permissions issue.

The cordova-plugin-geolocation plugin does not have the permission to run in the background, and more importantly it doesn't have the permission to track GPS while in the background. While the cordova-plugin-background-mode plugin does allow you to execute code in the background, you won't be able to get GPS coordinates while in the background.

The solution is to add a plugin that supports getting the location data while the app in the background. There are several plugins that support this, including the mauron85/cordova-plugin-background-geolocation plugin (as suggested previously). An alternative is to use the cordova-custom-config plugin which allows you to specify your own custom permissions. Really you can add any plugin, as long as it supports background location services. No additional code is required.

You can verify that you have the correct permissions, by opening the app settings (in iOS) and confirming that the "Always" option is available.

enter image description here

attila226
  • 315
  • 1
  • 13