diff --git a/Sources/Spezi/Capabilities/Notifications/NotificationHandler.swift b/Sources/Spezi/Capabilities/Notifications/NotificationHandler.swift index e05d4db0..ad5d566b 100644 --- a/Sources/Spezi/Capabilities/Notifications/NotificationHandler.swift +++ b/Sources/Spezi/Capabilities/Notifications/NotificationHandler.swift @@ -35,7 +35,7 @@ public protocol NotificationHandler { /// /// - Parameter notification: The notification that is about to be delivered. /// - Returns: The option for notifying the user. Use `[]` to silence the notification. - func receiveIncomingNotification(_ notification: UNNotification) async -> UNNotificationPresentationOptions + func receiveIncomingNotification(_ notification: UNNotification) async -> UNNotificationPresentationOptions? #if !os(macOS) /// Handle remote notification when the app is running in background. @@ -74,9 +74,8 @@ extension NotificationHandler { #endif /// Empty default implementation. - func receiveIncomingNotification(_ notification: UNNotification) async -> UNNotificationPresentationOptions { - // TODO: is there are better default? - [.badge, .badge, .list, .sound] // default is to fully present the notification + func receiveIncomingNotification(_ notification: UNNotification) async -> UNNotificationPresentationOptions? { + nil } #if !os(macOS) diff --git a/Sources/Spezi/Spezi/SpeziNotificationCenterDelegate.swift b/Sources/Spezi/Spezi/SpeziNotificationCenterDelegate.swift index 093b9d79..6a30447c 100644 --- a/Sources/Spezi/Spezi/SpeziNotificationCenterDelegate.swift +++ b/Sources/Spezi/Spezi/SpeziNotificationCenterDelegate.swift @@ -39,17 +39,28 @@ class SpeziNotificationCenterDelegate: NSObject, UNUserNotificationCenterDelegat } - return await withTaskGroup(of: UNNotificationPresentationOptions.self) { group in + return await withTaskGroup(of: UNNotificationPresentationOptions?.self) { group in for handler in delegate.spezi.notificationHandler { group.addTask { await handler.receiveIncomingNotification(notification) } } - - // TODO: fine to just merge all options? (this doesn't work with the empty default implementation!) - return await group.reduce(into: []) { result, options in + + var hasSpecified = false + let unionOptions: UNNotificationPresentationOptions = await group.reduce(into: []) { result, options in + guard let options else { + return + } + + hasSpecified = true result.formUnion(options) } + + if hasSpecified { + return unionOptions + } else { + return [.badge, .badge, .list, .sound] + } } } } diff --git a/Tests/UITests/TestAppUITests/LifecycleHandlerTests.swift b/Tests/UITests/TestAppUITests/LifecycleHandlerTests.swift index 2a83eb38..4a131384 100644 --- a/Tests/UITests/TestAppUITests/LifecycleHandlerTests.swift +++ b/Tests/UITests/TestAppUITests/LifecycleHandlerTests.swift @@ -12,7 +12,7 @@ import XCTestExtensions final class LifecycleHandlerTests: XCTestCase { func testLifecycleHandler() throws { - #if os(macOS) || os(watchOS) + #if os(macOS) || os(watchOS) throw XCTSkip("LifecycleHandler is not supported on macOS or watchOS.") #endif