From a661cceb66ae30b3a94a8ccc42e97fd1007a5b75 Mon Sep 17 00:00:00 2001 From: Andreas Bauer Date: Mon, 16 Sep 2024 18:35:08 +0200 Subject: [PATCH] Support querying pending and delivered notifications --- .../Notifications/UserNotifications.swift | 60 ++++++++++++++++++- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/Sources/Spezi/Notifications/UserNotifications.swift b/Sources/Spezi/Notifications/UserNotifications.swift index 579a479..c53edda 100644 --- a/Sources/Spezi/Notifications/UserNotifications.swift +++ b/Sources/Spezi/Notifications/UserNotifications.swift @@ -18,15 +18,22 @@ /// ### Configuration /// - ``init()`` /// +/// ### Badge Count +/// - ``setBadgeCount(_:)`` +/// /// ### Add a Notification Request -/// - ``add(isolation:request:)`` +/// - ``add(request:)`` /// /// ### Notification Limits /// - ``pendingNotificationsLimit`` -/// - ``remainingNotificationLimit(isolation:)`` +/// - ``remainingNotificationLimit()`` +/// +/// ### Fetching Notifications +/// - ``pendingNotificationRequests()`` +/// - ``deliveredNotifications()`` /// /// ### Categories -/// - ``add(isolation:categories:)`` +/// - ``add(categories:)`` public final class LocalNotifications: Module, DefaultInitializable, EnvironmentAccessible { /// The total limit of simultaneously scheduled notifications. /// @@ -36,6 +43,23 @@ public final class LocalNotifications: Module, DefaultInitializable, Environment /// Configure the local notifications module. public init() {} +#if compiler(>=6) + /// Updates the badge count for your app’s icon. + /// - Parameters: + /// - isolation: Inherits the current isolation. + /// - badgeCount: The new badge count to display. + public func setBadgeCount( // swiftlint:disable:this function_default_parameter_at_end + isolation: isolated (any Actor)? = #isolation, + _ badgeCount: Int + ) async throws { + try await UNUserNotificationCenter.current().setBadgeCount(badgeCount) + } +#else + public func setBadgeCount(_ badgeCount: Int) async throws { + try await UNUserNotificationCenter.current().setBadgeCount(badgeCount) + } +#endif + #if compiler(>=6) /// Schedule a new notification request. /// - Parameters: @@ -80,6 +104,36 @@ public final class LocalNotifications: Module, DefaultInitializable, Environment } #endif +#if compiler(>=6) + /// Fetch all notification requests that are pending delivery. + /// - Parameter isolation: Inherits the current isolation. + /// - Returns: The array of pending notifications requests. + public func pendingNotificationRequests(isolation: isolated (any Actor)? = #isolation) async -> sending [UNNotificationRequest] { + await UNUserNotificationCenter.current().pendingNotificationRequests() + } +#else + /// Fetch all notification requests that are pending delivery. + /// - Returns: The array of pending notifications requests. + public func pendingNotificationRequests() async -> [UNNotificationRequest] { + await UNUserNotificationCenter.current().pendingNotificationRequests() + } +#endif + +#if compiler(>=6) + /// Fetch all delivered notifications that are still shown in the notification center. + /// - Parameter isolation: Inherits the current isolation. + /// - Returns: The array of local and remote notifications that have been delivered and are still show in the notification center. + public func deliveredNotifications(isolation: isolated (any Actor)? = #isolation) async -> sending [UNNotification] { + await UNUserNotificationCenter.current().deliveredNotifications() + } +#else + /// Fetch all delivered notifications that are still shown in the notification center. + /// - Returns: The array of local and remote notifications that have been delivered and are still show in the notification center. + public func deliveredNotifications() async -> [UNNotification] { + await UNUserNotificationCenter.current().deliveredNotifications() + } +#endif + #if compiler(>=6) /// Add additional notification categories. ///