Fix background location

This commit is contained in:
Sebastien Riviere
2026-03-13 01:46:29 +01:00
parent 471e514981
commit 9913549610
6 changed files with 82 additions and 14 deletions

View File

@@ -11,7 +11,19 @@
} }
} }
], ],
"expo-font" "expo-font",
[
"expo-location",
{
"locationAlwaysPermission": "L'application a besoin de votre position pour suivre votre activité sportive en continu.",
"isAndroidForegroundServiceEnabled": true,
"foregroundService": {
"notificationTitle": "La Traque",
"notificationBody": "L'application utilise votre position en arrière plan.",
"notificationColor": "#FF0000"
}
}
]
], ],
"newArchEnabled": true, "newArchEnabled": true,
"name": "La Traque", "name": "La Traque",

View File

@@ -26,9 +26,9 @@ export const LOCATION_PARAMETERS = {
showsBackgroundLocationIndicator: true, // iOS only showsBackgroundLocationIndicator: true, // iOS only
pausesUpdatesAutomatically: false, // (iOS) Prevents auto-pausing of location updates pausesUpdatesAutomatically: false, // (iOS) Prevents auto-pausing of location updates
foregroundService: { foregroundService: {
notificationTitle: "Enregistrement de votre position.", notificationTitle: "La Traque",
notificationBody: "L'application utilise votre position en arrière plan.", notificationBody: "L'application utilise votre position en arrière plan.",
notificationColor: "#FF0000", // (Android) Notification icon color notificationColor: "#FF0000",
}, },
} }
}; };

View File

@@ -34,14 +34,12 @@ export const AuthProvider = ({ children }) => {
emitLogout(); emitLogout();
}, [loggedIn, setTeamId]); }, [loggedIn, setTeamId]);
/*
// Try to log in with saved teamId // Try to log in with saved teamId
useEffect(() => { useEffect(() => {
if (!loggedIn && teamId) { if (!loggedIn && teamId) {
login(teamId); login(teamId);
} }
}, [loggedIn, teamId, login]); }, [loggedIn, teamId, login]);
*/
// Emit battery level and phone model at log in // Emit battery level and phone model at log in
useEffect(() => { useEffect(() => {

View File

@@ -1,17 +1,59 @@
// Services // Services
import { socket } from "@/services/socket/connection"; import { socket } from "@/services/socket/connection";
import AsyncStorage from "@react-native-async-storage/async-storage";
const customEmit = (event, ...args) => { export const connectSocketAsync = () => {
if (!socket?.connected) return false; return new Promise(async (resolve) => {
if (socket.connected) return resolve(true);
socket.connect();
const connected = await new Promise((res) => {
socket.once("connect", () => res(true));
socket.once("connect_error", () => res(false));
setTimeout(() => res(false), 5000);
});
if (!connected) return resolve(false);
try {
const rawItem = await AsyncStorage.getItem("team_id");
if (rawItem) {
const teamId = JSON.parse(rawItem);
console.log("Emit login");
socket.emit("login", teamId, (response) => {
console.log("Received : ", response);
resolve(response && response.isLoggedIn);
});
setTimeout(() => resolve(false), 3000);
} else {
console.log("No team_id found for auto-login");
resolve(true);
}
} catch (e) {
console.error("Auto-login error:", e);
resolve(false);
}
});
};
const customEmit = async (event, ...args) => {
const isConnected = await connectSocketAsync();
if (!isConnected) {
console.error(`Failed to connect for event: ${event}`);
return false;
}
console.log("Emit", event); console.log("Emit", event);
socket.emit(event, ...args); socket.emit(event, ...args);
return true; return true;
}; };
const customEmitCallback = (event, ...args) => { const customEmitCallback = async (event, ...args) => {
return new Promise((resolve, reject) => { const isConnected = await connectSocketAsync();
if (!socket?.connected) return reject(new Error("Socket not connected")); if (!isConnected) {
throw new Error("Socket connection failed");
}
return new Promise((resolve, reject) => {
console.log("Emit", event); console.log("Emit", event);
const timeout = setTimeout(() => { const timeout = setTimeout(() => {

View File

@@ -22,7 +22,11 @@ defineTask(TASKS.BACKGROUND_LOCATION, async ({ data, error }) => {
return; return;
} }
const { latitude, longitude } = locations[0].coords; const { latitude, longitude } = locations[0].coords;
emitUpdatePosition([latitude, longitude]); try {
emitUpdatePosition([latitude, longitude]);
} catch (e) {
console.log(e);
}
} }
}); });

View File

@@ -17,12 +17,23 @@ const errorFile = (err) => {
if (err) console.error("Error appending to file:", err); if (err) console.error("Error appending to file:", err);
}; };
function addLineToFile(teamID, line) {
// Insert the line at the end of the file named teamID
try {
fs.appendFileSync(teamIDToPath(teamID), line + '\n');
} catch (err) {
console.error("Error appending to file:", err);
}
}
/*
function addLineToFile(teamID, line) { function addLineToFile(teamID, line) {
// Insert the line in the file of teamID depending on the date (lines are sorted by date) // Insert the line in the file of teamID depending on the date (lines are sorted by date)
if (!fs.existsSync(teamIDToPath(teamID))) { if (!fs.existsSync(teamIDToPath(teamID))) {
fs.writeFile(teamIDToPath(teamID), line + '\n', errorFile); fs.writeFileSync(teamIDToPath(teamID), line + '\n', errorFile);
} else { } else {
fs.readFile(teamIDToPath(teamID), 'utf8', (err, data) => { fs.readFileSync(teamIDToPath(teamID), 'utf8', (err, data) => {
if (err) { if (err) {
errorFile(err); errorFile(err);
return; return;
@@ -38,10 +49,11 @@ function addLineToFile(teamID, line) {
} }
} }
lines.splice(insertIndex, 0, line); lines.splice(insertIndex, 0, line);
fs.writeFile(teamIDToPath(teamID), lines.join('\n') + '\n', errorFile); fs.writeFileSync(teamIDToPath(teamID), lines.join('\n') + '\n', errorFile);
}); });
} }
} }
*/
function initTrajectories() { function initTrajectories() {
const files = fs.readdirSync(UPLOAD_DIR); const files = fs.readdirSync(UPLOAD_DIR);