Tentative fix for ForegroundServiceStartNotAllowedException (#6509)

* Tentative fix for `ForegroundServiceStartNotAllowedException`

When failing to start the service in foreground, don't crash. This is a helper to speed up the scheduling by keeping the CPU awake, not a critical part that should succeed

* Simplify `DefaultPushHandlingWakeLock`

It seems like restarting the service from background won't work in some cases, so don't try it.
This commit is contained in:
Jorge Martin Espinosa 2026-04-02 11:10:47 +02:00 committed by GitHub
parent b64efe222f
commit 15dc582279
2 changed files with 7 additions and 13 deletions

View file

@ -14,7 +14,6 @@ import dev.zacsweers.metro.SingleIn
import io.element.android.libraries.di.annotations.ApplicationContext import io.element.android.libraries.di.annotations.ApplicationContext
import io.element.android.libraries.push.api.push.PushHandlingWakeLock import io.element.android.libraries.push.api.push.PushHandlingWakeLock
import timber.log.Timber import timber.log.Timber
import java.util.concurrent.atomic.AtomicInteger
import kotlin.time.Duration import kotlin.time.Duration
@ContributesBinding(AppScope::class) @ContributesBinding(AppScope::class)
@ -22,24 +21,13 @@ import kotlin.time.Duration
class DefaultPushHandlingWakeLock( class DefaultPushHandlingWakeLock(
@ApplicationContext private val context: Context, @ApplicationContext private val context: Context,
) : PushHandlingWakeLock { ) : PushHandlingWakeLock {
private val count = AtomicInteger(0)
override fun lock(time: Duration) { override fun lock(time: Duration) {
Timber.d("Acquiring wakelock for push handling, starting service.") Timber.d("Acquiring wakelock for push handling, starting service.")
FetchPushForegroundService.startIfNeeded(context) FetchPushForegroundService.startIfNeeded(context)
count.incrementAndGet()
} }
override suspend fun unlock() { override suspend fun unlock() {
Timber.d("Releasing wakelock used for push handling.") Timber.d("Releasing wakelock used for push handling.")
FetchPushForegroundService.stop(context) FetchPushForegroundService.stop(context)
if (count.decrementAndGet() <= 0) {
Timber.d("No more wakelock needed for push handling, stopping service.")
count.set(0)
} else {
Timber.d("Wakelock still needed for push handling, restarting service | count: ${count.get()}.")
FetchPushForegroundService.startIfNeeded(context)
}
} }
} }

View file

@ -145,7 +145,13 @@ class FetchPushForegroundService : Service() {
fun start(context: Context) { fun start(context: Context) {
val intent = Intent(context, FetchPushForegroundService::class.java) val intent = Intent(context, FetchPushForegroundService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent) runCatchingExceptions { context.startForegroundService(intent) }
.onFailure { throwable ->
Timber.e(
throwable,
"Failed to start FetchPushForegroundService, notifications may take longer than usual to sync"
)
}
} else { } else {
context.startService(intent) context.startService(intent)
} }