Add a foreground service with a wakelock for fetching push notifications (#6321)

* Create `PushHandlingWakeLock` to start a foreground service:

When receiving a push and scheduling the notification fetching, several problems can happen:

1. Some async operation is waiting for a timeout and it takes way longer than that to finish (i.e. timeout of 10s but it took 30s to advance).
2. The same, but when starting new coroutines. I've seen the time between scheduling a coroutine and it running sometimes take up to 1 minute.
3. Notification fetching can be scheduled immediately, but it can take a while to actually run because the OS understands the app is now in Doze.

Having a wakelock that runs as soon as the push handling starts fixes these: it continues the previous wakelock held by either Firebase or the UnifiedPush distributor.

* Acquire the wakelock as soon as we received the pushes in both receivers

* Also release the wakelock ahead of time if possible
This commit is contained in:
Jorge Martin Espinosa 2026-03-17 14:24:26 +01:00 committed by GitHub
parent b88be242ea
commit 8e46e68630
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 287 additions and 6 deletions

View file

@ -0,0 +1,26 @@
/*
* 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.api.push
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
/**
* Abstraction over wakelocks used for push handling to ensure the device stays awake while we handle the push and schedule and run the work.
*/
interface PushHandlingWakeLock {
/**
* Acquire a wakelock. The wakelock will be held for the given [time] or until [unlock] is called, whichever happens first.
*/
fun lock(time: Duration = 1.minutes)
/**
* Release the wakelock. If no wakelock is associated with the key, this method does nothing.
*/
fun unlock()
}