Matthias Nott
2026-03-24 62fc4821c4a66313c300328aa1cf19808a41e0de
fix: send catch_up on app resume to fetch messages missed during background
2 files modified
changed files
lib/screens/chat_screen.dart patch | view | blame | history
lib/services/mqtt_service.dart patch | view | blame | history
lib/screens/chat_screen.dart
....@@ -164,6 +164,12 @@
164164 _sendCommand('sync', activeId != null ? {'activeSessionId': activeId} : null);
165165 // catch_up is sent after sessions arrive (in _handleSessions)
166166 };
167
+ _ws!.onResume = () {
168
+ // App came back from background with connection still alive.
169
+ // Send catch_up to fetch any messages missed during suspend.
170
+ _chatLog('onResume: sending catch_up with lastSeq=$_lastSeq');
171
+ _sendCommand('catch_up', {'lastSeq': _lastSeq});
172
+ };
167173 _ws!.onError = (error) {
168174 debugPrint('MQTT error: $error');
169175 };
lib/services/mqtt_service.dart
....@@ -56,6 +56,7 @@
5656 void Function()? onOpen;
5757 void Function()? onClose;
5858 void Function()? onReconnecting;
59
+ void Function()? onResume;
5960 void Function(String error)? onError;
6061
6162 ConnectionStatus get status => _status;
....@@ -524,10 +525,10 @@
524525 _setStatus(ConnectionStatus.reconnecting);
525526 connect();
526527 } else {
527
- // Appears connected — just let it be. The MQTT keepalive will
528
- // detect dead connections and auto-reconnect will handle it.
529
- // Don't call onOpen (it resets sessionReady and causes flicker).
530
- _mqttLog('MQTT: appears connected on resume, keeping current state');
528
+ // Appears connected — notify listener to fetch missed messages
529
+ // via catch_up. Don't call onOpen (it resets sessionReady and causes flicker).
530
+ _mqttLog('MQTT: appears connected on resume, triggering catch_up');
531
+ onResume?.call();
531532 }
532533 case AppLifecycleState.paused:
533534 break;