diff --git a/ElementX/Sources/Services/Notification/Manager/NotificationManager.swift b/ElementX/Sources/Services/Notification/Manager/NotificationManager.swift index 045cbbcc3..060c59460 100644 --- a/ElementX/Sources/Services/Notification/Manager/NotificationManager.swift +++ b/ElementX/Sources/Services/Notification/Manager/NotificationManager.swift @@ -98,7 +98,7 @@ final class NotificationManager: NSObject, NotificationManagerProtocol { Task { [weak self] in guard let self else { return } - if await notificationCenter.authorizationStatus() == .authorized { + if await notificationCenter.authorizationStatus() == .authorized, appSettings.enableNotifications { await MainActor.run { [weak self] in self?.delegate?.registerForRemoteNotifications() } diff --git a/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift b/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift index 12e2dc1d7..9a076686b 100644 --- a/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift +++ b/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift @@ -163,6 +163,38 @@ final class NotificationManagerTests: XCTestCase { await fulfillment(of: [expectation]) XCTAssertTrue(authorizationStatusWasGranted) } + + func test_whenStartAndAuthorizedAndNotificationDisabled_registerForRemoteNotificationsNotCalled() async throws { + appSettings.enableNotifications = false + notificationCenter.authorizationStatusReturnValue = .authorized + notificationManager.delegate = self + + notificationManager.setUserSession(MockUserSession(clientProxy: ClientProxyMock(), + mediaProvider: MockMediaProvider(), + voiceMessageMediaManager: VoiceMessageMediaManagerMock())) + try await Task.sleep(for: .seconds(1)) + + XCTAssertFalse(authorizationStatusWasGranted) + } + + func test_whenStartAndAuthorized_registerForRemoteNotificationsCalled() async throws { + appSettings.enableNotifications = true + notificationCenter.authorizationStatusReturnValue = .authorized + notificationManager.delegate = self + + let expectation: XCTestExpectation = expectation(description: "registerForRemoteNotifications delegate function should be called") + expectation.assertForOverFulfill = false + registerForRemoteNotificationsDelegateCalled = { + expectation.fulfill() + } + + notificationManager.setUserSession(MockUserSession(clientProxy: ClientProxyMock(), + mediaProvider: MockMediaProvider(), + voiceMessageMediaManager: VoiceMessageMediaManagerMock())) + await fulfillment(of: [expectation]) + + XCTAssertTrue(authorizationStatusWasGranted) + } func test_whenWillPresentNotificationsDelegateNotSet_CorrectPresentationOptionsReturned() async throws { let archiver = MockCoder(requiringSecureCoding: false) diff --git a/changelog.d/2721.bugfix b/changelog.d/2721.bugfix new file mode 100644 index 000000000..882caec48 --- /dev/null +++ b/changelog.d/2721.bugfix @@ -0,0 +1 @@ +Only register for notifications when the setting is enabled. \ No newline at end of file