fix: recover missed messages on mobile app resume#1529
Open
kuu-26 wants to merge 1 commit intoValour-Software:mainfrom
Open
fix: recover missed messages on mobile app resume#1529kuu-26 wants to merge 1 commit intoValour-Software:mainfrom
kuu-26 wants to merge 1 commit intoValour-Software:mainfrom
Conversation
When the mobile app is reopened after being in the background, messages that arrived during the background period were not loading. This was caused by two issues: 1. Missing lifecycle hooks: iOS and MacCatalyst had no native lifecycle callbacks, so AppLifecycle.NotifyResumed() never fired on those platforms. Android only fired Resumed but not Backgrounded. 2. Race condition: OnBrowserFocused() would call RecoverMissedMessages() immediately, but the SignalR reconnection triggered by CheckConnections() is async. The recovery query could complete before the connection was re-established, returning stale data and missing messages that arrived during the reconnection gap. Fixes: - Add OnActivated/DidEnterBackground lifecycle hooks to iOS AppDelegate - Add OnActivated/DidEnterBackground lifecycle hooks to MacCatalyst AppDelegate - Add OnPause/NotifyBackground to Android MainActivity - Add Backgrounded event to AppLifecycle - Add reconnection wait logic in OnBrowserFocused() that waits up to 5 seconds for the node to finish reconnecting before recovering messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the mobile app is reopened after being in the background, messages that arrived during the background period were not loading. Users had to manually switch channels or restart the app to see new messages.
Root Cause
Two issues were identified:
1. Missing lifecycle hooks (iOS & MacCatalyst)
The
AppLifecycle.NotifyResumed()event never fired on iOS or MacCatalyst because neither platform had native lifecycle callbacks wired up. Only Android hadOnResume→NotifyResumed(). Without theResumedevent,BrowserUtils.OnRefocus()never triggered, andChatWindowComponent.OnBrowserFocused()never ran on those platforms.Android also lacked a
Backgroundednotification.2. Race condition on resume
Even on Android where the lifecycle event did fire, there was a race condition. The flow was:
AppLifecycle.NotifyResumed()→BrowserUtils.OnRefocus()→NodeService.CheckConnections()+Focused.Invoke()ChatWindowComponent.OnBrowserFocused()→RecoverMissedMessages()immediatelyCheckConnections()kicks off an async SignalR reconnection, butRecoverMissedMessages()runs immediately after without waiting for the reconnection to complete. The recovery API call could return before the server re-joins the channel, causing it to miss messages that arrived during the reconnection gap.Changes
AppDelegate: AddOnActivated→NotifyResumed()andDidEnterBackground→NotifyBackground()AppDelegate: Same lifecycle hooks as iOSMainActivity: AddOnPause→NotifyBackground()AppLifecycle: AddBackgroundedevent andNotifyBackground()methodChatWindowComponent.OnBrowserFocused(): Wait up to 5 seconds for the relevant node to finish reconnecting before callingRecoverMissedMessages(). If the reconnect takes longer, log a warning and proceed anyway.Testing
OnResume/OnPauselifecycle hooks verifiedOnActivated/DidEnterBackgroundhooks added (matching standard Xamarin/MAUI patterns)Files Changed
Valour/Client.Maui/Platforms/Android/MainActivity.csValour/Client.Maui/Platforms/iOS/AppDelegate.csValour/Client.Maui/Platforms/MacCatalyst/AppDelegate.csValour/Client/AppLifecycle.csValour/Client/Components/Windows/ChannelWindows/ChatWindowComponent.razor