Good morning people and good year, I am with a problem that I have been carrying for weeks and he looked for many solutions for all Google and none works for me, since I ask you to forgive me for my bad English! I tell them maybe someone can help me:
I am in a phone tracking system that works perfectly with ionic 3.9.2 and observe the follow-up with the angular angle in the latitude and longitude of the data that the application sends a firebase, until then everything is great. But I have a small problem when it comes to the application work in the background, I have installed background mode, background geolocation and powermanagement and this happens to me:
Lock the phone with the lock button and send the coordinates a firebase for no more than 2 min.
If I refer to a middle button that is in the background, it directly stops me from passing the coordinates.
What I need is to be able to use the phone normally and the application.
I hope you have explained me well!
Thanks and I hope someone has already gone through this and you can help me!
Thanks again !!
Scripts:
App.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { IonicStorageModule } from '@ionic/storage';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { LoginPage } from '../pages/login/login';
import { UsuarioProvider } from '../providers/usuario/usuario';
import { AgmCoreModule } from '@agm/core';
// Plugins
import { BackgroundGeolocation, } from '@ionic-native/background-geolocation';
import { Geolocation } from '@ionic-native/geolocation';
// Firebase
import { AngularFireModule } from 'angularfire2';
import { AngularFirestoreModule } from 'angularfire2/firestore';
import { firebaseConfig } from '../config/firebase.config';
import { UbicacionProvider } from '../providers/ubicacion/ubicacion';
import { BackgroundMode } from '@ionic-native/background-mode';
@NgModule({
declarations: [
MyApp,
HomePage,
LoginPage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
AngularFireModule.initializeApp(firebaseConfig),
AngularFirestoreModule,
IonicStorageModule.forRoot(),
AgmCoreModule.forRoot({
apiKey: 'AIzaSyA-HXVa2jtkGfKtIJwisxgC46RaWqC1xuI'
})
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
LoginPage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
UsuarioProvider,
UbicacionProvider,
Geolocation,
BackgroundMode,
BackgroundGeolocation
]
})
export class AppModule {}
App.component.ts
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { BackgroundGeolocation } from '@ionic-native/background-geolocation';
import { Geolocation } from '@ionic-native/geolocation';
import { HomePage } from '../pages/home/home';
import { LoginPage } from '../pages/login/login';
import { UsuarioProvider } from '../providers/usuario/usuario';
import { BackgroundMode } from '@ionic-native/background-mode';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any;
constructor(platform: Platform,
statusBar: StatusBar,
splashScreen: SplashScreen,
public _usuarioProv: UsuarioProvider,
private backgroundMode: BackgroundMode,
private backgroundGeolocation: BackgroundGeolocation) {
platform.ready().then(() => {
_usuarioProv.cargarStorage().then( existe => {
statusBar.styleDefault();
splashScreen.hide();
if ( existe ) {
this.backgroundMode.enable();
this.backgroundGeolocation.start();
this.rootPage = HomePage;
}else {
this.rootPage = LoginPage;
}
});
});
}
}
Provider - ubicacion.ts
import { Injectable } from '@angular/core';
import { Geolocation } from '@ionic-native/geolocation';
import { AngularFirestore, AngularFirestoreDocument } from 'angularfire2/firestore';
import { UsuarioProvider } from '../usuario/usuario';
import { Subscription } from 'rxjs/Subscription';
import { BackgroundGeolocation } from '@ionic-native/background-geolocation';
@Injectable()
export class UbicacionProvider {
taxista: AngularFirestoreDocument<any>;
private watch: Subscription;
constructor( private afDB: AngularFirestore,
private geolocation: Geolocation,
public _usuarioProv: UsuarioProvider,
public backgroundGeolocation: BackgroundGeolocation) {
// this.taxista = afDB.doc(`/usuarios/${ _usuarioProv.clave }`);
}
inicializarTaxista(){
this.taxista = this.afDB.doc(`/usuarios/${ this._usuarioProv.clave }`);
}
iniciarGeoLocalizacion() {
let config = {
desiredAccuracy: 0,
stationaryRadius: 20,
distanceFilter: 10,
debug: true,
interval: 2000
};
this.geolocation.getCurrentPosition().then((resp) => {
// resp.coords.latitude
// resp.coords.longitude
this.taxista.update({
lat: resp.coords.latitude,
lng: resp.coords.longitude,
clave: this._usuarioProv.clave
});
this.watch = this.geolocation.watchPosition()
.subscribe((data) => {
// data can be a set of coordinates, or an error (if an error occurred).
// data.coords.latitude
// data.coords.longitude
this.taxista.update({
lat: data.coords.latitude,
lng: data.coords.longitude,
clave: this._usuarioProv.clave
});
this.backgroundGeolocation.start();
console.log( this.taxista );
});
}).catch((error) => {
console.log('Error getting location', error);
});
}
detenerUbicacion() {
try {
this.watch.unsubscribe();
}catch(e){
console.log(JSON.stringify(e));
}
}
}
Help Please!
Thanks!