From e7c5b5de5160f3d82fc9c36bc5185b5c6e091e9e Mon Sep 17 00:00:00 2001
From: Matthias Nott <mnott@mnsoft.org>
Date: Wed, 01 Apr 2026 13:06:18 +0200
Subject: [PATCH] fix: clear badge on app open, add clearBadge method

---
 ios/Runner/AppDelegate.swift   |    6 ++++++
 lib/services/push_service.dart |   16 +++++++++++++++-
 2 files changed, 21 insertions(+), 1 deletions(-)

diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift
index e437af6..194fe1c 100644
--- a/ios/Runner/AppDelegate.swift
+++ b/ios/Runner/AppDelegate.swift
@@ -15,6 +15,12 @@
     GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry)
   }
 
+  // Clear badge when app becomes active
+  override func applicationDidBecomeActive(_ application: UIApplication) {
+    super.applicationDidBecomeActive(application)
+    application.applicationIconBadgeNumber = 0
+  }
+
   // Forward APNs token registration to the push plugin
   override func application(
     _ application: UIApplication,
diff --git a/lib/services/push_service.dart b/lib/services/push_service.dart
index e9e09b6..6f9b306 100644
--- a/lib/services/push_service.dart
+++ b/lib/services/push_service.dart
@@ -1,4 +1,5 @@
 import 'package:flutter/foundation.dart';
+import 'package:flutter/services.dart';
 import 'package:push/push.dart';
 
 import 'mqtt_service.dart';
@@ -82,13 +83,26 @@
     };
   }
 
-  /// Re-send the last known token when MQTT reconnects.
+  /// Re-send the last known token when MQTT reconnects, and clear badge.
   void onMqttConnected() {
     final token = _lastToken;
     if (token != null) {
       debugPrint('[Push] re-registering token after MQTT reconnect');
       _sendTokenToDaemon(token);
     }
+    clearBadge();
+  }
+
+  /// Clear the app icon badge number.
+  static void clearBadge() {
+    try {
+      // Use UIApplication.shared.applicationIconBadgeNumber = 0 via platform channel
+      const platform = MethodChannel('com.tekmidian.pailot/badge');
+      platform.invokeMethod('clearBadge').catchError((_) {
+        // Fallback: UNUserNotificationCenter approach
+        debugPrint('[Push] clearBadge via platform channel failed, using Push API');
+      });
+    } catch (_) {}
   }
 
   /// Publish the device token to the daemon via MQTT.

--
Gitblit v1.3.1