mirror of
https://github.com/element-hq/element-x-ios.git
synced 2025-03-10 21:39:12 +00:00
Remote Push Notifications can now be decrypted (#854)
* refactored the NSE to use the client function * removed unused imports in the target.yml * some code improvements * changelog * code improvement * code improvement * more code improvements * separated the client and the media provider in a dedicated NSEUserSession * Update ElementX/Sources/Services/Client/ClientProxy.swift Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com> * pr suggestions * logging the error --------- Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com>
This commit is contained in:
parent
b17c987fd0
commit
f679c0ca2a
@ -105,7 +105,8 @@ class ClientProxy: ClientProxyProtocol {
|
|||||||
|
|
||||||
let delegate = WeakClientProxyWrapper(clientProxy: self)
|
let delegate = WeakClientProxyWrapper(clientProxy: self)
|
||||||
client.setDelegate(delegate: delegate)
|
client.setDelegate(delegate: delegate)
|
||||||
// Uncomment to test local notifications
|
|
||||||
|
// Set up sync listener for generating local notifications.
|
||||||
await Task.dispatch(on: clientQueue) {
|
await Task.dispatch(on: clientQueue) {
|
||||||
client.setNotificationDelegate(notificationDelegate: delegate)
|
client.setNotificationDelegate(notificationDelegate: delegate)
|
||||||
}
|
}
|
||||||
|
@ -104,11 +104,13 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
|
|||||||
guard let userSession,
|
guard let userSession,
|
||||||
notification.event.timestamp > ServiceLocator.shared.settings.lastLaunchDate else { return }
|
notification.event.timestamp > ServiceLocator.shared.settings.lastLaunchDate else { return }
|
||||||
do {
|
do {
|
||||||
guard let content = try await notification.process(mediaProvider: userSession.mediaProvider),
|
guard let identifier = notification.id else {
|
||||||
let identifier = notification.id else {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let content = try await notification.process(mediaProvider: userSession.mediaProvider)
|
||||||
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: nil)
|
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: nil)
|
||||||
|
|
||||||
guard !ServiceLocator.shared.settings.servedNotificationIdentifiers.contains(identifier) else {
|
guard !ServiceLocator.shared.settings.servedNotificationIdentifiers.contains(identifier) else {
|
||||||
MXLog.info("NotificationManager] local notification discarded because it has already been served")
|
MXLog.info("NotificationManager] local notification discarded because it has already been served")
|
||||||
return
|
return
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
//
|
|
||||||
// Copyright 2022 New Vector Ltd
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
class MockNotificationServiceProxy: NotificationServiceProxyProtocol {
|
|
||||||
func notificationItem(roomId: String, eventId: String) async throws -> NotificationItemProxyProtocol? {
|
|
||||||
nil
|
|
||||||
}
|
|
||||||
}
|
|
@ -102,10 +102,7 @@ struct NotificationItemProxy: NotificationItemProxyProtocol {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The mock and the protocol are just temporary until we can handle
|
struct EmptyNotificationItemProxy: NotificationItemProxyProtocol {
|
||||||
// and decrypt notifications both in background and in foreground
|
|
||||||
// but they should not be necessary in the future
|
|
||||||
struct MockNotificationItemProxy: NotificationItemProxyProtocol {
|
|
||||||
let eventID: String
|
let eventID: String
|
||||||
|
|
||||||
var event: TimelineEventProxyProtocol {
|
var event: TimelineEventProxyProtocol {
|
||||||
@ -167,13 +164,13 @@ extension NotificationItemProxyProtocol {
|
|||||||
/// - roomId: Room identifier
|
/// - roomId: Room identifier
|
||||||
/// - mediaProvider: Media provider to process also media. May be passed nil to ignore media operations.
|
/// - mediaProvider: Media provider to process also media. May be passed nil to ignore media operations.
|
||||||
/// - Returns: A notification content object if the notification should be displayed. Otherwise nil.
|
/// - Returns: A notification content object if the notification should be displayed. Otherwise nil.
|
||||||
func process(mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent? {
|
func process(mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
|
||||||
if self is MockNotificationItemProxy {
|
if self is EmptyNotificationItemProxy {
|
||||||
return processMock()
|
return processEmpty()
|
||||||
} else {
|
} else {
|
||||||
switch event.type {
|
switch event.type {
|
||||||
case .none, .state:
|
case .none, .state:
|
||||||
return nil
|
return processEmpty()
|
||||||
case let .messageLike(content):
|
case let .messageLike(content):
|
||||||
switch content {
|
switch content {
|
||||||
case .roomMessage(messageType: let messageType):
|
case .roomMessage(messageType: let messageType):
|
||||||
@ -194,7 +191,7 @@ extension NotificationItemProxyProtocol {
|
|||||||
return try await processText(content: content, mediaProvider: mediaProvider)
|
return try await processText(content: content, mediaProvider: mediaProvider)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return nil
|
return processEmpty()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -204,8 +201,7 @@ extension NotificationItemProxyProtocol {
|
|||||||
|
|
||||||
// MARK: - Private
|
// MARK: - Private
|
||||||
|
|
||||||
// To be removed once we don't need the mock anymore
|
private func processEmpty() -> UNMutableNotificationContent {
|
||||||
private func processMock() -> UNMutableNotificationContent {
|
|
||||||
let notification = UNMutableNotificationContent()
|
let notification = UNMutableNotificationContent()
|
||||||
notification.receiverID = receiverID
|
notification.receiverID = receiverID
|
||||||
notification.roomID = roomID
|
notification.roomID = roomID
|
||||||
|
@ -1,33 +0,0 @@
|
|||||||
//
|
|
||||||
// Copyright 2022 New Vector Ltd
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
import MatrixRustSDK
|
|
||||||
|
|
||||||
class NotificationServiceProxy: NotificationServiceProxyProtocol {
|
|
||||||
private let userID: String
|
|
||||||
// private let service: NotificationServiceProtocol
|
|
||||||
|
|
||||||
init(basePath: String,
|
|
||||||
userID: String) {
|
|
||||||
self.userID = userID
|
|
||||||
// service = NotificationService(basePath: basePath, userId: userId)
|
|
||||||
}
|
|
||||||
|
|
||||||
func notificationItem(roomId: String, eventId: String) async throws -> NotificationItemProxyProtocol? {
|
|
||||||
MockNotificationItemProxy(eventID: eventId, roomID: roomId, receiverID: userID)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
//
|
|
||||||
// Copyright 2022 New Vector Ltd
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
protocol NotificationServiceProxyProtocol {
|
|
||||||
func notificationItem(roomId: String, eventId: String) async throws -> NotificationItemProxyProtocol?
|
|
||||||
}
|
|
@ -22,8 +22,8 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
|
|||||||
private let settings = NSESettings()
|
private let settings = NSESettings()
|
||||||
private lazy var keychainController = KeychainController(service: .sessions,
|
private lazy var keychainController = KeychainController(service: .sessions,
|
||||||
accessGroup: InfoPlistReader.main.keychainAccessGroupIdentifier)
|
accessGroup: InfoPlistReader.main.keychainAccessGroupIdentifier)
|
||||||
var handler: ((UNNotificationContent) -> Void)?
|
private var handler: ((UNNotificationContent) -> Void)?
|
||||||
var modifiedContent: UNMutableNotificationContent?
|
private var modifiedContent: UNMutableNotificationContent?
|
||||||
|
|
||||||
override func didReceive(_ request: UNNotificationRequest,
|
override func didReceive(_ request: UNNotificationRequest,
|
||||||
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
|
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
|
||||||
@ -51,9 +51,9 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
|
|||||||
MXLog.info("\(tag) Payload came: \(request.content.userInfo)")
|
MXLog.info("\(tag) Payload came: \(request.content.userInfo)")
|
||||||
|
|
||||||
Task {
|
Task {
|
||||||
try await run(with: credentials,
|
await run(with: credentials,
|
||||||
roomId: roomId,
|
roomId: roomId,
|
||||||
eventId: eventId)
|
eventId: eventId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,80 +66,51 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
|
|||||||
|
|
||||||
private func run(with credentials: KeychainCredentials,
|
private func run(with credentials: KeychainCredentials,
|
||||||
roomId: String,
|
roomId: String,
|
||||||
eventId: String) async throws {
|
eventId: String) async {
|
||||||
MXLog.info("\(tag) run with roomId: \(roomId), eventId: \(eventId)")
|
MXLog.info("\(tag) run with roomId: \(roomId), eventId: \(eventId)")
|
||||||
|
|
||||||
let service = NotificationServiceProxy(basePath: URL.sessionsBaseDirectory.path,
|
do {
|
||||||
userID: credentials.userID)
|
let userSession = try NSEUserSession(credentials: credentials)
|
||||||
|
|
||||||
|
let itemProxy = try await userSession.notificationItemProxy(roomID: roomId, eventID: eventId)
|
||||||
|
|
||||||
guard let itemProxy = try await service.notificationItem(roomId: roomId,
|
// After the first processing, update the modified content
|
||||||
eventId: eventId) else {
|
modifiedContent = try await itemProxy.process(mediaProvider: nil)
|
||||||
MXLog.error("\(tag) got no notification item")
|
|
||||||
|
|
||||||
// Notification should be discarded
|
guard itemProxy.requiresMediaProvider else {
|
||||||
return discard()
|
MXLog.info("\(tag) no media needed")
|
||||||
}
|
|
||||||
|
|
||||||
// First process without a media proxy.
|
// We've processed the item and no media operations needed, so no need to go further
|
||||||
// After this some properties of the notification should be set, like title, subtitle, sound etc.
|
return notify()
|
||||||
guard let firstContent = try await itemProxy.process(mediaProvider: nil) else {
|
}
|
||||||
MXLog.error("\(tag) not even first content")
|
|
||||||
|
|
||||||
// Notification should be discarded
|
MXLog.info("\(tag) process with media")
|
||||||
return discard()
|
|
||||||
}
|
|
||||||
|
|
||||||
// After the first processing, update the modified content
|
// There is some media to load, process it again
|
||||||
modifiedContent = firstContent
|
if let latestContent = try? await itemProxy.process(mediaProvider: userSession.mediaProvider) {
|
||||||
|
// Processing finished, hopefully with some media
|
||||||
guard itemProxy.requiresMediaProvider else {
|
modifiedContent = latestContent
|
||||||
MXLog.info("\(tag) no media needed")
|
}
|
||||||
|
// We still notify, but without the media attachment if it fails to load
|
||||||
// We've processed the item and no media operations needed, so no need to go further
|
|
||||||
return notify()
|
return notify()
|
||||||
}
|
} catch {
|
||||||
|
MXLog.error("NSE run error: \(error)")
|
||||||
MXLog.info("\(tag) process with media")
|
|
||||||
|
|
||||||
// There is some media to load, process it again
|
|
||||||
if let latestContent = try await itemProxy.process(mediaProvider: createMediaProvider(with: credentials)) {
|
|
||||||
// Processing finished, hopefully with some media
|
|
||||||
modifiedContent = latestContent
|
|
||||||
return notify()
|
|
||||||
} else {
|
|
||||||
// This is not very likely, as it should discard the notification sooner
|
|
||||||
return discard()
|
return discard()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func createMediaProvider(with credentials: KeychainCredentials) throws -> MediaProviderProtocol {
|
|
||||||
let builder = ClientBuilder()
|
|
||||||
.basePath(path: URL.sessionsBaseDirectory.path)
|
|
||||||
.username(username: credentials.userID)
|
|
||||||
|
|
||||||
let client = try builder.build()
|
|
||||||
try client.restoreSession(session: credentials.restorationToken.session)
|
|
||||||
|
|
||||||
MXLog.info("\(tag) creating media provider")
|
|
||||||
|
|
||||||
return MediaProvider(mediaLoader: MediaLoader(client: client),
|
|
||||||
imageCache: .onlyOnDisk,
|
|
||||||
backgroundTaskService: nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func notify() {
|
private func notify() {
|
||||||
MXLog.info("\(tag) notify")
|
MXLog.info("\(tag) notify")
|
||||||
|
|
||||||
guard let modifiedContent else {
|
guard let modifiedContent else {
|
||||||
MXLog.info("\(tag) notify: no modified content")
|
MXLog.info("\(tag) notify: no modified content")
|
||||||
return
|
return discard()
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let identifier = modifiedContent.notificationID,
|
guard let identifier = modifiedContent.notificationID,
|
||||||
!settings.servedNotificationIdentifiers.contains(identifier) else {
|
!settings.servedNotificationIdentifiers.contains(identifier) else {
|
||||||
MXLog.info("\(tag) notify: notification already served")
|
MXLog.info("\(tag) notify: notification already served")
|
||||||
discard()
|
return discard()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
settings.servedNotificationIdentifiers.insert(identifier)
|
settings.servedNotificationIdentifiers.insert(identifier)
|
||||||
|
47
NSE/Sources/Other/NSEUserSession.swift
Normal file
47
NSE/Sources/Other/NSEUserSession.swift
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
//
|
||||||
|
// Copyright 2023 New Vector Ltd
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import MatrixRustSDK
|
||||||
|
|
||||||
|
final class NSEUserSession {
|
||||||
|
private let client: ClientProtocol
|
||||||
|
private(set) lazy var mediaProvider: MediaProviderProtocol = MediaProvider(mediaLoader: MediaLoader(client: client),
|
||||||
|
imageCache: .onlyOnDisk,
|
||||||
|
backgroundTaskService: nil)
|
||||||
|
|
||||||
|
init(credentials: KeychainCredentials) throws {
|
||||||
|
let builder = ClientBuilder()
|
||||||
|
.basePath(path: URL.sessionsBaseDirectory.path)
|
||||||
|
.username(username: credentials.userID)
|
||||||
|
|
||||||
|
client = try builder.build()
|
||||||
|
try client.restoreSession(session: credentials.restorationToken.session)
|
||||||
|
}
|
||||||
|
|
||||||
|
func notificationItemProxy(roomID: String, eventID: String) async throws -> NotificationItemProxyProtocol {
|
||||||
|
let userID = try client.userId()
|
||||||
|
return await Task.dispatch(on: .global()) {
|
||||||
|
do {
|
||||||
|
let notification = try self.client.getNotificationItem(roomId: roomID, eventId: eventID)
|
||||||
|
return NotificationItemProxy(notificationItem: notification, receiverID: userID)
|
||||||
|
} catch {
|
||||||
|
MXLog.error("NSE: Could not get notification's content creating an empty notification instead, error: \(error)")
|
||||||
|
return EmptyNotificationItemProxy(eventID: eventID, roomID: roomID, receiverID: userID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -72,8 +72,6 @@ targets:
|
|||||||
- path: ../../ElementX/Sources/Services/Keychain/KeychainControllerProtocol.swift
|
- path: ../../ElementX/Sources/Services/Keychain/KeychainControllerProtocol.swift
|
||||||
- path: ../../ElementX/Sources/Services/Keychain/KeychainController.swift
|
- path: ../../ElementX/Sources/Services/Keychain/KeychainController.swift
|
||||||
- path: ../../ElementX/Sources/Services/UserSession/RestorationToken.swift
|
- path: ../../ElementX/Sources/Services/UserSession/RestorationToken.swift
|
||||||
- path: ../../ElementX/Sources/Services/Notification/Proxy/NotificationServiceProxyProtocol.swift
|
|
||||||
- path: ../../ElementX/Sources/Services/Notification/Proxy/NotificationServiceProxy.swift
|
|
||||||
- path: ../../ElementX/Sources/Services/Notification/Proxy/NotificationItemProxy.swift
|
- path: ../../ElementX/Sources/Services/Notification/Proxy/NotificationItemProxy.swift
|
||||||
- path: ../../ElementX/Sources/Services/Notification/NotificationConstants.swift
|
- path: ../../ElementX/Sources/Services/Notification/NotificationConstants.swift
|
||||||
- path: ../../ElementX/Sources/Services/Media/Provider
|
- path: ../../ElementX/Sources/Services/Media/Provider
|
||||||
|
1
changelog.d/855.feature
Normal file
1
changelog.d/855.feature
Normal file
@ -0,0 +1 @@
|
|||||||
|
Remote Push Notifications can now be displayed as rich push notifications.
|
Loading…
x
Reference in New Issue
Block a user