Last active
June 22, 2018 19:14
-
-
Save rafwell/d1ef287e253134550049f72895d1fb5f to your computer and use it in GitHub Desktop.
Working with mauron85/cordova-plugin-background-geolocation 3.x with Ionic 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { ApiProvider } from '../../providers/api/api'; | |
import { NativeStorage } from '@ionic-native/native-storage'; | |
import { UserProvider } from '../user/user'; | |
import { Device } from '@ionic-native/device'; | |
import { ENV } from '../../config/environment'; | |
import { AppVersion } from '@ionic-native/app-version'; | |
/* | |
Generated class for the LocationProvider provider. | |
See https://angular.io/guide/dependency-injection for more info on providers | |
and Angular DI. | |
*/ | |
@Injectable() | |
export class LocationProvider { | |
public backgroundGeolocation; | |
public token:any; | |
public version; | |
constructor( | |
public apiProvider: ApiProvider, | |
public nativeStorage: NativeStorage, | |
public userProvider: UserProvider, | |
public device: Device, | |
public api: ApiProvider, | |
public appVersion: AppVersion | |
) { | |
this.backgroundGeolocation = (<any>window).BackgroundGeolocation; | |
} | |
prepare(){ | |
let p1 = this.api.getToken().then(token=>{ | |
this.token = token; | |
}); | |
let p2 = this.appVersion.getVersionNumber().then(version=>{ | |
this.version = version; | |
}); | |
return Promise.all([p1, p2]); | |
} | |
startLocationTracker() { | |
let that = this; | |
this.backgroundGeolocation.configure({ | |
locationProvider: this.backgroundGeolocation.ACTIVITY_PROVIDER, | |
desiredAccuracy: this.backgroundGeolocation.HIGH_ACCURACY, | |
stationaryRadius: 50, | |
distanceFilter: 50, | |
notificationTitle: 'AP Refrigeração', | |
notificationText: 'Em Execução', | |
debug: false, | |
interval: 10000, | |
fastestInterval: 5000, | |
activitiesInterval: 10000, | |
url: ENV.API_URL+'/api/locations', | |
httpHeaders: { | |
'Authorization': 'Bearer '+this.token['access_token'], | |
'App-Version': this.version, | |
'Device-UUID': this.device.uuid, | |
'Device-Model': this.device.model | |
} | |
}); | |
this.backgroundGeolocation.on('location', function(location) { | |
// handle your locations here | |
// to perform long running operation on iOS | |
// you need to create background task | |
that.backgroundGeolocation.startTask(function(taskKey) { | |
// execute long running task | |
// eg. ajax post location | |
// IMPORTANT: task has to be ended by endTask | |
that.backgroundGeolocation.endTask(taskKey); | |
}); | |
}); | |
this.backgroundGeolocation.on('stationary', function(stationaryLocation) { | |
// handle stationary locations here | |
console.log('stationary', stationaryLocation); | |
}); | |
this.backgroundGeolocation.on('error', function(error) { | |
console.log('[ERROR] BackgroundGeolocation error:', error.code, error.message); | |
}); | |
this.backgroundGeolocation.on('start', function() { | |
console.log('[INFO] BackgroundGeolocation service has been started'); | |
}); | |
this.backgroundGeolocation.on('stop', function() { | |
console.log('[INFO] BackgroundGeolocation service has been stopped'); | |
}); | |
this.backgroundGeolocation.on('authorization', function(status) { | |
console.log('[INFO] BackgroundGeolocation authorization status: ' + status); | |
if (status !== this.backgroundGeolocation.AUTHORIZED) { | |
// we need to set delay or otherwise alert may not be shown | |
setTimeout(function() { | |
var showSettings = confirm('App requires location tracking permission. Would you like to open app settings?'); | |
if (showSettings) { | |
return that.backgroundGeolocation.showAppSettings(); | |
} | |
}, 1000); | |
} | |
}); | |
/* | |
this.backgroundGeolocation.on('background', function() { | |
console.log('[INFO] App is in background'); | |
// you can also reconfigure service (changes will be applied immediately) | |
that.backgroundGeolocation.configure({ debug: true }); | |
}); | |
this.backgroundGeolocation.on('foreground', function() { | |
console.log('[INFO] App is in foreground'); | |
that.backgroundGeolocation.configure({ debug: false }); | |
});*/ | |
this.backgroundGeolocation.checkStatus(function(status) { | |
console.log('[INFO] BackgroundGeolocation service is running', status.isRunning); | |
console.log('[INFO] BackgroundGeolocation services enabled', status.locationServicesEnabled); | |
console.log('[INFO] BackgroundGeolocation auth status: ' + status.authorization); | |
// you don't need to check status before start (this is just the example) | |
if (!status.isRunning) { | |
that.backgroundGeolocation.start(); //triggers start on start event | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment