12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import * as TaskManager from 'expo-task-manager';
- import * as Location from 'expo-location';
- import axios from 'axios';
- import { storage, StoreType } from 'src/storage';
- import { Platform } from 'react-native';
- import { API_URL } from 'src/constants';
- import { API } from 'src/types';
- const LOCATION_TASK_NAME = 'BACKGROUND_LOCATION_TASK';
- // TaskManager.defineTask(LOCATION_TASK_NAME, async ({ data, error }) => {
- // if (error) {
- // console.error('[BackgroundLocation] Task error:', error);
- // return;
- // }
- // if (data) {
- // const { locations } = data as any;
- // if (locations && locations.length > 0) {
- // const { coords, timestamp } = locations[0];
- // console.log('[BackgroundLocation] New location:', coords);
- // const token = storage.get('token', StoreType.STRING);
- // if (!token) {
- // return;
- // }
- // try {
- // const response = await axios.postForm(
- // API_URL + '/' + API.UPDATE_LOCATION,
- // {
- // token,
- // lat: coords.latitude,
- // lng: coords.longitude,
- // background: LOCATION_TASK_NAME,
- // location_timestamp: timestamp
- // },
- // {
- // headers: {
- // Platform: Platform.OS
- // }
- // }
- // );
- // console.log('[BackgroundLocation] Location sent:', response.data);
- // } catch (sendError) {
- // console.error('[BackgroundLocation] Sending location failed:', sendError);
- // }
- // }
- // }
- // console.log('[BackgroundLocation] TaskManager end');
- // });
- export const startBackgroundLocationUpdates = async () => {
- const hasStarted = await Location.hasStartedLocationUpdatesAsync(LOCATION_TASK_NAME);
- if (hasStarted) {
- console.log('[BackgroundLocation] Already started...');
- return;
- }
- await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
- accuracy: Location.Accuracy.Highest,
- // 10 minutes for Android
- timeInterval: 10 * 60 * 1000,
- showsBackgroundLocationIndicator: false,
- pausesUpdatesAutomatically: false,
- // banner on Android
- foregroundService: {
- notificationTitle: 'NomadMania tracking your location',
- notificationBody: 'Location is used in background every 10 minutes.',
- notificationColor: '#0F3F4F'
- },
- // iOS only
- activityType: Location.ActivityType.Other
- });
- console.log('[BackgroundLocation] Started task');
- };
- export const stopBackgroundLocationUpdates = async () => {
- const hasStarted = await Location.hasStartedLocationUpdatesAsync(LOCATION_TASK_NAME);
- if (hasStarted) {
- await Location.stopLocationUpdatesAsync(LOCATION_TASK_NAME);
- console.log('[BackgroundLocation] Stopped task');
- }
- };
|