mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
New notifications section in settings (#1273)
This commit is contained in:
@@ -24,7 +24,9 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
|
||||
private let appSettings: AppSettings
|
||||
|
||||
private var userSession: UserSessionProtocol?
|
||||
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
private var notificationsEnabled = false
|
||||
|
||||
init(notificationCenter: UserNotificationCenterProtocol,
|
||||
appSettings: AppSettings) {
|
||||
self.notificationCenter = notificationCenter
|
||||
@@ -50,15 +52,35 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
|
||||
options: [])
|
||||
notificationCenter.setNotificationCategories([messageCategory, inviteCategory])
|
||||
notificationCenter.delegate = self
|
||||
|
||||
notificationsEnabled = appSettings.enableNotifications
|
||||
MXLog.info("[NotificationManager] app setting 'enableNotifications' is '\(notificationsEnabled)'")
|
||||
|
||||
// Listen for changes to AppSettings.enableNotifications
|
||||
appSettings.$enableNotifications
|
||||
.sink { [weak self] newValue in
|
||||
self?.enableNotifications(newValue)
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
// Request authorization uppon UIApplication.didBecomeActiveNotification notification
|
||||
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
|
||||
.sink { [weak self] _ in
|
||||
self?.requestAuthorization()
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
|
||||
func requestAuthorization() {
|
||||
guard appSettings.enableNotifications, !userSession.isNil else { return }
|
||||
Task {
|
||||
do {
|
||||
let granted = try await notificationCenter.requestAuthorization(options: [.alert, .sound, .badge])
|
||||
MXLog.info("[NotificationManager] permission granted: \(granted)")
|
||||
let permissionGranted = try await notificationCenter.requestAuthorization(options: [.alert, .sound, .badge])
|
||||
MXLog.info("[NotificationManager] permission granted: \(permissionGranted)")
|
||||
await MainActor.run {
|
||||
self.delegate?.authorizationStatusUpdated(self, granted: granted)
|
||||
if permissionGranted {
|
||||
self.delegate?.registerForRemoteNotifications()
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
MXLog.error("[NotificationManager] request authorization failed: \(error)")
|
||||
@@ -136,6 +158,17 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
|
||||
appSettings.pusherProfileTag = newTag
|
||||
return newTag
|
||||
}
|
||||
|
||||
private func enableNotifications(_ enable: Bool) {
|
||||
guard notificationsEnabled != enable else { return }
|
||||
notificationsEnabled = enable
|
||||
MXLog.info("[NotificationManager] app setting 'enableNotifications' changed to '\(enable)'")
|
||||
if enable {
|
||||
requestAuthorization()
|
||||
} else {
|
||||
delegate?.unregisterForRemoteNotifications()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - UNUserNotificationCenterDelegate
|
||||
|
||||
Reference in New Issue
Block a user