Fix wakelock not stopping early when notifications are disabled (#6424)

If notifications for a device are disabled when there is no connection with the HS, the push registration will still exist, so the device can still receive push notifications.

In that cases, we were running into an issue where the wakelock for push notifications was started immediately after receiving a push but was never stopped and it ran for 3 minutes until its timeout, keeping the device awake for no reason.

This patch changes `DefaultPushHandler` so if we don't need the wakelock it returns `false` and we can stop the wakelock early.
This commit is contained in:
Jorge Martin Espinosa 2026-03-23 18:07:25 +01:00 committed by GitHub
parent 3a7faa8b76
commit f1708f6366
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 157 additions and 13 deletions

View file

@ -71,11 +71,17 @@ class VectorUnifiedPushMessagingReceiver : MessagingReceiver() {
providerInfo = "${UnifiedPushConfig.NAME} - $instance",
data = String(message.content),
)
pushHandlingWakeLock.unlock()
} else {
pushHandler.handle(
val handled = pushHandler.handle(
pushData = pushData,
providerInfo = "${UnifiedPushConfig.NAME} - $instance",
)
// If we failed to handle the push, we should release the wakelock early to avoid keeping the device awake for too long.
if (!handled) {
pushHandlingWakeLock.unlock()
}
}
}
}

View file

@ -39,6 +39,7 @@ import org.unifiedpush.android.connector.FailedReason
import org.unifiedpush.android.connector.data.PublicKeySet
import org.unifiedpush.android.connector.data.PushEndpoint
import org.unifiedpush.android.connector.data.PushMessage
import kotlin.time.Duration
@RunWith(RobolectricTestRunner::class)
class VectorUnifiedPushMessagingReceiverTest {
@ -76,7 +77,7 @@ class VectorUnifiedPushMessagingReceiverTest {
@Test
fun `onMessage valid invokes the push handler`() = runTest {
val context = InstrumentationRegistry.getInstrumentation().context
val pushHandlerResult = lambdaRecorder<PushData, String, Unit> { _, _ -> }
val pushHandlerResult = lambdaRecorder<PushData, String, Boolean> { _, _ -> true }
val vectorUnifiedPushMessagingReceiver = createVectorUnifiedPushMessagingReceiver(
pushHandler = FakePushHandler(
handleResult = pushHandlerResult
@ -101,6 +102,60 @@ class VectorUnifiedPushMessagingReceiverTest {
)
}
@Test
fun `pushHandler returning true locks the wake lock but does not unlock it so it continues to run`() = runTest {
val context = InstrumentationRegistry.getInstrumentation().context
val pushHandlerResult = lambdaRecorder<PushData, String, Boolean> { _, _ -> true }
val lockLambda = lambdaRecorder<Duration, Unit> { _ -> }
val unlockLambda = lambdaRecorder<Unit> { }
val vectorUnifiedPushMessagingReceiver = createVectorUnifiedPushMessagingReceiver(
pushHandler = FakePushHandler(
handleResult = pushHandlerResult
),
pushHandlingWakeLock = FakePushHandlingWakeLock(
lock = lockLambda,
unlock = unlockLambda,
),
)
vectorUnifiedPushMessagingReceiver.onMessage(context, aPushMessage(), A_SECRET)
// The wakelock should be locked but not unlocked, so it should continue to run
lockLambda.assertions().isCalledOnce()
unlockLambda.assertions().isNeverCalled()
advanceUntilIdle()
// After waiting for any possible timeout, the lock is still locked
unlockLambda.assertions().isNeverCalled()
}
@Test
fun `pushHandler returning false locks and unlocks the wakelock early`() = runTest {
val context = InstrumentationRegistry.getInstrumentation().context
val pushHandlerResult = lambdaRecorder<PushData, String, Boolean> { _, _ -> false }
val lockLambda = lambdaRecorder<Duration, Unit> { _ -> }
val unlockLambda = lambdaRecorder<Unit> { }
val vectorUnifiedPushMessagingReceiver = createVectorUnifiedPushMessagingReceiver(
pushHandler = FakePushHandler(
handleResult = pushHandlerResult
),
pushHandlingWakeLock = FakePushHandlingWakeLock(
lock = lockLambda,
unlock = unlockLambda,
),
)
vectorUnifiedPushMessagingReceiver.onMessage(context, aPushMessage(), A_SECRET)
// The wakelock should be locked but not unlocked, so it should continue to run
lockLambda.assertions().isCalledOnce()
unlockLambda.assertions().isNeverCalled()
advanceUntilIdle()
// After waiting for a bit, the lock should have been unlocked since the handler returned false
unlockLambda.assertions().isCalledOnce()
}
@Test
fun `onMessage invalid invokes the push handler invalid method`() = runTest {
val context = InstrumentationRegistry.getInstrumentation().context