Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get location in Ionic/Cordova app when in background

Is it possible to run a background service if I close my Ionic/Cordova app (both for iOS and Android) ?

For the purpose I picked that pluging https://github.com/katzer/cordova-plugin-background-mode

So far I have that code:

$ionicPlatform.ready(function () {
            cordova.plugins.backgroundMode.isEnabled();

            cordova.plugins.backgroundMode.configure({
                silent: true
            }) 
              ............
            ///do some task
)}

It works fine if the app goes to the foreground but as soon as I close the application the task I am running stops as well. So is there any workaround/way to make my task running even if the app is closed ?

EDIT:

I have also added permissions for both iOS and Andorid but I am getting the same result.

EDIT 2:

What am I trying to do in background is to write my own implementation of significant location-change service since there is no free plugin for Cordova or PhoneGap that can work with both iOS and Android.

like image 389
radioaktiv Avatar asked Oct 13 '15 20:10

radioaktiv


People also ask

How do I get the location in the background in react native?

Geolocation is enabled by default when you create a project with react-native init . In order to enable geolocation in the background, you need to include the 'NSLocationAlwaysUsageDescription' key in Info. plist and add location as a background mode in the 'Capabilities' tab in Xcode.


2 Answers

Ionic Framework

I've recently implemented a feature like this in my project. I did use Ionic and I did use the Cordova plugin background mode from Katzer. (right now I'm running the background process through the iOS 9.2 simulator).

Here's a code snippet to get it working:

// Run when the device is ready document.addEventListener('deviceready', function () {      // Android customization     // To indicate that the app is executing tasks in background and being paused would disrupt the user.     // The plug-in has to create a notification while in background - like a download progress bar.     cordova.plugins.backgroundMode.setDefaults({          title:  'TheTitleOfYourProcess',         text:   'Executing background tasks.'     });      // Enable background mode     cordova.plugins.backgroundMode.enable();      // Called when background mode has been activated     cordova.plugins.backgroundMode.onactivate = function () {          // Set an interval of 3 seconds (3000 milliseconds)         setInterval(function () {              // The code that you want to run repeatedly          }, 3000);     } }, false); 

Ionic Framework 2

Here's an Ionic 2 example ES6 ready:

// Import the Ionic Native plugin  import { BackgroundMode } from 'ionic-native';  // Run when the device is ready document.addEventListener('deviceready', () => {      // Android customization     // To indicate that the app is executing tasks in background and being paused would disrupt the user.     // The plug-in has to create a notification while in background - like a download progress bar.     BackgroundMode.setDefaults({          title:  'TheTitleOfYourProcess',         text:   'Executing background tasks.'     });      // Enable background mode     BackgroundMode.enable();      // Called when background mode has been activated     // note: onactive now returns an returns an observable that emits when background mode is activated     BackgroundMode.onactivate.subscribe(() => {           // The code that you want to run repeatedly     }); }, false); 
like image 130
0x1ad2 Avatar answered Oct 10 '22 09:10

0x1ad2


I think the background geolocation tracking that you're trying to implement already exists as a cordova plugin, it's called cordova-plugin-mauron85-background-geolocation.

This plugin is both a foreground and background geolocation service. It is far more battery and data efficient then html5 geolocation or cordova-geolocation plugin.

There are a lot of configuration options, see the github page linked above.

like image 27
theDmi Avatar answered Oct 10 '22 09:10

theDmi