Skip to content

Instantly share code, notes, and snippets.

@dereknelson
Last active September 3, 2019 18:39
Show Gist options
  • Save dereknelson/97c6cb5e6ae36ef3c8668f670b4c3c2d to your computer and use it in GitHub Desktop.
Save dereknelson/97c6cb5e6ae36ef3c8668f670b4c3c2d to your computer and use it in GitHub Desktop.
background location config
import { Permissions, Location, TaskManager, Notifications } from 'expo'
import axios from 'axios'
export const BACKGROUND_LOCATION_UPDATES_TASK = 'background-location-updates'
export const BACKGROUND_GEOFENCING_UPDATES_TASK = 'background-geofencing-updates'
const geofences = [] //I get this from the server, omitted for sake of clarity
const url = 'api.metoo.io'
TaskManager.defineTask(BACKGROUND_LOCATION_UPDATES_TASK, handleLocationUpdate)
TaskManager.defineTask(BACKGROUND_GEOFENCING_UPDATES_TASK, handleGeofencingUpdate)
export async function initializeBackgroundLocationTasks(){
try {
await Permissions.askAsync(Permissions.LOCATION)
let isBGLRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_LOCATION_UPDATES_TASK)
if (!isBGLRegistered) await Location.startLocationUpdatesAsync(BACKGROUND_LOCATION_UPDATES_TASK, {
accuracy: Location.Accuracy.High, timeInterval: 5000, distanceInterval: 10,
})
let isGeofencingRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_GEOFENCING_UPDATES_TASK)
if (!isGeofencingRegistered) await Location.startGeofencingAsync(BACKGROUND_GEOFENCING_UPDATES_TASK, geofences)
return true
} catch (error) {
return false
}
}
export async function handleLocationUpdate({ data, error }) {
if (error) return
const username = await AsyncStorage.getItem('username')
if (data) {
try {
const { locations } = data
const res = await axios.get(`${url}/${__DEV__ ? 'dev/' : ''}userlocation/${username}/${JSON.stringify(locations[0].coords)}`)
} catch (error) {
console.log('location update error',error)
}
}
}
export async function handleGeofencingUpdate({ data: { eventType, region }, error }) {
if (error) return
const username = await AsyncStorage.getItem('username')
const url = eventType == Location.GeofencingEventType.Enter ? 'entered' : 'exited'
try {
// const response = await metooApi.sendLocation({ link: JSON.stringify(region), token })
const response = await axios.get(siccurl(`${url}/${__DEV__ ? 'dev/' : ''}geofence/${username}/${url}/${JSON.stringify(region)}`))
const body = eventType === Location.GeofencingEventType.Enter ? 'Entered region' : 'Exited region'
Notifications.presentLocalNotificationAsync({ title: '', body, ios: { sound: true } })
} catch (error) {
console.log('geofence error',error)
}
}
@dereknelson
Copy link
Author

dereknelson commented Sep 3, 2019

@niv-breez hmm I never got it to work at all in the background (meaning quit, and re-awakened by the OS for a location update) on a standalone so I can't help you there, this is getting beyond my level of expertise. I see you opened an issue with the expo team, that's probably next best thing you can do. Good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment