Explorar el Código

Merge pull request #456 from nightscout/bogus-notification-fix

Fix Bogus Not-Looping Notification
Sam King hace 1 año
padre
commit
515a99effa

+ 34 - 0
Trio/Sources/Application/TrioApp.swift

@@ -137,8 +137,14 @@ extension Notification.Name {
                     cleanupOldData()
                     cleanupOldData()
 
 
                     self.initState.complete = true
                     self.initState.complete = true
+
+                    // Notifications handling
+                    // Notify of completed initialization
                     Foundation.NotificationCenter.default.post(name: .initializationCompleted, object: nil)
                     Foundation.NotificationCenter.default.post(name: .initializationCompleted, object: nil)
                     UIApplication.shared.registerForRemoteNotifications()
                     UIApplication.shared.registerForRemoteNotifications()
+                    // Cancel scheduled not looping notifications when app was completely shut down and has now re-initialized completely
+                    self.clearNotLoopingNotifications()
+
                     do {
                     do {
                         try await BuildDetails.shared.handleExpireDateChange()
                         try await BuildDetails.shared.handleExpireDateChange()
                     } catch {
                     } catch {
@@ -159,6 +165,34 @@ extension Notification.Name {
         }
         }
     }
     }
 
 
+    /// Clears any legacy (Trio 0.2.x) delivered and pending notifications related to non-looping alerts.
+    /// It targets the following notifications:
+    /// - `noLoopFirstNotification`: The first notification for non-looping alerts.
+    /// - `noLoopSecondNotification`: The second notification for non-looping alerts.
+    ///
+    /// It ensures that any notifications that have already been shown to the user, as well as
+    /// any that are scheduled for the future, are removed when the system no longer needs to
+    /// alert about non-looping conditions.
+    ///
+    /// This function is typically used when the app was completely shut down and restarted,
+    /// i.e., underwent a fresh initialization and boot-up,  to avoid bogus not looping notifications
+    /// due to dangling "zombie" pending notification requests for users that update from
+    /// old Trio versions to the new generation of the app.
+    ///
+    /// Delivered notifications are cleared for completeness.
+    private func clearNotLoopingNotifications() {
+        let legacyNoLoopFirstNotification = "FreeAPS.noLoopFirstNotification"
+        let legacyNoLoopSecondNotification = "FreeAPS.noLoopSecondNotification"
+        UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [
+            legacyNoLoopFirstNotification,
+            legacyNoLoopSecondNotification
+        ])
+        UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [
+            legacyNoLoopFirstNotification,
+            legacyNoLoopSecondNotification
+        ])
+    }
+
     /// Attempts to initialize the CoreDataStack again after a previous failure.
     /// Attempts to initialize the CoreDataStack again after a previous failure.
     ///
     ///
     /// Resets error states and triggers the initialization process from the beginning. Called in response
     /// Resets error states and triggers the initialization process from the beginning. Called in response

+ 1 - 1
Trio/Sources/Services/UserNotifications/UserNotificationsManager.swift

@@ -38,7 +38,7 @@ protocol pumpNotificationObserver {
 }
 }
 
 
 final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, Injectable {
 final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, Injectable {
-    private enum Identifier: String {
+    enum Identifier: String {
         case glucoseNotification = "Trio.glucoseNotification"
         case glucoseNotification = "Trio.glucoseNotification"
         case carbsRequiredNotification = "Trio.carbsRequiredNotification"
         case carbsRequiredNotification = "Trio.carbsRequiredNotification"
         case noLoopFirstNotification = "Trio.noLoopFirstNotification"
         case noLoopFirstNotification = "Trio.noLoopFirstNotification"