From 1a0838dea3b00a73ebedd9ed6814ebcb2ef263a7 Mon Sep 17 00:00:00 2001 From: Davide Ferrari Date: Mon, 5 Jan 2026 14:15:26 +0100 Subject: [PATCH] fix(sync): run as foreground service with persistent notification MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Call setForeground() at start of doWork() to: - Keep sync running when screen is off - Show persistent notification during sync - Update notification with sync progress 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../java/com/matedroid/data/sync/DataSyncWorker.kt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/matedroid/data/sync/DataSyncWorker.kt b/app/src/main/java/com/matedroid/data/sync/DataSyncWorker.kt index 96544c7..d9dc7d2 100644 --- a/app/src/main/java/com/matedroid/data/sync/DataSyncWorker.kt +++ b/app/src/main/java/com/matedroid/data/sync/DataSyncWorker.kt @@ -60,6 +60,15 @@ class DataSyncWorker @AssistedInject constructor( override suspend fun doWork(): Result { log("Starting data sync worker (attempt ${runAttemptCount})") + // Run as foreground service to prevent being killed when screen is off + // This shows a persistent notification during sync + try { + setForeground(createForegroundInfo("Starting sync...")) + } catch (e: Exception) { + log("Could not start foreground service: ${e.message}") + // Continue anyway - expedited work may still work + } + try { // Get list of cars val carsResult = teslamateRepository.getCars() @@ -85,8 +94,11 @@ class DataSyncWorker @AssistedInject constructor( // Sync cars sequentially to better handle network errors var hasNetworkError = false - for (car in cars) { + for ((index, car) in cars.withIndex()) { try { + // Update notification with current car + setForeground(createForegroundInfo("Syncing car ${index + 1}/${cars.size}...")) + val success = syncRepository.syncCar(car.carId) if (!success) { log("Sync incomplete for car ${car.carId}, will retry")