backgroundLocation.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import * as TaskManager from 'expo-task-manager';
  2. import * as Location from 'expo-location';
  3. import axios from 'axios';
  4. import { storage, StoreType } from 'src/storage';
  5. import { Platform } from 'react-native';
  6. import { API_URL } from 'src/constants';
  7. import { API } from 'src/types';
  8. const LOCATION_TASK_NAME = 'BACKGROUND_LOCATION_TASK';
  9. // TaskManager.defineTask(LOCATION_TASK_NAME, async ({ data, error }) => {
  10. // if (error) {
  11. // console.error('[BackgroundLocation] Task error:', error);
  12. // return;
  13. // }
  14. // if (data) {
  15. // const { locations } = data as any;
  16. // if (locations && locations.length > 0) {
  17. // const { coords, timestamp } = locations[0];
  18. // console.log('[BackgroundLocation] New location:', coords);
  19. // const token = storage.get('token', StoreType.STRING);
  20. // if (!token) {
  21. // return;
  22. // }
  23. // try {
  24. // const response = await axios.postForm(
  25. // API_URL + '/' + API.UPDATE_LOCATION,
  26. // {
  27. // token,
  28. // lat: coords.latitude,
  29. // lng: coords.longitude,
  30. // background: LOCATION_TASK_NAME,
  31. // location_timestamp: timestamp
  32. // },
  33. // {
  34. // headers: {
  35. // Platform: Platform.OS
  36. // }
  37. // }
  38. // );
  39. // console.log('[BackgroundLocation] Location sent:', response.data);
  40. // } catch (sendError) {
  41. // console.error('[BackgroundLocation] Sending location failed:', sendError);
  42. // }
  43. // }
  44. // }
  45. // console.log('[BackgroundLocation] TaskManager end');
  46. // });
  47. export const startBackgroundLocationUpdates = async () => {
  48. const hasStarted = await Location.hasStartedLocationUpdatesAsync(LOCATION_TASK_NAME);
  49. if (hasStarted) {
  50. console.log('[BackgroundLocation] Already started...');
  51. return;
  52. }
  53. await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
  54. accuracy: Location.Accuracy.Highest,
  55. // 10 minutes for Android
  56. timeInterval: 10 * 60 * 1000,
  57. showsBackgroundLocationIndicator: false,
  58. pausesUpdatesAutomatically: false,
  59. // banner on Android
  60. foregroundService: {
  61. notificationTitle: 'NomadMania tracking your location',
  62. notificationBody: 'Location is used in background every 10 minutes.',
  63. notificationColor: '#0F3F4F'
  64. },
  65. // iOS only
  66. activityType: Location.ActivityType.Other
  67. });
  68. console.log('[BackgroundLocation] Started task');
  69. };
  70. export const stopBackgroundLocationUpdates = async () => {
  71. const hasStarted = await Location.hasStartedLocationUpdatesAsync(LOCATION_TASK_NAME);
  72. if (hasStarted) {
  73. await Location.stopLocationUpdatesAsync(LOCATION_TASK_NAME);
  74. console.log('[BackgroundLocation] Stopped task');
  75. }
  76. };