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:
parent
78c9076281
commit
13bbd24df1
7 changed files with 157 additions and 13 deletions
|
|
@ -58,11 +58,17 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() {
|
|||
"$it: ${message.data[it]}"
|
||||
},
|
||||
)
|
||||
pushHandlingWakeLock.unlock()
|
||||
} else {
|
||||
pushHandler.handle(
|
||||
val handled = pushHandler.handle(
|
||||
pushData = pushData,
|
||||
providerInfo = FirebaseConfig.NAME,
|
||||
)
|
||||
|
||||
// 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ import kotlinx.coroutines.test.runTest
|
|||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import kotlin.time.Duration
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class VectorFirebaseMessagingServiceTest {
|
||||
|
|
@ -56,7 +57,7 @@ class VectorFirebaseMessagingServiceTest {
|
|||
|
||||
@Test
|
||||
fun `test receiving valid data`() = runTest {
|
||||
val lambda = lambdaRecorder<PushData, String, Unit> { _, _ -> }
|
||||
val lambda = lambdaRecorder<PushData, String, Boolean> { _, _ -> true }
|
||||
val vectorFirebaseMessagingService = createVectorFirebaseMessagingService(
|
||||
pushHandler = FakePushHandler(handleResult = lambda)
|
||||
)
|
||||
|
|
@ -78,6 +79,68 @@ class VectorFirebaseMessagingServiceTest {
|
|||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test pushHandler returning true locks and does not unlock the wakelock so it continues running`() = runTest {
|
||||
val lockLambda = lambdaRecorder<Duration, Unit> { _ -> }
|
||||
val unlockLambda = lambdaRecorder<Unit> { }
|
||||
val vectorFirebaseMessagingService = createVectorFirebaseMessagingService(
|
||||
pushHandler = FakePushHandler(handleResult = { _, _ -> true }),
|
||||
pushHandlingWakeLock = FakePushHandlingWakeLock(
|
||||
lock = lockLambda,
|
||||
unlock = unlockLambda
|
||||
)
|
||||
)
|
||||
vectorFirebaseMessagingService.onMessageReceived(
|
||||
message = RemoteMessage(
|
||||
Bundle().apply {
|
||||
putString("event_id", AN_EVENT_ID.value)
|
||||
putString("room_id", A_ROOM_ID.value)
|
||||
putString("cs", A_SECRET)
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
// The wakelock should be locked but not unlocked
|
||||
lockLambda.assertions().isCalledOnce()
|
||||
unlockLambda.assertions().isNeverCalled()
|
||||
|
||||
advanceUntilIdle()
|
||||
|
||||
// After handling the push, the wakelock should still not be unlocked
|
||||
unlockLambda.assertions().isNeverCalled()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test pushHandler returning false locks and unlocks the wakelock early`() = runTest {
|
||||
val lockLambda = lambdaRecorder<Duration, Unit> { _ -> }
|
||||
val unlockLambda = lambdaRecorder<Unit> { }
|
||||
val vectorFirebaseMessagingService = createVectorFirebaseMessagingService(
|
||||
pushHandler = FakePushHandler(handleResult = { _, _ -> false }),
|
||||
pushHandlingWakeLock = FakePushHandlingWakeLock(
|
||||
lock = lockLambda,
|
||||
unlock = unlockLambda
|
||||
)
|
||||
)
|
||||
vectorFirebaseMessagingService.onMessageReceived(
|
||||
message = RemoteMessage(
|
||||
Bundle().apply {
|
||||
putString("event_id", AN_EVENT_ID.value)
|
||||
putString("room_id", A_ROOM_ID.value)
|
||||
putString("cs", A_SECRET)
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
// The wakelock should be locked but not unlocked
|
||||
lockLambda.assertions().isCalledOnce()
|
||||
unlockLambda.assertions().isNeverCalled()
|
||||
|
||||
advanceUntilIdle()
|
||||
|
||||
// After handling the push, the wakelock should be unlocked
|
||||
unlockLambda.assertions().isCalledOnce()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test new token is forwarded to the handler`() = runTest {
|
||||
val lambda = lambdaRecorder<String, Unit> { }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue