From 77b444581d9ead7cd9ef45f64c09832f9cd9d7a8 Mon Sep 17 00:00:00 2001 From: Jorge Martin Espinosa Date: Tue, 12 May 2026 11:40:46 +0200 Subject: [PATCH] Improve `FetchPushForegroundService`'s reliability (#6757) * Improve `FetchPushForegroundService`'s reliability - Don't use DI, we can just create the notification channel. This should speed up the creation of the service and reduce the number of `ForegroundServiceDidNotStartInTimeException` received. Also use `MainScope` instead of the app's coroutine scope. - Move the wakelock releasing mechanism to `onDestroy` so it's always used. Previously, this would only happen when `stopService` was called, which would only happen when `stopSelf()` is called, but not when the OS or the service manager stops the service. * Add fallback value for the notification channel title * Replace the wrong string for the notification/channel title --------- Co-authored-by: Benoit Marty --- .../libraries/push/impl/di/PushBindings.kt | 17 ----- .../impl/push/FetchPushForegroundService.kt | 65 ++++++++++++------- .../src/main/res/values-cs/translations.xml | 2 +- .../src/main/res/values-da/translations.xml | 2 +- .../src/main/res/values-de/translations.xml | 2 +- .../src/main/res/values-el/translations.xml | 2 +- .../src/main/res/values-et/translations.xml | 2 +- .../src/main/res/values-fi/translations.xml | 2 +- .../src/main/res/values-fr/translations.xml | 2 +- .../src/main/res/values-hr/translations.xml | 2 +- .../src/main/res/values-hu/translations.xml | 2 +- .../src/main/res/values-it/translations.xml | 2 +- .../src/main/res/values-ja/translations.xml | 2 +- .../src/main/res/values-ko/translations.xml | 2 +- .../src/main/res/values-pl/translations.xml | 2 +- .../src/main/res/values-ru/translations.xml | 2 +- .../src/main/res/values-uk/translations.xml | 2 +- .../src/main/res/values-uz/translations.xml | 2 +- .../src/main/res/values-vi/translations.xml | 2 +- .../main/res/values-zh-rTW/translations.xml | 2 +- .../src/main/res/values-zh/translations.xml | 2 +- .../src/main/res/values/localazy.xml | 2 +- 22 files changed, 60 insertions(+), 62 deletions(-) delete mode 100644 libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/di/PushBindings.kt diff --git a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/di/PushBindings.kt b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/di/PushBindings.kt deleted file mode 100644 index 49b2c43bc7..0000000000 --- a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/di/PushBindings.kt +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright (c) 2026 Element Creations Ltd. - * - * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial. - * Please see LICENSE files in the repository root for full details. - */ - -package io.element.android.libraries.push.impl.di - -import dev.zacsweers.metro.AppScope -import dev.zacsweers.metro.ContributesTo -import io.element.android.libraries.push.impl.push.FetchPushForegroundService - -@ContributesTo(AppScope::class) -interface PushBindings { - fun inject(fetchPushForegroundService: FetchPushForegroundService) -} diff --git a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/push/FetchPushForegroundService.kt b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/push/FetchPushForegroundService.kt index 2b3587837e..b1c90a374a 100644 --- a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/push/FetchPushForegroundService.kt +++ b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/push/FetchPushForegroundService.kt @@ -13,17 +13,14 @@ import android.content.pm.ServiceInfo import android.os.Build import android.os.IBinder import android.os.PowerManager +import androidx.core.app.NotificationChannelCompat import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationManagerCompat import androidx.core.app.ServiceCompat -import dev.zacsweers.metro.Inject -import io.element.android.libraries.architecture.bindings import io.element.android.libraries.core.extensions.runCatchingExceptions import io.element.android.libraries.designsystem.utils.CommonDrawables -import io.element.android.libraries.di.annotations.AppCoroutineScope -import io.element.android.libraries.push.impl.di.PushBindings -import io.element.android.libraries.push.impl.notifications.channels.NotificationChannels import io.element.android.libraries.ui.strings.CommonStrings -import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.MainScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch import timber.log.Timber @@ -34,6 +31,12 @@ private const val NOTIFICATION_ID = 1001 // This kind of foreground service can only last up to 3 minutes before onTimeout is called private val wakelockTimeout = 3.minutes.inWholeMilliseconds +// The channel ID to use for the notification of the foreground service. +private const val CHANNEL_ID = "fetch_push_notification_channel" + +// The tag to use for the wakelock, this is used for debugging purposes and should be unique to this service. +private const val WAKELOCK_TAG = "FetchPushService:WakeLock" + /** * Foreground service used to ensure the device stays awake while we handle the pushes and schedule and run the work to fetch the notification content. */ @@ -42,28 +45,35 @@ class FetchPushForegroundService : Service() { return null } - @Inject lateinit var notificationChannels: NotificationChannels - @Inject @AppCoroutineScope lateinit var coroutineScope: CoroutineScope - private val wakelock: PowerManager.WakeLock by lazy { val powerManager = getSystemService(POWER_SERVICE) as PowerManager - powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "FetchPushService:WakeLock").apply { + powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG).apply { setReferenceCounted(false) } } private var isOnForeground = false + private fun ensureNotificationChannelExists() { + NotificationManagerCompat.from(this).createNotificationChannelsCompat( + listOf( + NotificationChannelCompat.Builder(CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_LOW) + .setName(getString(CommonStrings.common_fetching_notifications_title_android).ifEmpty { "Syncing notifications…" }) + .setVibrationEnabled(false) + .setSound(null, null) + .build() + ) + ) + } + override fun onCreate() { - Timber.d("Creating FetchPushForegroundService") + Timber.i("Creating FetchPushForegroundService to handle incoming push, acquiring wakelock for up to $wakelockTimeout ms") + ensureNotificationChannelExists() - bindings().inject(this) - - Timber.d("Starting FetchPushForegroundService with wakelock timeout of $wakelockTimeout ms") // Start the foreground service as soon as possible - val notificationCompat = NotificationCompat.Builder(this, notificationChannels.getSilentChannelId()) + val notificationCompat = NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(CommonDrawables.ic_notification) - .setContentTitle(getString(CommonStrings.common_android_fetching_notifications_title)) + .setContentTitle(getString(CommonStrings.common_fetching_notifications_title_android).ifEmpty { "Syncing notifications…" }) .setProgress(0, 0, true) .setVibrate(longArrayOf(0)) .setSound(null) @@ -103,7 +113,7 @@ class FetchPushForegroundService : Service() { // The timeout is not automatic before Android 15, so we need to schedule it ourselves if (Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) { - coroutineScope.launch { + MainScope().launch { delay(wakelockTimeout) onTimeoutAction(calledByTheSystem = false) } @@ -112,13 +122,18 @@ class FetchPushForegroundService : Service() { return START_NOT_STICKY } - override fun stopService(intent: Intent?): Boolean { - if (isOnForeground) { - wakelock.release() - ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE) - } + override fun onDestroy() { + super.onDestroy() - return super.stopService(intent) + if (isOnForeground) { + Timber.i("Destroying FetchPushForegroundService, releasing wakelock and stopping foreground") + if (wakelock.isHeld) { + wakelock.release() + } + ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE) + } else { + Timber.w("Destroying FetchPushForegroundService that was not running in foreground, this is unexpected") + } } override fun onTimeout(startId: Int) { @@ -127,9 +142,9 @@ class FetchPushForegroundService : Service() { } private fun onTimeoutAction(calledByTheSystem: Boolean) { - Timber.d("onTimeoutAction, calledByTheSystem: $calledByTheSystem, isOnForeground: $isOnForeground") + Timber.w("onTimeoutAction, calledByTheSystem: $calledByTheSystem, isOnForeground: $isOnForeground") if (isOnForeground) { - Timber.d("Wakelock timeout reached, stopping FetchPushForegroundService") + Timber.w("Wakelock timeout reached, stopping FetchPushForegroundService") stopSelf() } } diff --git a/libraries/ui-strings/src/main/res/values-cs/translations.xml b/libraries/ui-strings/src/main/res/values-cs/translations.xml index e177c23957..4a8623f5dc 100644 --- a/libraries/ui-strings/src/main/res/values-cs/translations.xml +++ b/libraries/ui-strings/src/main/res/values-cs/translations.xml @@ -198,7 +198,6 @@ "Pokročilá nastavení" "obrázek" "Analytika" - "Synchronizace oznámení…" "Opustili jste místnost" "Byli jste odhlášeni z relace" "Vzhled" @@ -242,6 +241,7 @@ Důvod: %1$s." "Selhalo" "Oblíbené" "Oblíbené" + "Synchronizace oznámení…" "Soubor" "Soubor smazán" "Soubor uložen" diff --git a/libraries/ui-strings/src/main/res/values-da/translations.xml b/libraries/ui-strings/src/main/res/values-da/translations.xml index 1769a5d223..0837d0bb33 100644 --- a/libraries/ui-strings/src/main/res/values-da/translations.xml +++ b/libraries/ui-strings/src/main/res/values-da/translations.xml @@ -198,7 +198,6 @@ "Avancerede indstillinger" "et billede" "Analyse-værktøj" - "Synkroniserer notifikationer…" "Du forlod rummet" "Du blev logget ud af sessionen" "Udseende" @@ -244,6 +243,7 @@ "Mislykkedes" "Favorit" "Favoritmarkeret" + "Synkroniserer notifikationer…" "Fil" "Fil slettet" "Fil gemt" diff --git a/libraries/ui-strings/src/main/res/values-de/translations.xml b/libraries/ui-strings/src/main/res/values-de/translations.xml index e6332b9df2..1a9190568c 100644 --- a/libraries/ui-strings/src/main/res/values-de/translations.xml +++ b/libraries/ui-strings/src/main/res/values-de/translations.xml @@ -192,7 +192,6 @@ "Erweiterte Einstellungen" "ein Bild" "Analysedaten" - "Benachrichtigungen werden synchronisiert…" "Du hast den Chat verlassen" "Du wurdest aus der Sitzung abgemeldet." "Erscheinungsbild" @@ -236,6 +235,7 @@ Grund: %1$s." "Fehlgeschlagen" "Favorit" "Favorisiert" + "Benachrichtigungen werden synchronisiert…" "Datei" "Datei wurde gelöscht" "Datei gespeichert" diff --git a/libraries/ui-strings/src/main/res/values-el/translations.xml b/libraries/ui-strings/src/main/res/values-el/translations.xml index a12122b00b..e5d6d0d1f5 100644 --- a/libraries/ui-strings/src/main/res/values-el/translations.xml +++ b/libraries/ui-strings/src/main/res/values-el/translations.xml @@ -192,7 +192,6 @@ "Ρυθμίσεις για προχωρημένους" "μια εικόνα" "Στατιστικά στοιχεία" - "Συγχρονισμός ειδοποιήσεων…" "Αποχωρήσατε από την αίθουσα" "Αποσυνδεθήκατε από την περίοδο λειτουργίας" "Εμφάνιση" @@ -236,6 +235,7 @@ "Απέτυχε" "Αγαπημένο" "Είναι αγαπημένο" + "Συγχρονισμός ειδοποιήσεων…" "Αρχείο" "Το αρχείο διαγράφηκε" "Το αρχείο αποθηκεύτηκε" diff --git a/libraries/ui-strings/src/main/res/values-et/translations.xml b/libraries/ui-strings/src/main/res/values-et/translations.xml index e76e3153e1..6004792f51 100644 --- a/libraries/ui-strings/src/main/res/values-et/translations.xml +++ b/libraries/ui-strings/src/main/res/values-et/translations.xml @@ -197,7 +197,6 @@ "Täiendavad seadistused" "pilt" "Analüütika" - "Sünkroonin teavitusi…" "Sina lahkusid jututoast" "Sa olid sessioonist väljaloginud" "Välimus" @@ -242,6 +241,7 @@ Põhjus: %1$s." "Ei õnnestunud" "Lemmik" "Märgitud lemmikuks" + "Sünkroonin teavitusi…" "Fail" "Fail on kustutatud" "Fail on salvestatud" diff --git a/libraries/ui-strings/src/main/res/values-fi/translations.xml b/libraries/ui-strings/src/main/res/values-fi/translations.xml index e35ef5c2e1..1a7f6c50c9 100644 --- a/libraries/ui-strings/src/main/res/values-fi/translations.xml +++ b/libraries/ui-strings/src/main/res/values-fi/translations.xml @@ -193,7 +193,6 @@ "Edistyneet asetukset" "kuva" "Analytiikka" - "Synkronoidaan ilmoituksia…" "Poistuit huoneesta" "Sinut kirjattiin ulos istunnosta" "Ulkoasu" @@ -237,6 +236,7 @@ Syy: %1$s." "Epäonnistui" "Lisää suosikkeihin" "Lisätty suosikkeihin" + "Synkronoidaan ilmoituksia…" "Tiedosto" "Tiedosto poistettu" "Tiedosto tallennettu" diff --git a/libraries/ui-strings/src/main/res/values-fr/translations.xml b/libraries/ui-strings/src/main/res/values-fr/translations.xml index 7617571f17..5cb21b772f 100644 --- a/libraries/ui-strings/src/main/res/values-fr/translations.xml +++ b/libraries/ui-strings/src/main/res/values-fr/translations.xml @@ -198,7 +198,6 @@ "Paramètres avancés" "une image" "Statistiques d’utilisation" - "Synchronisation des notifications…" "Vous avez quitté le salon" "Vous avez été déconnecté de la session" "Apparence" @@ -244,6 +243,7 @@ Raison : %1$s." "Échec" "Favori" "Ajouté aux favoris" + "Synchronisation des notifications…" "Fichier" "Fichier supprimé" "Fichier enregistré" diff --git a/libraries/ui-strings/src/main/res/values-hr/translations.xml b/libraries/ui-strings/src/main/res/values-hr/translations.xml index 7c3cfbd83b..bb6cf79187 100644 --- a/libraries/ui-strings/src/main/res/values-hr/translations.xml +++ b/libraries/ui-strings/src/main/res/values-hr/translations.xml @@ -200,7 +200,6 @@ "Napredne postavke" "slika" "Analitika" - "Sinkronizacija obavijesti…" "Napustili ste sobu" "Odjavljeni ste iz sesije" "Izgled" @@ -246,6 +245,7 @@ Razlog: %1$s ." "Nije uspjelo" "Favorit" "Označeno kao favorit" + "Sinkronizacija obavijesti…" "Datoteka" "Datoteka je izbrisana" "Datoteka je spremljena" diff --git a/libraries/ui-strings/src/main/res/values-hu/translations.xml b/libraries/ui-strings/src/main/res/values-hu/translations.xml index 8973ebc4f6..c477a6b6ba 100644 --- a/libraries/ui-strings/src/main/res/values-hu/translations.xml +++ b/libraries/ui-strings/src/main/res/values-hu/translations.xml @@ -199,7 +199,6 @@ "Speciális beállítások" "egy kép" "Elemzések" - "Értesítések szinkronizálása…" "Elhagyta a szobát" "Ki lett jelentkeztetve a munkamenetből" "Megjelenítés" @@ -245,6 +244,7 @@ Ok: %1$s." "Sikertelen" "Kedvenc" "Kedvencnek jelölve" + "Értesítések szinkronizálása…" "Fájl" "Fájl törölve" "A fájl mentve" diff --git a/libraries/ui-strings/src/main/res/values-it/translations.xml b/libraries/ui-strings/src/main/res/values-it/translations.xml index ba0d6f022c..cdc523a5ab 100644 --- a/libraries/ui-strings/src/main/res/values-it/translations.xml +++ b/libraries/ui-strings/src/main/res/values-it/translations.xml @@ -199,7 +199,6 @@ "Impostazioni avanzate" "un\'immagine" "Statistiche di utilizzo" - "Sincronizzazione delle notifiche…" "Hai lasciato la stanza" "Sei stato disconnesso dalla sessione" "Aspetto" @@ -245,6 +244,7 @@ Motivo:. %1$s" "Fallito" "Preferiti" "Preferita" + "Sincronizzazione delle notifiche…" "File" "File eliminato" "File salvato" diff --git a/libraries/ui-strings/src/main/res/values-ja/translations.xml b/libraries/ui-strings/src/main/res/values-ja/translations.xml index b7a33d4dfd..e25ca0cf4e 100644 --- a/libraries/ui-strings/src/main/res/values-ja/translations.xml +++ b/libraries/ui-strings/src/main/res/values-ja/translations.xml @@ -197,7 +197,6 @@ "高度な設定" "画像" "分析" - "通知を同期中…" "あなたがルームを退出" "セッションからログアウトされました" "テーマ" @@ -243,6 +242,7 @@ "失敗" "お気に入り" "お気に入り" + "通知を同期中…" "ファイル" "ファイルを削除しました" "ファイルを保存しました" diff --git a/libraries/ui-strings/src/main/res/values-ko/translations.xml b/libraries/ui-strings/src/main/res/values-ko/translations.xml index 4f594f002b..3a029e20fc 100644 --- a/libraries/ui-strings/src/main/res/values-ko/translations.xml +++ b/libraries/ui-strings/src/main/res/values-ko/translations.xml @@ -190,7 +190,6 @@ "고급 설정" "이미지" "통계" - "알림 동기화 중…" "방을 떠남" "세션에서 로그아웃되었습니다." "외관" @@ -234,6 +233,7 @@ "실패" "즐겨찾기" "즐겨찾기 됨" + "알림 동기화 중…" "파일" "파일 삭제됨" "파일 저장됨" diff --git a/libraries/ui-strings/src/main/res/values-pl/translations.xml b/libraries/ui-strings/src/main/res/values-pl/translations.xml index 30f2c383b6..1a9a45cd94 100644 --- a/libraries/ui-strings/src/main/res/values-pl/translations.xml +++ b/libraries/ui-strings/src/main/res/values-pl/translations.xml @@ -201,7 +201,6 @@ "Ustawienia zaawansowane" "obraz" "Dane analityczne" - "Synchronizuję powiadomienia…" "Opuściłeś pokój" "Zostałeś wylogowany z sesji" "Wygląd" @@ -247,6 +246,7 @@ Powód: %1$s." "Niepowodzenie" "Ulubione" "Ulubione" + "Synchronizuję powiadomienia…" "Plik" "Plik usunięty" "Plik zapisany" diff --git a/libraries/ui-strings/src/main/res/values-ru/translations.xml b/libraries/ui-strings/src/main/res/values-ru/translations.xml index a691d1d69b..fe852dcfd0 100644 --- a/libraries/ui-strings/src/main/res/values-ru/translations.xml +++ b/libraries/ui-strings/src/main/res/values-ru/translations.xml @@ -195,7 +195,6 @@ "Расширенные настройки" "изображение" "Аналитика" - "Синхронизация уведомлений…" "Вы покинули комнату" "Вы вышли из сессии" "Внешний вид" @@ -239,6 +238,7 @@ "Ошибка" "Избранное" "Избранное" + "Синхронизация уведомлений…" "Файл" "Файл удалён" "Файл сохранен" diff --git a/libraries/ui-strings/src/main/res/values-uk/translations.xml b/libraries/ui-strings/src/main/res/values-uk/translations.xml index db0fa31804..a9e1d14b40 100644 --- a/libraries/ui-strings/src/main/res/values-uk/translations.xml +++ b/libraries/ui-strings/src/main/res/values-uk/translations.xml @@ -201,7 +201,6 @@ "Додаткові налаштування" "зображення" "Аналітика" - "Синхронізація сповіщень…" "Ви вийшли з кімнати" "Ви вийшли з сеансу" "Тема" @@ -247,6 +246,7 @@ "Помилка" "Обране" "Обране" + "Синхронізація сповіщень…" "Файл" "Файл видалено" "Файл збережено" diff --git a/libraries/ui-strings/src/main/res/values-uz/translations.xml b/libraries/ui-strings/src/main/res/values-uz/translations.xml index 304a044e0b..50d14ef4d4 100644 --- a/libraries/ui-strings/src/main/res/values-uz/translations.xml +++ b/libraries/ui-strings/src/main/res/values-uz/translations.xml @@ -193,7 +193,6 @@ "Kengaytirilgan sozlamalar" "rasm" "Analitika" - "Bildirishnomalar sinxronlanmoqda…" "Siz xonani tark etdingiz" "Siz sessiyadan chiqdingiz" "Ko\'rinish" @@ -237,6 +236,7 @@ Sababi:%1$s." "Xatolikka uchradi" "Sevimli" "Sevimli" + "Bildirishnomalar sinxronlanmoqda…" "Fayl" "Fayl o\'chirildi" "Fayl saqlandi" diff --git a/libraries/ui-strings/src/main/res/values-vi/translations.xml b/libraries/ui-strings/src/main/res/values-vi/translations.xml index 2db0ce1dae..825ff12bea 100644 --- a/libraries/ui-strings/src/main/res/values-vi/translations.xml +++ b/libraries/ui-strings/src/main/res/values-vi/translations.xml @@ -191,7 +191,6 @@ "Cài đặt nâng cao" "một hình ảnh" "Phân tích" - "Đang đồng bộ thông báo…" "Bạn rời phòng" "Bạn đã bị đăng xuất" "Giao diện" @@ -235,6 +234,7 @@ Lý do: %1$s ." "Thất bại" "Yêu thích" "Được yêu thích" + "Đang đồng bộ thông báo…" "Tập tin" "Tệp đã bị xóa" "Tệp đã được lưu" diff --git a/libraries/ui-strings/src/main/res/values-zh-rTW/translations.xml b/libraries/ui-strings/src/main/res/values-zh-rTW/translations.xml index 819c993bb1..b4c993e852 100644 --- a/libraries/ui-strings/src/main/res/values-zh-rTW/translations.xml +++ b/libraries/ui-strings/src/main/res/values-zh-rTW/translations.xml @@ -191,7 +191,6 @@ "進階設定" "影像" "分析" - "正在同步通知……" "您離開聊天室" "您已登出工作階段" "外觀" @@ -235,6 +234,7 @@ "失敗" "我的最愛" "我的最愛" + "正在同步通知……" "檔案" "檔案已刪除" "檔案已儲存" diff --git a/libraries/ui-strings/src/main/res/values-zh/translations.xml b/libraries/ui-strings/src/main/res/values-zh/translations.xml index 00bb686f78..7366a4fd95 100644 --- a/libraries/ui-strings/src/main/res/values-zh/translations.xml +++ b/libraries/ui-strings/src/main/res/values-zh/translations.xml @@ -196,7 +196,6 @@ "高级设置" "一张图片" "分析" - "正在同步通知…" "你离开了房间" "你已注销会话" "外观" @@ -242,6 +241,7 @@ "失败" "收藏" "已收藏" + "正在同步通知…" "文件" "文件已删除" "文件已保存" diff --git a/libraries/ui-strings/src/main/res/values/localazy.xml b/libraries/ui-strings/src/main/res/values/localazy.xml index 656ec50569..173a525919 100644 --- a/libraries/ui-strings/src/main/res/values/localazy.xml +++ b/libraries/ui-strings/src/main/res/values/localazy.xml @@ -199,7 +199,6 @@ "Advanced settings" "an image" "Analytics" - "Syncing notifications…" "You left the room" "You were logged out of the session" "Appearance" @@ -245,6 +244,7 @@ Reason: %1$s." "Failed" "Favourite" "Favourited" + "Syncing notifications…" "File" "File deleted" "File saved"