Как остановить отслеживание фона, когда приложение было убито в Rayact Native? Я использую Appstate для переключения межIOS

Программируем под IOS
Ответить Пред. темаСлед. тема
Anonymous
 Как остановить отслеживание фона, когда приложение было убито в Rayact Native? Я использую Appstate для переключения меж

Сообщение Anonymous »

React Native - я создаю приложение GPS, где я хочу отслеживать местоположение пользователей, пока он находится в приложении и когда он минимизирует его (работая в фоновом режиме). Когда он полностью отключает приложение (убивает /завершает приложение), я хочу, чтобы приложение остановило отслеживание фоновых. /> [*] Фон - приложение работает в фоновом режиме. The user is either:
  • in another app
  • on the home screen
  • [Android] on another Activity (even if it was launched by your app)
  • [iOS] inactive - This is Состояние, которое происходит при переходе между передним планом и фоном, и в периоды неактивности, таких как вход в многозадачный представление, открытие центра уведомлений или в случае входящего вызова.
Как я могу объяснить, когда приложение было прекращено, поэтому я могу закончить задачу отслеживания фонового отслеживания? /> homescreen.tsx
import { useEffect, useState, useRef } from 'react';
import { foregroundLocationService, LocationUpdate } from '@/services/foregroundLocation';
import { startBackgroundLocationTracking, stopBackgroundLocationTracking } from '@/services/backgroundLocation';
import { speedCameraManager } from '@/src/services/speedCameraManager';

export default function HomeScreen() {
const appState = useRef(AppState.currentState);

useEffect(() => {
requestLocationPermissions();

// Handle app state changes
const subscription = AppState.addEventListener('change', handleAppStateChange);

return () => {
subscription.remove();
foregroundLocationService.stopForegroundLocationTracking();
stopBackgroundLocationTracking();
console.log('HomeScreen unmounted');
};
}, []);

const handleAppStateChange = async (nextAppState: AppStateStatus) => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === 'active'
) {
// App has come to foreground
await stopBackgroundLocationTracking();
await startForegroundTracking();
} else if (
appState.current === 'active' &&
nextAppState.match(/inactive|background/)
) {
// App has gone to background
foregroundLocationService.stopForegroundLocationTracking();
await startBackgroundLocationTracking();
} else if(appState.current.match(/inactive|background/) && nextAppState === undefined || appState.current === 'active' && nextAppState === undefined) {
console.log('HomeScreen unmounted');
}

appState.current = nextAppState;
};


backgroundlocation.ts
import * as Location from 'expo-location';
import * as TaskManager from 'expo-task-manager';
import { cameraAlertService } from '@/src/services/cameraAlertService';
import * as Notifications from 'expo-notifications';
import { speedCameraManager } from '@/src/services/speedCameraManager';
import { notificationService } from '@/src/services/notificationService';

const BACKGROUND_LOCATION_TASK = 'background-location-task';

interface LocationUpdate {
location: Location.LocationObject;
speed: number; // speed in mph
}

// Convert m/s to mph
const convertToMph = (speedMs: number | null): number => {
if (speedMs === null || isNaN(speedMs)) return 0;
return Math.round(speedMs * 2.237); // 2.237 is the conversion factor from m/s to mph
};

// Define the background task
TaskManager.defineTask(BACKGROUND_LOCATION_TASK, async ({ data, error }) => {
if (error) {
console.error(error);
return;
}
if (data) {
const { locations } = data as { locations: Location.LocationObject[] };
const location = locations[0];

const speedMph = convertToMph(location.coords.speed);

console.log('Background Tracking: Location:', location, 'Speed:', speedMph);

// Check for nearby cameras that need alerts
const alertCamera = cameraAlertService.checkForAlerts(
location,
speedMph,
speedCameraManager.getCameras()
);
console.log('Background Alert Camera:', alertCamera);

if (alertCamera) {
// Trigger local notification
await notificationService.showSpeedCameraAlert(alertCamera, speedMph);
console.log('Background Notification Shown');
}
}
});

export const startBackgroundLocationTracking = async (): Promise => {
try {
// Check if background location is available
const { status: backgroundStatus } =
await Location.getBackgroundPermissionsAsync();

if (backgroundStatus === 'granted') {
console.log('Background location permission granted, background location tracking started');
}

if (backgroundStatus !== 'granted') {
console.log('Background location permission not granted');
return false;
}

// Start background location updates
await Location.startLocationUpdatesAsync(BACKGROUND_LOCATION_TASK, {
accuracy: Location.Accuracy.High,
timeInterval: 2000, // Update every 2 seconds
distanceInterval: 5, // Update every 5 meters
deferredUpdatesInterval: 5000, // Minimum time between updates
// Android behavior
foregroundService: {
notificationTitle: "RoadSpy is active",
notificationBody: "Monitoring for nearby speed cameras",
notificationColor: "#FF0000",
},
// iOS behavior
activityType: Location.ActivityType.AutomotiveNavigation,
showsBackgroundLocationIndicator: true,
});

return true;
} catch (error) {
console.error('Error starting background location:', error);
return false;
}
};

export const stopBackgroundLocationTracking = async (): Promise => {
try {
const hasStarted = await TaskManager.isTaskRegisteredAsync(BACKGROUND_LOCATION_TASK);
console.log('Is background task registered:', hasStarted);
if (hasStarted) {
await Location.stopLocationUpdatesAsync(BACKGROUND_LOCATION_TASK);
console.log('Background location tracking stopped');
}
} catch (error) {
console.error('Error stopping background location:', error);
}
};


Подробнее здесь: https://stackoverflow.com/questions/795 ... t-native-i
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «IOS»