Skip to content

Instantly share code, notes, and snippets.

@dereknelson
Last active September 3, 2019 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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)
}
}
@niv-breez
Copy link

Hi, Is line 39 calling the AsyncStorage to get the username from the device works for you ?
Please let me know, I working on a similar project and I can't access the device storage,
you can refer it here

@dereknelson
Copy link
Author

dereknelson commented Sep 3, 2019

@niv-breez Yes it worked, specifically in your example you have to define the task as an async one:

defineTask(LOCATION_TASK_NAME, async ({ data, error }) => {
  const accessToken = await AsyncStorage.getItem('accessToken')
   ...restOfLogic

However for different reasons I could never get expo's background tasks to run in the standalone app, only the client one.

@niv-breez
Copy link

Hi @dereknelson thank you for your replay.
I checked what you offered but it still not work, it work me with/without your suggestion the same in expo client both background/froeground and killed mode.
But on standalone app it works just on background/foreground, in killed mode it just don't work on standalone app.
I log each location update, I get the location updated in killed mode but the access token is null.

What else am I missing ?

thanks.

@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