Created
May 18, 2018 07:53
-
-
Save GillesVercammen/aba915e6e53bd9abe5ba382f15c2742d to your computer and use it in GitHub Desktop.
full backgroundgeolocation code
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
state = { | |
activeSlide: FIRSTITEM, | |
markerPlaceAble: false, | |
promptVisible: false, | |
placedMarkerCoords: {}, | |
types: [], | |
status: '', | |
statusAlways: '', | |
alertShowed: false, | |
region: null, | |
stationaries: [], | |
isTrackingRunning: false, | |
manualStop: false, | |
showAlert: true | |
} | |
async componentWillMount() { | |
const { retrieveMeetingDataUser, user } = this.props | |
const emailAddress = user.emailAddress | |
await retrieveMeetingDataUser(emailAddress) | |
this._requestPermission('location') | |
} | |
componentWillUnmount() { | |
AppState.removeEventListener('change', this._handleAppStateChange) | |
BackgroundGeolocation.events.forEach(event => BackgroundGeolocation.removeAllListeners(event)) | |
} | |
componentDidMount() { | |
BackgroundGeolocation.configure({ | |
desiredAccuracy: BackgroundGeolocation.MEDIUM_ACCURACY, | |
stationaryRadius: 50, | |
distanceFilter: 50, | |
notificationTitle: 'Background tracking', | |
notificationText: 'enabled', | |
debug: true, | |
startOnBoot: false, | |
stopOnTerminate: false, | |
locationProvider: BackgroundGeolocation.DISTANCE_FILTER_PROVIDER, | |
interval: 10000, | |
fastestInterval: 5000, | |
activitiesInterval: 10000, | |
stopOnStillActivity: false, | |
visitorArrivedAtFirstMeeting: false | |
}) | |
BackgroundGeolocation.on('location', location => { | |
COUNT += 1 | |
if (COUNT === 10) { | |
console.log('VILLERMONT NOW') | |
location = { | |
latitude: 51.140195, | |
longitude: 4.441995 | |
} | |
} | |
// const FCM = firebase.messaging() | |
// FCM.createLocalNotification({ | |
// title: 'hallo', | |
// body: 'new location' | |
// }) | |
// handle your locations here | |
// to perform long running operation on iOS | |
// you need to create background task | |
BackgroundGeolocation.startTask(taskKey => { | |
const { meetingData } = this.props | |
const { activeSlide, showAlert } = this.state | |
const meetingTime = meetingData[0]._data.meetingLocation.coordinate | |
const start = { | |
latitude: location.latitude, | |
longitude: location.longitude | |
} | |
const end = { | |
latitude: meetingTime.latitude, | |
longitude: meetingTime.longitude | |
} | |
const distanceBetweenTwoCoords = haversine(start, end, { unit: 'meter' }) | |
console.log('distance: ', distanceBetweenTwoCoords) | |
if (Math.round(distanceBetweenTwoCoords) < MIN_DISTANCE_TO_MEETING) { | |
this.setState({ | |
visitorArrivedAtFirstMeeting: true, | |
showAlert: true, | |
manualStop: true | |
}) | |
const FCM = firebase.messaging() | |
FCM.createLocalNotification({ | |
title: 'Arrived at meeting location', | |
body: 'You have arrived at your meeting location, let your contact know that you have arrived!' | |
}) | |
let buttons = [{ text: 'Cancel', style: 'cancel' }] | |
buttons.push({ | |
text: 'Yes!', | |
onPress: this._sendMessageArrived | |
}) | |
Alert.alert( | |
'You have arrived!', | |
'You have arrived at your meeting location, would you like to let your contact know that you have arrived?', | |
buttons | |
) | |
} | |
// execute long running task | |
// eg. ajax post location | |
// IMPORTANT: task has to be ended by endTask | |
BackgroundGeolocation.endTask(taskKey) | |
}) | |
}) | |
BackgroundGeolocation.on('stationary', location => { | |
console.log('[DEBUG] BackgroundGeolocation stationary', location) | |
BackgroundGeolocation.startTask(taskKey => { | |
requestAnimationFrame(() => { | |
const stationaries = this.state.stationaries.slice(0) | |
if (location.radius) { | |
const longitudeDelta = 0.01 | |
const latitudeDelta = 0.01 | |
const region = Object.assign({}, location, { | |
latitudeDelta, | |
longitudeDelta | |
}) | |
const stationaries = this.state.stationaries.slice(0) | |
stationaries.push(location) | |
this.setState({ stationaries, region }) | |
} | |
BackgroundGeolocation.endTask(taskKey) | |
}) | |
}) | |
}) | |
BackgroundGeolocation.on('error', error => { | |
console.log('[ERROR] BackgroundGeolocation error:', error) | |
}) | |
BackgroundGeolocation.on('start', () => { | |
console.log('[INFO] BackgroundGeolocation service has been started') | |
}) | |
BackgroundGeolocation.on('stop', () => { | |
console.log('[INFO] BackgroundGeolocation service has been stopped') | |
}) | |
BackgroundGeolocation.on('background', () => { | |
console.log('[INFO] App is in background') | |
const { status, statusAlways, manualStop } = this.state | |
if (status === 'authorized' && statusAlways === 'authorized' && !manualStop) { | |
BackgroundGeolocation.configure({ | |
locationProvider: BackgroundGeolocation.DISTANCE_FILTER_PROVIDER, | |
interval: 10000, | |
fastestInterval: 5000, | |
activitiesInterval: 10000 | |
}) | |
console.log('IN BACKGROUND AND STARTING') | |
BackgroundGeolocation.stop() | |
BackgroundGeolocation.start() | |
} | |
}) | |
BackgroundGeolocation.on('foreground', () => { | |
console.log('[INFO] App is in foreground') | |
const { status, manualStop } = this.state | |
if (status === 'authorized' && !manualStop) { | |
BackgroundGeolocation.configure({ | |
locationProvider: BackgroundGeolocation.RAW_PROVIDER, | |
interval: 10000, | |
fastestInterval: 5000, | |
activitiesInterval: 10000 | |
}) | |
console.log('IN FOREGROUND AND STARTING') | |
BackgroundGeolocation.stop() | |
BackgroundGeolocation.start() | |
} | |
}) | |
// you can also just start without checking for status | |
// BackgroundGeolocation.start(); | |
let types = Permissions.getTypes() | |
let canOpenSettings = Permissions.canOpenSettings() | |
this.setState({ types, canOpenSettings }) | |
this._updatePermissions('location', 'didmount') | |
AppState.addEventListener('change', this._handleAppStateChange) | |
} | |
//update permissions when app comes back from settings | |
_handleAppStateChange = appState => { | |
const { status, manualStop } = this.state | |
if (appState === 'active') { | |
this._updatePermissions('location', 'handle') | |
if (status === 'authorized' && !manualStop) { | |
console.log('[NOTE] app is in handleAppstate and authorized, starting tracking!!!') | |
BackgroundGeolocation.configure({ | |
locationProvider: BackgroundGeolocation.RAW_PROVIDER, | |
interval: 20000, | |
fastestInterval: 5000, | |
activitiesInterval: 10000 | |
}) | |
console.log('HANDLE STATE AND STARTING') | |
BackgroundGeolocation.stop() | |
BackgroundGeolocation.start() | |
} | |
} | |
BackgroundGeolocation.checkStatus(({ isRunning }) => { | |
console.log('[NOTE] CHECKING THE TRACKING STATUS!!!') | |
if (isRunning) { | |
console.log('[NOTE] TRACKING IS RUNNING') | |
this.setState({ isTrackingRunning: true }) | |
} else { | |
console.log('[NOTE] TRACKING IS NOT RUNNING') | |
this.setState({ isTrackingRunning: false }) | |
} | |
}) | |
} | |
_openSettings = () => Permissions.openSettings().then(() => {}) | |
_updatePermissions = (type, from) => { | |
Permissions.check(type).then(status => { | |
this.setState({ status: status }) | |
}) | |
Permissions.check('location', 'always').then(status => { | |
this.setState({ statusAlways: status }) | |
}) | |
} | |
_requestPermission = permission => { | |
let options | |
if (permission === 'location') { | |
options = 'always' | |
} | |
Permissions.request(permission, options) | |
.then(res => { | |
this.setState({ | |
status: res | |
}) | |
if (res !== 'authorized') { | |
let buttons = [{ text: 'Cancel', style: 'cancel' }] | |
if (this.state.canOpenSettings) { | |
buttons.push({ | |
text: 'Open Settings', | |
onPress: this._openSettings | |
}) | |
} | |
Alert.alert( | |
'Whoops!', | |
'There was a problem getting your location sharing permissions. Please enable it from settings.', | |
buttons | |
) | |
} | |
}) | |
.catch(e => console.warn(e)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment