From 7a10ae3232877c0da58bcf16940be2a2d6caa766 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sat, 21 May 2022 11:47:42 +0100 Subject: [PATCH] fixing reading notification on other session not always being taken into account - the sync wasn't being triggered for long enough, now we're waiting for 60 seconds or until the unread change --- .../dapk/st/notifications/NotificationsModule.kt | 1 + .../dapk/st/notifications/PushAndroidService.kt | 15 ++++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationsModule.kt b/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationsModule.kt index 24a55bf..e70350b 100644 --- a/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationsModule.kt +++ b/features/notifications/src/main/kotlin/app/dapk/st/notifications/NotificationsModule.kt @@ -28,6 +28,7 @@ class NotificationsModule( fun syncService() = syncService fun credentialProvider() = credentialsStore fun firebasePushTokenUseCase() = firebasePushTokenUseCase + fun roomStore() = roomStore fun notificationsUseCase() = NotificationsUseCase( roomStore, NotificationRenderer(notificationManager(), NotificationFactory(iconLoader, context, intentFactory)), diff --git a/features/notifications/src/main/kotlin/app/dapk/st/notifications/PushAndroidService.kt b/features/notifications/src/main/kotlin/app/dapk/st/notifications/PushAndroidService.kt index 8fc6945..9f6bf55 100644 --- a/features/notifications/src/main/kotlin/app/dapk/st/notifications/PushAndroidService.kt +++ b/features/notifications/src/main/kotlin/app/dapk/st/notifications/PushAndroidService.kt @@ -56,11 +56,8 @@ class PushAndroidService : FirebaseMessagingService() { private suspend fun doSync(roomId: RoomId?, eventId: EventId?) { when (roomId) { null -> { - log(PUSH, "empty push payload - triggering a sync if not running") - withTimeoutOrNull(60_000) { - log(PUSH, "got empty event, forcing a sync") - module.syncService().startSyncing().first() - } ?: log(PUSH, "timed out waiting for sync") + log(PUSH, "empty push payload - keeping sync alive until unread changes") + waitForUnreadChange(60_000) ?: log(PUSH, "timed out waiting for sync") } else -> { log(PUSH, "push with eventId payload - keeping sync alive until the event shows up in the sync response") @@ -82,4 +79,12 @@ class PushAndroidService : FirebaseMessagingService() { } } + private suspend fun waitForUnreadChange(timeout: Long): String? { + return withTimeoutOrNull(timeout) { + combine(module.syncService().startSyncing(), module.roomStore().observeUnread()) { _, unread -> unread } + .first() + "ignored" + } + } + }