From 4837a3b845970e10b346afba204f1f74b1d37d31 Mon Sep 17 00:00:00 2001 From: Gyula Gubacsi Date: Wed, 22 Apr 2026 15:28:45 +0100 Subject: [PATCH] fix(notifications): Catch exceptions from notification foreground service The foreground notification listener service eventually times out, as Android 12+ does not allow applications running in the foreground indefinitely. The service than gets interrupted and runs into an exception, which never gets properly caught and so it ends up with the application crashing. While this change doesn't fix the underlying issue, that the foreground notification listener service cannot run any longer, at least it doesn't crash the application. I do wonder, however, if the whole foreground notification listener is not really viable in this form. --- .../vector/app/fdroid/service/GuardAndroidService.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/vector-app/src/fdroid/java/im/vector/app/fdroid/service/GuardAndroidService.kt b/vector-app/src/fdroid/java/im/vector/app/fdroid/service/GuardAndroidService.kt index 3d5cb4bdb48..e4ac1c4a962 100644 --- a/vector-app/src/fdroid/java/im/vector/app/fdroid/service/GuardAndroidService.kt +++ b/vector-app/src/fdroid/java/im/vector/app/fdroid/service/GuardAndroidService.kt @@ -11,6 +11,7 @@ import dagger.hilt.android.AndroidEntryPoint import im.vector.app.core.services.VectorAndroidService import im.vector.app.features.notifications.NotificationUtils import im.vector.lib.strings.CommonStrings +import timber.log.Timber import javax.inject.Inject /** @@ -27,7 +28,14 @@ class GuardAndroidService : VectorAndroidService() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { val notificationSubtitleRes = CommonStrings.notification_listening_for_notifications val notification = notificationUtils.buildForegroundServiceNotification(notificationSubtitleRes, false) - startForeground(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, notification) + try { + startForeground(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, notification) + } catch (e: Exception) { + Timber.e("## Sync: Failed to start GuardAndroidService as foreground service: ${e.message}") + // Stop the service if we can't start it as foreground + stopSelf() + return START_NOT_STICKY + } return START_STICKY } }