diff --git a/Kit/helpers.swift b/Kit/helpers.swift index c9e104cc..e8e5a511 100644 --- a/Kit/helpers.swift +++ b/Kit/helpers.swift @@ -479,7 +479,7 @@ public func isNewestVersion(currentVersion: String, latestVersion: String) -> Bo } @available(macOS 10.14, *) -public func showNotification(title: String, subtitle: String? = nil, userInfo: [AnyHashable: Any] = [:], delegate: UNUserNotificationCenterDelegate? = nil) { +public func showNotification(title: String, subtitle: String? = nil, userInfo: [AnyHashable: Any] = [:], delegate: UNUserNotificationCenterDelegate? = nil) -> String { let id = UUID().uuidString let content = UNMutableNotificationContent() @@ -500,9 +500,17 @@ public func showNotification(title: String, subtitle: String? = nil, userInfo: [ print(err) } } + + return id } -public func showNSNotification(title: String, subtitle: String? = nil, body: String? = nil, userInfo: [AnyHashable: Any] = [:]) { +@available(macOS 10.14, *) +public func removeNotification(_ id: String) { + let center = UNUserNotificationCenter.current() + center.removeDeliveredNotifications(withIdentifiers: [id]) +} + +public func showNSNotification(title: String, subtitle: String? = nil, body: String? = nil, userInfo: [AnyHashable: Any] = [:]) -> String { let notification = NSUserNotification() let id = UUID().uuidString @@ -514,6 +522,15 @@ public func showNSNotification(title: String, subtitle: String? = nil, body: Str notification.hasActionButton = false NSUserNotificationCenter.default.deliver(notification) + + return id +} + +public func removeNSNotification(_ id: String) { + let notificationCenter = NSUserNotificationCenter.default + if let notification = notificationCenter.deliveredNotifications.first(where: { $0.identifier == id }) { + notificationCenter.removeScheduledNotification(notification) + } } public struct TopProcess { diff --git a/Modules/Battery/main.swift b/Modules/Battery/main.swift index 7ee98383..277f92c5 100644 --- a/Modules/Battery/main.swift +++ b/Modules/Battery/main.swift @@ -50,6 +50,7 @@ public class Battery: Module { private var lowLevelNotificationState: Bool = false private var highLevelNotificationState: Bool = false + private var notificationID: String? = nil public init() { self.settingsView = Settings("Battery") @@ -97,6 +98,18 @@ public class Battery: Module { } } + public override func willTerminate() { + guard self.isAvailable() else { return } + + if let id = self.notificationID { + if #available(macOS 10.14, *) { + removeNotification(id) + } else { + removeNSNotification(id) + } + } + } + public override func isAvailable() -> Bool { let snapshot = IOPSCopyPowerSourcesInfo().takeRetainedValue() let sources = IOPSCopyPowerSourcesList(snapshot).takeRetainedValue() as Array @@ -144,6 +157,14 @@ public class Battery: Module { if (value.level > notificationLevel || value.powerSource != "Battery Power") && self.lowLevelNotificationState { if value.level > notificationLevel { + if let id = self.notificationID { + if #available(macOS 10.14, *) { + removeNotification(id) + } else { + removeNSNotification(id) + } + self.notificationID = nil + } self.lowLevelNotificationState = false } return @@ -161,12 +182,12 @@ public class Battery: Module { } if #available(macOS 10.14, *) { - showNotification( + self.notificationID = showNotification( title: title, subtitle: subtitle ) } else { - showNSNotification( + self.notificationID = showNSNotification( title: title, subtitle: subtitle ) @@ -188,6 +209,14 @@ public class Battery: Module { if (value.level < notificationLevel || value.powerSource == "Battery Power") && self.highLevelNotificationState { if value.level < notificationLevel { + if let id = self.notificationID { + if #available(macOS 10.14, *) { + removeNotification(id) + } else { + removeNSNotification(id) + } + self.notificationID = nil + } self.highLevelNotificationState = false } return @@ -205,12 +234,12 @@ public class Battery: Module { } if #available(macOS 10.14, *) { - showNotification( + self.notificationID = showNotification( title: title, subtitle: subtitle ) } else { - showNSNotification( + self.notificationID = showNSNotification( title: title, subtitle: subtitle ) diff --git a/Stats/helpers.swift b/Stats/helpers.swift index 0983d66b..c481bdfd 100644 --- a/Stats/helpers.swift +++ b/Stats/helpers.swift @@ -74,13 +74,13 @@ extension AppDelegate { let subtitle: String = localizedString("Stats was updated to v", currentVersion) if #available(macOS 10.14, *) { - showNotification( + _ = showNotification( title: title, subtitle: subtitle, delegate: self ) } else { - showNSNotification( + _ = showNSNotification( title: title, subtitle: subtitle ) @@ -193,14 +193,14 @@ extension AppDelegate { let userInfo = ["url": version.url] if #available(macOS 10.14, *) { - showNotification( + _ = showNotification( title: title, subtitle: subtitle, userInfo: userInfo, delegate: self ) } else { - showNSNotification( + _ = showNSNotification( title: title, subtitle: subtitle, userInfo: userInfo