From 0af9986262e53b232731408ad38e9fda3da2cfa2 Mon Sep 17 00:00:00 2001
From: Matthias Nott <mnott@mnsoft.org>
Date: Wed, 01 Apr 2026 18:02:59 +0200
Subject: [PATCH] feat: smart badge counting, persisted unreads, flutter_app_badger, race condition fixes

---
 ios/Runner/AppDelegate.swift |   17 ++++++++++-------
 1 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift
index 046a9dd..c719ba2 100644
--- a/ios/Runner/AppDelegate.swift
+++ b/ios/Runner/AppDelegate.swift
@@ -8,8 +8,6 @@
     _ application: UIApplication,
     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
   ) -> Bool {
-    application.applicationIconBadgeNumber = 0
-    if #available(iOS 16.0, *) { UNUserNotificationCenter.current().setBadgeCount(0) }
     return super.application(application, didFinishLaunchingWithOptions: launchOptions)
   }
 
@@ -17,13 +15,18 @@
     GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry)
   }
 
-  // Clear badge when app becomes active
-  override func applicationDidBecomeActive(_ application: UIApplication) {
-    super.applicationDidBecomeActive(application)
-    application.applicationIconBadgeNumber = 0
-    if #available(iOS 16.0, *) { UNUserNotificationCenter.current().setBadgeCount(0) }
+  // Read badge count from Flutter's SharedPreferences (UserDefaults) and update icon
+  private func updateBadgeFromPrefs() {
+    // Flutter SharedPreferences stores ints with "flutter." prefix
+    let prefs = UserDefaults.standard
+    let count = prefs.integer(forKey: "flutter.badgeCount")
+    UIApplication.shared.applicationIconBadgeNumber = count
   }
 
+  // Don't touch badge on resume — APNs sets it, Flutter decrements it on session view
+
+  // Badge handled by Flutter via platform channel on session switch and background
+
   // Forward APNs token registration to the push plugin
   override func application(
     _ application: UIApplication,

--
Gitblit v1.3.1