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,
"name": "La Traque",

View File

@@ -26,9 +26,9 @@ export const LOCATION_PARAMETERS = {
showsBackgroundLocationIndicator: true, // iOS only
pausesUpdatesAutomatically: false, // (iOS) Prevents auto-pausing of location updates
foregroundService: {
notificationTitle: "Enregistrement de votre position.",
notificationTitle: "La Traque",
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();
}, [loggedIn, setTeamId]);
/*
// Try to log in with saved teamId
useEffect(() => {
if (!loggedIn && teamId) {
login(teamId);
}
}, [loggedIn, teamId, login]);
*/
// Emit battery level and phone model at log in
useEffect(() => {

View File

@@ -1,17 +1,59 @@
// Services
import { socket } from "@/services/socket/connection";
import AsyncStorage from "@react-native-async-storage/async-storage";
const customEmit = (event, ...args) => {
if (!socket?.connected) return false;
export const connectSocketAsync = () => {
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);
socket.emit(event, ...args);
return true;
};
const customEmitCallback = (event, ...args) => {
return new Promise((resolve, reject) => {
if (!socket?.connected) return reject(new Error("Socket not connected"));
const customEmitCallback = async (event, ...args) => {
const isConnected = await connectSocketAsync();
if (!isConnected) {
throw new Error("Socket connection failed");
}
return new Promise((resolve, reject) => {
console.log("Emit", event);
const timeout = setTimeout(() => {

View File

@@ -22,7 +22,11 @@ defineTask(TASKS.BACKGROUND_LOCATION, async ({ data, error }) => {
return;
}
const { latitude, longitude } = locations[0].coords;
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);
};
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) {
// Insert the line in the file of teamID depending on the date (lines are sorted by date)
if (!fs.existsSync(teamIDToPath(teamID))) {
fs.writeFile(teamIDToPath(teamID), line + '\n', errorFile);
fs.writeFileSync(teamIDToPath(teamID), line + '\n', errorFile);
} else {
fs.readFile(teamIDToPath(teamID), 'utf8', (err, data) => {
fs.readFileSync(teamIDToPath(teamID), 'utf8', (err, data) => {
if (err) {
errorFile(err);
return;
@@ -38,10 +49,11 @@ function addLineToFile(teamID, 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() {
const files = fs.readdirSync(UPLOAD_DIR);