Matthias Nott
2026-04-01 f68a986682535dca139515741dd60be26a82edd6
lib/screens/chat_screen.dart
....@@ -20,6 +20,7 @@
2020 import '../services/audio_service.dart';
2121 import '../services/message_store.dart';
2222 import '../services/mqtt_service.dart';
23
+import '../services/push_service.dart';
2324 import '../theme/app_theme.dart';
2425 import '../widgets/command_bar.dart';
2526 import '../widgets/input_bar.dart';
....@@ -51,6 +52,7 @@
5152 class _ChatScreenState extends ConsumerState<ChatScreen>
5253 with WidgetsBindingObserver {
5354 MqttService? _ws;
55
+ PushService? _push;
5456 final TextEditingController _textController = TextEditingController();
5557 final ScrollController _scrollController = ScrollController();
5658 final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
....@@ -185,6 +187,9 @@
185187 final activeId = ref.read(activeSessionIdProvider);
186188 _sendCommand('sync', activeId != null ? {'activeSessionId': activeId} : null);
187189 // catch_up is sent after sessions arrive (in _handleSessions)
190
+
191
+ // Re-register APNs token after reconnect so daemon always has a fresh token
192
+ _push?.onMqttConnected();
188193 };
189194 _ws!.onResume = () {
190195 // App came back from background with connection still alive.
....@@ -206,6 +211,19 @@
206211 );
207212
208213 await _ws!.connect();
214
+
215
+ // Initialize push notifications after MQTT is set up so token can be
216
+ // sent immediately if already connected.
217
+ _push = PushService(mqttService: _ws!);
218
+ _push!.onNotificationTap = (data) {
219
+ // If notification carried a sessionId, switch to it
220
+ final sessionId = data['sessionId'] as String?;
221
+ if (sessionId != null && mounted) {
222
+ ref.read(activeSessionIdProvider.notifier).state = sessionId;
223
+ ref.read(messagesProvider.notifier).switchSession(sessionId);
224
+ }
225
+ };
226
+ await _push!.initialize();
209227 }
210228
211229 void _handleMessage(Map<String, dynamic> msg) {