Matthias Nott
9 days ago 34b82ffb9b31866b528cd91a3d2ec360dbb8da5e
lib/screens/chat_screen.dart
....@@ -223,10 +223,27 @@
223223 _push?.onMqttConnected();
224224 };
225225 _ws!.onResume = () {
226
- // App came back from background with connection still alive.
227
- // Send catch_up to fetch any messages missed during suspend.
226
+ // App came back from background — connection may or may not be alive.
227
+ // Try catch_up first, but if no response comes, force reconnect.
228228 _chatLog('onResume: sending catch_up with lastSeq=$_lastSeq');
229
+ final seqBefore = _lastSeq;
229230 _sendCommand('catch_up', {'lastSeq': _lastSeq});
231
+ // Force UI rebuild for any buffered messages
232
+ Future.delayed(const Duration(milliseconds: 300), () {
233
+ if (mounted) {
234
+ setState(() {});
235
+ _scrollToBottom();
236
+ }
237
+ });
238
+ // If catch_up didn't produce a response in 2s, connection is dead — reconnect
239
+ Future.delayed(const Duration(seconds: 2), () {
240
+ if (!mounted) return;
241
+ if (_lastSeq == seqBefore) {
242
+ // No new messages arrived — connection likely dead
243
+ _chatLog('onResume: no catch_up response after 2s, forcing reconnect');
244
+ _ws?.forceReconnect();
245
+ }
246
+ });
230247 };
231248 _ws!.onError = (error) {
232249 debugPrint('MQTT error: $error');
....@@ -247,7 +264,9 @@
247264 // sent immediately if already connected.
248265 _push = PushService(mqttService: _ws!);
249266 _push!.onNotificationTap = (data) {
250
- // If notification carried a sessionId, switch to that session
267
+ // Switch to the session immediately, then request catch_up.
268
+ // The MQTT connection auto-reconnects on resume, and onResume
269
+ // already sends catch_up. We just need to be on the right session.
251270 final sessionId = data['sessionId'] as String?;
252271 if (sessionId != null && mounted) {
253272 _switchSession(sessionId);