Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GPS coordinates in background in cordova app

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!

like image 796
iCediCe Avatar asked Mar 16 '16 12:03

iCediCe


People also ask

What does watch position () is used for?

The Geolocation method watchPosition() method is used to register a handler function that will be called automatically each time the position of the device changes. You can also, optionally, specify an error handling callback function.

What is navigator Geolocation?

The Navigator geolocation property is used to return a geolocation object by the browser which can be used to locate a user's position. It is a read-only property and is only available on the approval of the user since it can compromise the user's privacy.

What is a Geolocation plugin?

The Geolocation plugin provides information about the device's location, such as latitude and longitude. Common sources of location information include Global Positioning System (GPS) and location inferred from network signals such as IP address, RFID, WiFi and Bluetooth MAC addresses, and GSM/CDMA cell IDs.


2 Answers

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.

like image 150
Faraji Anderson Avatar answered Sep 23 '22 02:09

Faraji Anderson


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
like image 30
Mark Ryan Avatar answered Sep 24 '22 02:09

Mark Ryan