Notification Handler to handle notifications in foreground (#837)

* changed how the notifications are handled

* local notifications can be decrypted and shown!

* Improved the handling for the mock and for the normal messages

* Avatar images are also displayed

* pr ready, commented some code that is not supposed to be releaaed yet, adjusted some tests and added some source code in the targets

* fixing some swiftlint complaints and removed an unused function

* better swiftlint disable

* addressing PR comments
This commit is contained in:
Mauro
2023-04-28 14:36:16 +02:00
committed by GitHub
parent 3fb0829b88
commit ccccdce0fc
18 changed files with 550 additions and 437 deletions
@@ -14,13 +14,15 @@
// limitations under the License.
//
import Combine
import Foundation
import UIKit
import UserNotifications
class NotificationManager: NSObject, NotificationManagerProtocol {
private let notificationCenter: UserNotificationCenterProtocol
private var clientProxy: ClientProxyProtocol?
private var userSession: UserSessionProtocol?
var clientCancellable: AnyCancellable?
init(notificationCenter: UserNotificationCenterProtocol = UNUserNotificationCenter.current()) {
self.notificationCenter = notificationCenter
@@ -58,14 +60,25 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
}
func register(with deviceToken: Data) async -> Bool {
guard let clientProxy else {
guard let userSession else {
return false
}
return await setPusher(with: deviceToken, clientProxy: clientProxy)
return await setPusher(with: deviceToken, clientProxy: userSession.clientProxy)
}
func setClientProxy(_ clientProxy: ClientProxyProtocol?) {
self.clientProxy = clientProxy
func setUserSession(_ userSession: UserSessionProtocol?) {
self.userSession = userSession
clientCancellable = userSession?.clientProxy.callbacks.sink { [weak self] value in
guard let self else { return }
switch value {
case let .receivedNotification(notification):
Task {
await self.showLocalNotification(notification)
}
default:
return
}
}
}
func registrationFailed(with error: Error) { }
@@ -86,6 +99,19 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
MXLog.error("[NotificationManager] show local notification failed: \(error)")
}
}
private func showLocalNotification(_ notification: NotificationItemProxyProtocol) async {
guard let userSession else { return }
do {
guard let content = try await notification.process(receiverId: userSession.userID, roomId: notification.roomID, mediaProvider: userSession.mediaProvider) else {
return
}
let request = UNNotificationRequest(identifier: ProcessInfo.processInfo.globallyUniqueString, content: content, trigger: nil)
try await notificationCenter.add(request)
} catch {
MXLog.error("[NotificationManager] show local notification item failed: \(error)")
}
}
private func setPusher(with deviceToken: Data, clientProxy: ClientProxyProtocol) async -> Bool {
do {