mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 20:39:24 +00:00
Notify every signed-in account and turn notifications on and off per account (#6337)
* Store the notifications switch per account Adds AccountSettings.enableNotifications and moves the app-wide value into the signed in accounts before they're restored: the first account takes it out of the app settings and the others get the same switch, so someone who turned notifications off keeps them off. * Add a notification service to every user session Each account's NotificationService sets its pusher when its notifications are on and deletes it when they're off (appending with multiple accounts), keeps the account's unread count and decides which of its delivered notifications to clear. It asks the NotificationManager, the only owner of the notification center, through actions: remove the notifications matching a rule, request authorization and the unread count. The UserSessionManager connects every live session's service to the manager, and flows and screens use the session's service instead of the manager, which leaves the flow parameters. * Route notifications by account Taps only open rooms of the active account for now, a tap made while restoring only opens once its account is the active one, replies are sent from the account they were made for, and signing out only clears that account's notifications unless it was the last one. The notification extension hides pushes for an account that signed out while other accounts are still signed in. * Turn notifications on and off for each account The notification settings switch is now saved per account, and each account's NotificationService sets or deletes its pusher when it changes, asking for permission when it's turned on. * Apply review suggestions to multi-account notifications Migration - Move the notifications switch with a single per-account migration, like 26.10.1 and 26.10.2, and drop its dedicated test. Notification manager and service - Follow each account's badge count publisher directly, removing the badge count action and the stored counts. - Only reset the badge and unregister from APNs when the last account signs out, as each account already clears its own notifications. - Set up the service's subscriptions in a single setupSubscriptions(). - Rename switchChanged(to:) to enableNotificationsChanged(to:) and pusherConfiguration(identifiers:) to pusherConfiguration(for:). - Build the service without hopping to the main actor, it already runs there. Notification service extension - Check the account's switch in NotificationHandler using the session's settings, and log an error when a push arrives for an account that's turned off. - Always show pushes that match no signed-in account and log an error, so a broken setup stays visible. Other - Store a pending inline reply in a PendingInlineReplyDetails struct. - Assign the chats tab badge with weakAssign. - Make PusherConfiguration Equatable so the pusher test checks it in one expectation. - Clarify the comment about notifications without a receiver. * Print instead of logging before the app hooks run in the NSE A push that matches no signed-in account goes back to printing, as MXLog shouldn't be used before the app hooks have run. The push is still shown so a broken setup stays visible.
This commit is contained in:
@@ -64,7 +64,10 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
private let appRouteURLParser: AppRouteURLParser
|
||||
|
||||
private var storedAppRoute: AppRoute?
|
||||
@Consumable private var storedInlineReply: (roomID: String, message: String)?
|
||||
/// The account of a notification tapped while restoring, its route only opens once that account is the active one.
|
||||
@Consumable private var storedAppRouteReceiverID: String?
|
||||
/// A reply made from a notification before its account was live.
|
||||
private var storedInlineReply: PendingInlineReplyDetails?
|
||||
@Consumable private var storedRoomsToAwait: Set<String>?
|
||||
|
||||
init(appDelegate: AppDelegate) {
|
||||
@@ -125,7 +128,8 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
analyticsService: analyticsService,
|
||||
appHooks: appHooks,
|
||||
networkMonitor: networkMonitor)
|
||||
userSessionManager = UserSessionManager(userSessionStore: userSessionStore, appSettings: appSettings)
|
||||
notificationManager = NotificationManager(notificationCenter: UNUserNotificationCenter.current(), appSettings: appSettings)
|
||||
userSessionManager = UserSessionManager(userSessionStore: userSessionStore, appSettings: appSettings, notificationManager: notificationManager)
|
||||
|
||||
let appLockService = AppLockService(keychainController: keychainController, appSettings: appSettings)
|
||||
let appLockNavigationCoordinator = NavigationRootCoordinator()
|
||||
@@ -133,9 +137,6 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
navigationCoordinator: appLockNavigationCoordinator,
|
||||
appSettings: appSettings)
|
||||
|
||||
notificationManager = NotificationManager(notificationCenter: UNUserNotificationCenter.current(),
|
||||
appSettings: appSettings)
|
||||
|
||||
bugReportService = BugReportService(rageshakeURLPublisher: appSettings.bugReportRageshakeURL.publisher,
|
||||
applicationID: appSettings.bugReportApplicationID,
|
||||
sdkGitSHA: sdkGitSha(),
|
||||
@@ -147,8 +148,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
|
||||
windowManager.delegate = self
|
||||
|
||||
notificationManager.delegate = self
|
||||
notificationManager.start()
|
||||
configureNotificationManager()
|
||||
|
||||
guard let currentVersion = Version(InfoPlistReader(bundle: .main).bundleShortVersionString) else {
|
||||
fatalError("The app's version number **must** use semver for migration purposes.")
|
||||
@@ -407,6 +407,10 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
// there is not a user session yet
|
||||
return false
|
||||
}
|
||||
// Only the active account's rooms can be on screen.
|
||||
if let receiverID = content.receiverID, receiverID != userSession?.clientProxy.userID {
|
||||
return true
|
||||
}
|
||||
return !userSessionFlowCoordinator.isDisplayingRoomScreen(withRoomID: roomID)
|
||||
}
|
||||
|
||||
@@ -414,10 +418,20 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
MXLog.info("Tapped Notification")
|
||||
|
||||
guard let roomID = content.roomID,
|
||||
content.receiverID != nil else {
|
||||
let receiverID = content.receiverID else {
|
||||
return
|
||||
}
|
||||
|
||||
if let userSession {
|
||||
guard receiverID == userSession.clientProxy.userID else {
|
||||
// TODO: Switch to the receiver's account and open the room (element-hq/element-meta#3328).
|
||||
MXLog.info("Ignoring a notification tap for \(receiverID), it isn't the active account.")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
storedAppRouteReceiverID = receiverID
|
||||
}
|
||||
|
||||
let eventID = appSettings.focusEventOnNotificationTap ? content.eventID : nil
|
||||
if content.categoryIdentifier == NotificationConstants.Category.invite {
|
||||
if let userSession {
|
||||
@@ -440,17 +454,18 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
func handleInlineReply(_ service: NotificationManagerProtocol, content: UNNotificationContent, replyText: String) async {
|
||||
MXLog.info("Handle notification reply")
|
||||
|
||||
guard let roomID = content.roomID else {
|
||||
guard let roomID = content.roomID,
|
||||
let receiverID = content.receiverID else {
|
||||
return
|
||||
}
|
||||
|
||||
if userSession == nil {
|
||||
// Store the data so it can be used after the session is established
|
||||
storedInlineReply = (roomID, replyText)
|
||||
guard let userSession = userSessionManager.session(for: receiverID) else {
|
||||
// Store the data so it can be sent once the receiver's session is live.
|
||||
storedInlineReply = PendingInlineReplyDetails(userID: receiverID, roomID: roomID, message: replyText)
|
||||
return
|
||||
}
|
||||
|
||||
await processInlineReply(roomID: roomID, replyText: replyText)
|
||||
await processInlineReply(replyText, roomID: roomID, userSession: userSession)
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
@@ -559,6 +574,11 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if oldVersion < Version(26, 10, 3) {
|
||||
MXLog.info("Migrating to version 26.10.3, moving the notifications switch into the account.")
|
||||
userSession.userSettings.account.migrateAppSettingsValue(\.enableNotificationsKey)
|
||||
}
|
||||
}
|
||||
|
||||
/// This could be removed once the adoption of 25.06.x is widespread.
|
||||
@@ -715,8 +735,9 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
|
||||
await userSessionManager.restoreSessions { [weak self] userSession in
|
||||
await self?.performUserSessionMigrations(userSession)
|
||||
} didAdd: { [weak self] _ in
|
||||
} didAdd: { [weak self] userSession in
|
||||
self?.showActiveSessionIfRestoring()
|
||||
self?.sendStoredInlineReply(from: userSession)
|
||||
}
|
||||
|
||||
guard !Task.isCancelled else { return }
|
||||
@@ -738,6 +759,16 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
stateMachine.processEvent(.createdUserSession)
|
||||
}
|
||||
|
||||
/// Sends a reply made from a notification before its account was live.
|
||||
private func sendStoredInlineReply(from userSession: UserSessionProtocol) {
|
||||
guard let storedInlineReply, storedInlineReply.userID == userSession.clientProxy.userID else { return }
|
||||
self.storedInlineReply = nil
|
||||
|
||||
Task {
|
||||
await processInlineReply(storedInlineReply.message, roomID: storedInlineReply.roomID, userSession: userSession)
|
||||
}
|
||||
}
|
||||
|
||||
private func startAuthentication() {
|
||||
let encryptionKeyProvider = EncryptionKeyProvider()
|
||||
let classicAppManager = ClassicAppManager()
|
||||
@@ -771,6 +802,13 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
fatalError("User session not setup")
|
||||
}
|
||||
|
||||
if let storedAppRouteReceiverID, storedAppRouteReceiverID != userSession.clientProxy.userID {
|
||||
// TODO: Switch to the receiver's account and open the room (element-hq/element-meta#3328).
|
||||
MXLog.info("Dropping a notification tap for \(storedAppRouteReceiverID), it isn't the active account.")
|
||||
storedAppRoute = nil
|
||||
storedRoomsToAwait = nil
|
||||
}
|
||||
|
||||
if let storedRoomsToAwait {
|
||||
userSession.clientProxy.roomsToAwait = storedRoomsToAwait
|
||||
}
|
||||
@@ -779,10 +817,6 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
let storedAppRoute = storedAppRoute.take() {
|
||||
userSessionFlowCoordinator.handleAppRoute(storedAppRoute, animated: false)
|
||||
}
|
||||
|
||||
if let storedInlineReply {
|
||||
await processInlineReply(roomID: storedInlineReply.roomID, replyText: storedInlineReply.message)
|
||||
}
|
||||
}
|
||||
|
||||
private func startAuthenticationSoftLogout() {
|
||||
@@ -858,7 +892,6 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
appHooks: appHooks,
|
||||
analytics: analyticsService,
|
||||
userIndicatorController: userIndicatorController,
|
||||
notificationManager: notificationManager,
|
||||
stateMachineFactory: StateMachineFactory())
|
||||
|
||||
let userSessionFlowCoordinator = UserSessionFlowCoordinator(isNewLogin: isNewLogin,
|
||||
@@ -913,14 +946,10 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
return
|
||||
}
|
||||
|
||||
// The user will log out, clear any existing notifications and unregister from receving new ones
|
||||
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
|
||||
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
|
||||
UNUserNotificationCenter.current().setBadgeCount(0)
|
||||
|
||||
unregisterForRemoteNotifications()
|
||||
|
||||
Task {
|
||||
// Delete the pusher while the session can still reach the server.
|
||||
await userSession.notificationService.tearDown()
|
||||
|
||||
// First log out from the server
|
||||
await userSession.clientProxy.logout()
|
||||
|
||||
@@ -928,6 +957,11 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
userSessionManager.remove(userID: userSession.clientProxy.userID)
|
||||
tearDownUserSession()
|
||||
|
||||
// Signed in accounts that aren't live yet still count.
|
||||
if userSessionManager.userIDs.isEmpty {
|
||||
notificationManager.reset()
|
||||
}
|
||||
|
||||
userSession.userSettings.account.reset()
|
||||
appHooks.remoteSettingsHook.reset(appSettings)
|
||||
|
||||
@@ -945,8 +979,6 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
userIndicatorController.retractAllIndicators()
|
||||
|
||||
userSessionFlowCoordinator = nil
|
||||
|
||||
notificationManager.setUserSession(nil)
|
||||
}
|
||||
|
||||
private func presentSplashScreen(isSoftLogout: Bool = false, disableAppLock: Bool = false) {
|
||||
@@ -976,15 +1008,14 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
|
||||
// TODO: Calls only follow the active account, ElementCallService must change to hold every account's session.
|
||||
elementCallService.setUserSession(userSession)
|
||||
// TODO: Notifications only follow the active account, the NotificationManager must change to handle every account.
|
||||
configureNotificationManager(for: userSession)
|
||||
Task {
|
||||
await resumeClientServices()
|
||||
}
|
||||
}
|
||||
|
||||
private func configureNotificationManager(for userSession: UserSessionProtocol) {
|
||||
notificationManager.setUserSession(userSession)
|
||||
private func configureNotificationManager() {
|
||||
notificationManager.delegate = self
|
||||
notificationManager.start()
|
||||
|
||||
appDelegateObserver = appDelegate.callbacks
|
||||
.receive(on: DispatchQueue.main)
|
||||
@@ -1142,11 +1173,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
|
||||
MXLog.info("Sentry configured (enabled: \(options.enabled))")
|
||||
}
|
||||
|
||||
private func processInlineReply(roomID: String, replyText: String) async {
|
||||
guard let userSession else {
|
||||
fatalError("User session not setup")
|
||||
}
|
||||
|
||||
private func processInlineReply(_ replyText: String, roomID: String, userSession: UserSessionProtocol) async {
|
||||
guard case let .joined(roomProxy) = await userSession.clientProxy.roomForIdentifier(roomID) else {
|
||||
MXLog.error("Tried to reply in an unjoined room: \(roomID)")
|
||||
return
|
||||
@@ -1486,3 +1513,9 @@ private extension AppCoordinator {
|
||||
task.setTaskCompleted(success: true)
|
||||
}
|
||||
}
|
||||
|
||||
private struct PendingInlineReplyDetails {
|
||||
let userID: String
|
||||
let roomID: String
|
||||
let message: String
|
||||
}
|
||||
|
||||
@@ -37,7 +37,6 @@ struct CommonFlowParameters {
|
||||
let appHooks: AppHooks
|
||||
let analytics: AnalyticsServiceProtocol
|
||||
let userIndicatorController: UserIndicatorControllerProtocol
|
||||
let notificationManager: NotificationManagerProtocol
|
||||
let stateMachineFactory: StateMachineFactoryProtocol
|
||||
|
||||
var windowManager: WindowManagerProtocol {
|
||||
|
||||
@@ -32,6 +32,10 @@ final nonisolated class AccountSettings: @unchecked Sendable {
|
||||
@UserPreference(defaultValue: true)
|
||||
var showAllRoomListActivity: Bool
|
||||
|
||||
/// Whether this account's notifications are on. When off, its pusher is deleted from the homeserver.
|
||||
@UserPreference(defaultValue: true)
|
||||
var enableNotifications: Bool
|
||||
|
||||
/// The tone played when the user hasn't selected one, which the homeserver can configure remotely.
|
||||
let defaultNotificationTone: RemotePreference<NotificationTone> = .init(.default)
|
||||
|
||||
|
||||
@@ -232,9 +232,6 @@ final nonisolated class AppSettings: @unchecked Sendable {
|
||||
pushGatewayBaseURL.appending(path: "_matrix/push/v1/notify")
|
||||
}
|
||||
|
||||
@UserPreference(defaultValue: true)
|
||||
var enableNotifications: Bool
|
||||
|
||||
@UserPreference(defaultValue: false)
|
||||
var hideQuietNotificationAlerts: Bool
|
||||
|
||||
|
||||
Reference in New Issue
Block a user