From f6a1d38972d7572c5f9f9ae66ba3b0e77d871810 Mon Sep 17 00:00:00 2001 From: Doug <6060466+pixlwave@users.noreply.github.com> Date: Tue, 11 Feb 2025 09:59:46 +0000 Subject: [PATCH] Log whether a notification is expected to make a noise. (#3782) * Log whether a notification is expected to make a noise. * Also log the system notification sound setting too. --- .../Mocks/Generated/GeneratedMocks.swift | 64 +++++++++++++++++++ .../Manager/NotificationManager.swift | 3 + .../UserNotificationCenterProtocol.swift | 1 + NSE/Sources/NotificationContentBuilder.swift | 6 +- .../NotificationManagerTests.swift | 1 + 5 files changed, 74 insertions(+), 1 deletion(-) diff --git a/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift b/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift index 79c51c461..7b8bb7272 100644 --- a/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift +++ b/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift @@ -17099,6 +17099,70 @@ class UserNotificationCenterMock: UserNotificationCenterProtocol, @unchecked Sen return authorizationStatusReturnValue } } + //MARK: - notificationSettings + + var notificationSettingsUnderlyingCallsCount = 0 + var notificationSettingsCallsCount: Int { + get { + if Thread.isMainThread { + return notificationSettingsUnderlyingCallsCount + } else { + var returnValue: Int? = nil + DispatchQueue.main.sync { + returnValue = notificationSettingsUnderlyingCallsCount + } + + return returnValue! + } + } + set { + if Thread.isMainThread { + notificationSettingsUnderlyingCallsCount = newValue + } else { + DispatchQueue.main.sync { + notificationSettingsUnderlyingCallsCount = newValue + } + } + } + } + var notificationSettingsCalled: Bool { + return notificationSettingsCallsCount > 0 + } + + var notificationSettingsUnderlyingReturnValue: UNNotificationSettings! + var notificationSettingsReturnValue: UNNotificationSettings! { + get { + if Thread.isMainThread { + return notificationSettingsUnderlyingReturnValue + } else { + var returnValue: UNNotificationSettings? = nil + DispatchQueue.main.sync { + returnValue = notificationSettingsUnderlyingReturnValue + } + + return returnValue! + } + } + set { + if Thread.isMainThread { + notificationSettingsUnderlyingReturnValue = newValue + } else { + DispatchQueue.main.sync { + notificationSettingsUnderlyingReturnValue = newValue + } + } + } + } + var notificationSettingsClosure: (() async -> UNNotificationSettings)? + + func notificationSettings() async -> UNNotificationSettings { + notificationSettingsCallsCount += 1 + if let notificationSettingsClosure = notificationSettingsClosure { + return await notificationSettingsClosure() + } else { + return notificationSettingsReturnValue + } + } } class UserSessionMock: UserSessionProtocol, @unchecked Sendable { var clientProxy: ClientProxyProtocol { diff --git a/ElementX/Sources/Services/Notification/Manager/NotificationManager.swift b/ElementX/Sources/Services/Notification/Manager/NotificationManager.swift index d080f50af..aceda2063 100644 --- a/ElementX/Sources/Services/Notification/Manager/NotificationManager.swift +++ b/ElementX/Sources/Services/Notification/Manager/NotificationManager.swift @@ -94,6 +94,9 @@ final class NotificationManager: NSObject, NotificationManagerProtocol { self?.delegate?.registerForRemoteNotifications() } } + + let settings = await notificationCenter.notificationSettings() + MXLog.info("Notification sound enabled: \(settings.soundSetting == .enabled)") } } diff --git a/ElementX/Sources/Services/Notification/Manager/UserNotificationCenterProtocol.swift b/ElementX/Sources/Services/Notification/Manager/UserNotificationCenterProtocol.swift index b17502125..12e149878 100644 --- a/ElementX/Sources/Services/Notification/Manager/UserNotificationCenterProtocol.swift +++ b/ElementX/Sources/Services/Notification/Manager/UserNotificationCenterProtocol.swift @@ -16,6 +16,7 @@ protocol UserNotificationCenterProtocol: AnyObject { func removeDeliveredNotifications(withIdentifiers identifiers: [String]) func setNotificationCategories(_ categories: Set) func authorizationStatus() async -> UNAuthorizationStatus + func notificationSettings() async -> UNNotificationSettings } // sourcery: AutoMockable diff --git a/NSE/Sources/NotificationContentBuilder.swift b/NSE/Sources/NotificationContentBuilder.swift index 8c7e57a5a..cffc5424d 100644 --- a/NSE/Sources/NotificationContentBuilder.swift +++ b/NSE/Sources/NotificationContentBuilder.swift @@ -53,16 +53,20 @@ struct NotificationContentBuilder { func baseMutableContent(for notificationItem: NotificationItemProxyProtocol) -> UNMutableNotificationContent { let notification = UNMutableNotificationContent() + notification.receiverID = notificationItem.receiverID notification.roomID = notificationItem.roomID notification.eventID = switch notificationItem.event { case .timeline(let event): event.eventId() case .invite, .none: nil } - notification.sound = notificationItem.isNoisy ? UNNotificationSound(named: UNNotificationSoundName(rawValue: "message.caf")) : nil // So that the UI groups notification that are received for the same room but also for the same user // Removing the @ fixes an iOS bug where the notification crashes if the mute button is tapped notification.threadIdentifier = "\(notificationItem.receiverID)\(notificationItem.roomID)".replacingOccurrences(of: "@", with: "") + + MXLog.info("isNoisy: \(notificationItem.isNoisy)") + notification.sound = notificationItem.isNoisy ? UNNotificationSound(named: UNNotificationSoundName(rawValue: "message.caf")) : nil + return notification } diff --git a/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift b/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift index 8cfef63df..671658ac5 100644 --- a/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift +++ b/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift @@ -30,6 +30,7 @@ final class NotificationManagerTests: XCTestCase { notificationCenter = UserNotificationCenterMock() notificationCenter.requestAuthorizationOptionsReturnValue = true notificationCenter.authorizationStatusReturnValue = .authorized + notificationCenter.notificationSettingsClosure = { await UNUserNotificationCenter.current().notificationSettings() } notificationManager = NotificationManager(notificationCenter: notificationCenter, appSettings: appSettings) notificationManager.start()