From f68a986682535dca139515741dd60be26a82edd6 Mon Sep 17 00:00:00 2001
From: Matthias Nott <mnott@mnsoft.org>
Date: Wed, 01 Apr 2026 12:28:15 +0200
Subject: [PATCH] feat: add APNs push notification support

---
 lib/screens/chat_screen.dart |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart
index 5017f31..63fc4cd 100644
--- a/lib/screens/chat_screen.dart
+++ b/lib/screens/chat_screen.dart
@@ -20,6 +20,7 @@
 import '../services/audio_service.dart';
 import '../services/message_store.dart';
 import '../services/mqtt_service.dart';
+import '../services/push_service.dart';
 import '../theme/app_theme.dart';
 import '../widgets/command_bar.dart';
 import '../widgets/input_bar.dart';
@@ -51,6 +52,7 @@
 class _ChatScreenState extends ConsumerState<ChatScreen>
     with WidgetsBindingObserver {
   MqttService? _ws;
+  PushService? _push;
   final TextEditingController _textController = TextEditingController();
   final ScrollController _scrollController = ScrollController();
   final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@@ -185,6 +187,9 @@
       final activeId = ref.read(activeSessionIdProvider);
       _sendCommand('sync', activeId != null ? {'activeSessionId': activeId} : null);
       // catch_up is sent after sessions arrive (in _handleSessions)
+
+      // Re-register APNs token after reconnect so daemon always has a fresh token
+      _push?.onMqttConnected();
     };
     _ws!.onResume = () {
       // App came back from background with connection still alive.
@@ -206,6 +211,19 @@
     );
 
     await _ws!.connect();
+
+    // Initialize push notifications after MQTT is set up so token can be
+    // sent immediately if already connected.
+    _push = PushService(mqttService: _ws!);
+    _push!.onNotificationTap = (data) {
+      // If notification carried a sessionId, switch to it
+      final sessionId = data['sessionId'] as String?;
+      if (sessionId != null && mounted) {
+        ref.read(activeSessionIdProvider.notifier).state = sessionId;
+        ref.read(messagesProvider.notifier).switchSession(sessionId);
+      }
+    };
+    await _push!.initialize();
   }
 
   void _handleMessage(Map<String, dynamic> msg) {

--
Gitblit v1.3.1