segundo plugin plano mauron85 cordova ionic-framework background background-service

plano - cordova-plugin-mauron85-background-geolocation



Obtener ubicación en la aplicación Ionic/Cordova cuando está en segundo plano (3)

¿Es posible ejecutar un servicio en segundo plano si cierro mi aplicación Ionic / Cordova (tanto para iOS como para Android)?

Para el propósito, elegí el plugin https://github.com/katzer/cordova-plugin-background-mode

Hasta ahora tengo ese código:

$ionicPlatform.ready(function () { cordova.plugins.backgroundMode.isEnabled(); cordova.plugins.backgroundMode.configure({ silent: true }) ............ ///do some task )}

Funciona bien si la aplicación va al primer plano, pero tan pronto como cierre la aplicación, la tarea que estoy ejecutando se detiene también. Entonces, ¿hay alguna solución / forma de hacer que mi tarea se ejecute incluso si la aplicación está cerrada?

EDITAR:

También he agregado permisos para iOS y Andorid, pero obtengo el mismo resultado.

EDICION 2:

Lo que intento hacer en segundo plano es escribir mi propia implementación de un servicio significativo de cambio de ubicación, ya que no hay un plugin gratuito para Cordova o PhoneGap que pueda funcionar tanto con iOS como con Android.


Creo que el seguimiento de geolocalización en segundo plano que intentas implementar ya existe como un complemento de cordova, se llama cordova-plugin-mauron85-background-geolocation .

Este plugin es un servicio de geolocalización en primer plano y en segundo plano. Es mucho más batería y datos eficientes que la geolocalización html5 o el plugin de geolocalización cordova.

Hay muchas opciones de configuración, consulte la página de github vinculada anteriormente.


Solución en IONIC-3

importar los complementos

import { Platform } from ''ionic-angular''; import { BackgroundMode } from ''@ionic-native/background-mode'';

agregar en constructor

constructor(private backgroundMode: BackgroundMode, private plt: Platform) { this.initBackgroundMode(); } private initBackgroundMode() { this.plt.ready().then(() => { this.backgroundMode.setDefaults({ silent: true }); this.backgroundMode.enable(); if (this.plt.is("android")) { this.backgroundMode.on(''activate'').subscribe(() => { this.backgroundMode.disableWebViewOptimizations(); // Custom code for updating the location // or do similar functionality // use timeout or interval accordingly to execute the functionality in loop }); } }) }


Marco iónico

Recientemente implementé una función como esta en mi proyecto. Utilicé Ionic y usé el modo de fondo del complemento Cordova de Katzer. (en este momento estoy ejecutando el proceso en segundo plano a través del simulador iOS 9.2).

Aquí hay un fragmento de código para que funcione:

// 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);

Marco Iónico 2

Aquí hay un ejemplo de Ionic 2 ES6 listo:

// 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);