Notifications are now handled when the app is in a killed state (#801)

* WIP, added support for receiver id and managed a way to store the app router state

* WIP, added support for receiver id and managed a way to store the app router state

* Notification Manager is now becoming the UNUserNotificationDelegate ASAP

* code improvements + changelog

* fixed Unit Tests

* pr suggestions
This commit is contained in:
Mauro
2023-04-14 14:24:48 +02:00
committed by GitHub
parent 41994baa4f
commit 3d0d883637
19 changed files with 338 additions and 89 deletions
@@ -142,6 +142,15 @@
"version" : "1.0.0" "version" : "1.0.0"
} }
}, },
{
"identity" : "swift-case-paths",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-case-paths",
"state" : {
"revision" : "fc45e7b2cfece9dd80b5a45e6469ffe67fe67984",
"version" : "0.14.1"
}
},
{ {
"identity" : "swift-collections", "identity" : "swift-collections",
"kind" : "remoteSourceControl", "kind" : "remoteSourceControl",
@@ -160,15 +169,33 @@
"version" : "1.0.2" "version" : "1.0.2"
} }
}, },
{
"identity" : "swift-parsing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-parsing",
"state" : {
"revision" : "c6e2241daa46e5c6e5027a93b161bca6ba692bcc",
"version" : "0.12.0"
}
},
{ {
"identity" : "swift-snapshot-testing", "identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl", "kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing", "location" : "https://github.com/pointfreeco/swift-snapshot-testing.git",
"state" : { "state" : {
"revision" : "cef5b3f6f11781dd4591bdd1dd0a3d22bd609334", "revision" : "cef5b3f6f11781dd4591bdd1dd0a3d22bd609334",
"version" : "1.11.0" "version" : "1.11.0"
} }
}, },
{
"identity" : "swift-url-routing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-url-routing",
"state" : {
"revision" : "2f4f0404b3de0a0711feb7190f724d8a80bc1cfd",
"version" : "0.5.0"
}
},
{ {
"identity" : "swiftstate", "identity" : "swiftstate",
"kind" : "remoteSourceControl", "kind" : "remoteSourceControl",
@@ -204,6 +231,15 @@
"revision" : "1fe824b80d89201652e7eca7c9252269a1d85e25", "revision" : "1fe824b80d89201652e7eca7c9252269a1d85e25",
"version" : "2.0.1" "version" : "2.0.1"
} }
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "ab8c9f45843694dd16be4297e6d44c0634fd9913",
"version" : "0.8.4"
}
} }
], ],
"version" : 2 "version" : 2
@@ -47,7 +47,9 @@ class AppCoordinator: AppCoordinatorProtocol {
private var userSessionCancellables = Set<AnyCancellable>() private var userSessionCancellables = Set<AnyCancellable>()
private var cancellables = Set<AnyCancellable>() private var cancellables = Set<AnyCancellable>()
private(set) var notificationManager: NotificationManagerProtocol? let notificationManager: NotificationManagerProtocol
@Consumable private var storedAppRoute: AppRoute?
init() { init() {
navigationRootCoordinator = NavigationRootCoordinator() navigationRootCoordinator = NavigationRootCoordinator()
@@ -66,6 +68,10 @@ class AppCoordinator: AppCoordinatorProtocol {
} }
userSessionStore = UserSessionStore(backgroundTaskService: backgroundTaskService) userSessionStore = UserSessionStore(backgroundTaskService: backgroundTaskService)
notificationManager = NotificationManager()
notificationManager.delegate = self
notificationManager.start()
guard let currentVersion = Version(InfoPlistReader(bundle: .main).bundleShortVersionString) else { guard let currentVersion = Version(InfoPlistReader(bundle: .main).bundleShortVersionString) else {
fatalError("The app's version number **must** use semver for migration purposes.") fatalError("The app's version number **must** use semver for migration purposes.")
@@ -259,6 +265,10 @@ class AppCoordinator: AppCoordinatorProtocol {
self.userSessionFlowCoordinator = userSessionFlowCoordinator self.userSessionFlowCoordinator = userSessionFlowCoordinator
navigationRootCoordinator.setRootCoordinator(navigationSplitCoordinator) navigationRootCoordinator.setRootCoordinator(navigationSplitCoordinator)
if let storedAppRoute {
userSessionFlowCoordinator.handleAppRoute(storedAppRoute)
}
} }
private func logout(isSoft: Bool) { private func logout(isSoft: Bool) {
@@ -292,11 +302,10 @@ class AppCoordinator: AppCoordinatorProtocol {
userSession = nil userSession = nil
userSessionFlowCoordinator = nil userSessionFlowCoordinator = nil
notificationManager?.delegate = nil
notificationManager = nil
}
notificationManager.setClientProxy(nil)
}
private func presentSplashScreen(isSoftLogout: Bool = false) { private func presentSplashScreen(isSoftLogout: Bool = false) {
navigationRootCoordinator.setRootCoordinator(SplashScreenCoordinator()) navigationRootCoordinator.setRootCoordinator(SplashScreenCoordinator())
@@ -308,34 +317,23 @@ class AppCoordinator: AppCoordinatorProtocol {
} }
private func configureNotificationManager() { private func configureNotificationManager() {
guard ServiceLocator.shared.settings.enableNotifications else { notificationManager.setClientProxy(userSession.clientProxy)
return notificationManager.requestAuthorization()
}
guard notificationManager == nil else {
return
}
let manager = NotificationManager(clientProxy: userSession.clientProxy) if let appDelegate = AppDelegate.shared {
if manager.isAvailable { appDelegate.callbacks
manager.delegate = self .receive(on: DispatchQueue.main)
notificationManager = manager .sink { [weak self] callback in
manager.start() switch callback {
case .registeredNotifications(let deviceToken):
if let appDelegate = AppDelegate.shared { Task { await self?.notificationManager.register(with: deviceToken) }
appDelegate.callbacks case .failedToRegisteredNotifications(let error):
.receive(on: DispatchQueue.main) self?.notificationManager.registrationFailed(with: error)
.sink { [weak self] callback in
switch callback {
case .registeredNotifications(let deviceToken):
Task { await self?.notificationManager?.register(with: deviceToken) }
case .failedToRegisteredNotifications(let error):
self?.notificationManager?.registrationFailed(with: error)
}
} }
.store(in: &cancellables) }
} else { .store(in: &cancellables)
MXLog.error("Couldn't register to AppDelegate callbacks") } else {
} MXLog.error("Couldn't register to AppDelegate callbacks")
} }
} }
@@ -437,6 +435,14 @@ class AppCoordinator: AppCoordinatorProtocol {
} }
}.store(in: &cancellables) }.store(in: &cancellables)
} }
private func handleAppRoute(_ appRoute: AppRoute) {
if let userSessionFlowCoordinator {
userSessionFlowCoordinator.handleAppRoute(appRoute)
} else {
storedAppRoute = appRoute
}
}
} }
// MARK: - AuthenticationCoordinatorDelegate // MARK: - AuthenticationCoordinatorDelegate
@@ -472,11 +478,14 @@ extension AppCoordinator: NotificationManagerDelegate {
MXLog.info("[AppCoordinator] tappedNotification") MXLog.info("[AppCoordinator] tappedNotification")
// We store the room identifier into the thread identifier // We store the room identifier into the thread identifier
guard !content.threadIdentifier.isEmpty else { guard !content.threadIdentifier.isEmpty,
content.receiverID != nil else {
return return
} }
userSessionFlowCoordinator?.handleAppRoute(.room(roomID: content.threadIdentifier)) // Handle here the account switching when available
handleAppRoute(.room(roomID: content.threadIdentifier))
} }
func handleInlineReply(_ service: NotificationManagerProtocol, content: UNNotificationContent, replyText: String) async { func handleInlineReply(_ service: NotificationManagerProtocol, content: UNNotificationContent, replyText: String) async {
@@ -17,5 +17,5 @@
import Foundation import Foundation
protocol AppCoordinatorProtocol: CoordinatorProtocol { protocol AppCoordinatorProtocol: CoordinatorProtocol {
var notificationManager: NotificationManagerProtocol? { get } var notificationManager: NotificationManagerProtocol { get }
} }
@@ -94,9 +94,7 @@ final class AppSettings: ObservableObject {
} }
let pushGatewayBaseURL = URL(staticString: "https://matrix.org/_matrix/push/v1/notify") let pushGatewayBaseURL = URL(staticString: "https://matrix.org/_matrix/push/v1/notify")
let enableNotifications = true
// MARK: - Bug report // MARK: - Bug report
let bugReportServiceBaseURL = URL(staticString: "https://riot.im/bugreports") let bugReportServiceBaseURL = URL(staticString: "https://riot.im/bugreports")
@@ -16,6 +16,40 @@
import Foundation import Foundation
import URLRouting
enum AppRoute { enum AppRoute {
case room(roomID: String) case room(roomID: String)
} }
struct AppRouterManager {
private let deeplinkRouter = OneOf {
Route(.case(AppRoute.room(roomID:))) {
// Check with product if this is the expect path
Path { "room" }
Query {
Field("id") { Parse(.string) }
}
}
}
private let permalinkRouter = OneOf {
Route(.case(AppRoute.room(roomID:))) {
Host("matrix.to")
Path {
"#"
Parse(.string)
}
}
}
func route(from url: URL) -> AppRoute? {
var route: AppRoute?
if let deeplinkRoute = try? deeplinkRouter.match(url: url) {
route = deeplinkRoute
} else if let permalinkRoute = try? permalinkRouter.match(url: url) {
route = permalinkRoute
}
return route
}
}
@@ -50,6 +50,103 @@ class BugReportServiceMock: BugReportServiceProtocol {
} }
} }
} }
class NotificationManagerMock: NotificationManagerProtocol {
var delegate: NotificationManagerDelegate?
//MARK: - start
var startCallsCount = 0
var startCalled: Bool {
return startCallsCount > 0
}
var startClosure: (() -> Void)?
func start() {
startCallsCount += 1
startClosure?()
}
//MARK: - register
var registerWithCallsCount = 0
var registerWithCalled: Bool {
return registerWithCallsCount > 0
}
var registerWithReceivedDeviceToken: Data?
var registerWithReceivedInvocations: [Data] = []
var registerWithReturnValue: Bool!
var registerWithClosure: ((Data) async -> Bool)?
func register(with deviceToken: Data) async -> Bool {
registerWithCallsCount += 1
registerWithReceivedDeviceToken = deviceToken
registerWithReceivedInvocations.append(deviceToken)
if let registerWithClosure = registerWithClosure {
return await registerWithClosure(deviceToken)
} else {
return registerWithReturnValue
}
}
//MARK: - registrationFailed
var registrationFailedWithCallsCount = 0
var registrationFailedWithCalled: Bool {
return registrationFailedWithCallsCount > 0
}
var registrationFailedWithReceivedError: Error?
var registrationFailedWithReceivedInvocations: [Error] = []
var registrationFailedWithClosure: ((Error) -> Void)?
func registrationFailed(with error: Error) {
registrationFailedWithCallsCount += 1
registrationFailedWithReceivedError = error
registrationFailedWithReceivedInvocations.append(error)
registrationFailedWithClosure?(error)
}
//MARK: - showLocalNotification
var showLocalNotificationWithSubtitleCallsCount = 0
var showLocalNotificationWithSubtitleCalled: Bool {
return showLocalNotificationWithSubtitleCallsCount > 0
}
var showLocalNotificationWithSubtitleReceivedArguments: (title: String, subtitle: String?)?
var showLocalNotificationWithSubtitleReceivedInvocations: [(title: String, subtitle: String?)] = []
var showLocalNotificationWithSubtitleClosure: ((String, String?) async -> Void)?
func showLocalNotification(with title: String, subtitle: String?) async {
showLocalNotificationWithSubtitleCallsCount += 1
showLocalNotificationWithSubtitleReceivedArguments = (title: title, subtitle: subtitle)
showLocalNotificationWithSubtitleReceivedInvocations.append((title: title, subtitle: subtitle))
await showLocalNotificationWithSubtitleClosure?(title, subtitle)
}
//MARK: - setClientProxy
var setClientProxyCallsCount = 0
var setClientProxyCalled: Bool {
return setClientProxyCallsCount > 0
}
var setClientProxyReceivedClientProxy: ClientProxyProtocol?
var setClientProxyReceivedInvocations: [ClientProxyProtocol?] = []
var setClientProxyClosure: ((ClientProxyProtocol?) -> Void)?
func setClientProxy(_ clientProxy: ClientProxyProtocol?) {
setClientProxyCallsCount += 1
setClientProxyReceivedClientProxy = clientProxy
setClientProxyReceivedInvocations.append(clientProxy)
setClientProxyClosure?(clientProxy)
}
//MARK: - requestAuthorization
var requestAuthorizationCallsCount = 0
var requestAuthorizationCalled: Bool {
return requestAuthorizationCallsCount > 0
}
var requestAuthorizationClosure: (() -> Void)?
func requestAuthorization() {
requestAuthorizationCallsCount += 1
requestAuthorizationClosure?()
}
}
class RoomMemberProxyMock: RoomMemberProxyProtocol { class RoomMemberProxyMock: RoomMemberProxyProtocol {
var userID: String { var userID: String {
get { return underlyingUserID } get { return underlyingUserID }
+37
View File
@@ -0,0 +1,37 @@
//
// 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
@propertyWrapper struct Consumable<Value> {
var wrappedValue: Value? {
mutating get {
defer {
value = nil
}
return value
}
set {
value = newValue
}
}
private var value: Value?
init(value: Value? = nil) {
self.value = value
}
}
@@ -1,5 +1,5 @@
// //
// Copyright 2022 New Vector Ltd // Copyright 2023 New Vector Ltd
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@@ -15,23 +15,21 @@
// //
import Foundation import Foundation
import UserNotifications
class MockNotificationManager: NotificationManagerProtocol { extension UNNotificationContent {
// MARK: NotificationManagerProtocol @objc var receiverID: String? {
userInfo[NotificationConstants.UserInfoKey.receiverIdentifier] as? String
var isAvailable: Bool { }
false }
extension UNMutableNotificationContent {
override var receiverID: String? {
get {
userInfo[NotificationConstants.UserInfoKey.receiverIdentifier] as? String
}
set {
userInfo[NotificationConstants.UserInfoKey.receiverIdentifier] = newValue
}
} }
weak var delegate: NotificationManagerDelegate?
func start() {
delegate?.authorizationStatusUpdated(self, granted: false)
}
func register(with deviceToken: Data) async -> Bool { false }
func registrationFailed(with error: Error) { }
func showLocalNotification(with title: String, subtitle: String?) { }
} }
@@ -20,10 +20,9 @@ import UserNotifications
class NotificationManager: NSObject, NotificationManagerProtocol { class NotificationManager: NSObject, NotificationManagerProtocol {
private let notificationCenter: UserNotificationCenterProtocol private let notificationCenter: UserNotificationCenterProtocol
private let clientProxy: ClientProxyProtocol private var clientProxy: ClientProxyProtocol?
init(clientProxy: ClientProxyProtocol, notificationCenter: UserNotificationCenterProtocol = UNUserNotificationCenter.current()) { init(notificationCenter: UserNotificationCenterProtocol = UNUserNotificationCenter.current()) {
self.clientProxy = clientProxy
self.notificationCenter = notificationCenter self.notificationCenter = notificationCenter
super.init() super.init()
} }
@@ -32,10 +31,6 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
weak var delegate: NotificationManagerDelegate? weak var delegate: NotificationManagerDelegate?
var isAvailable: Bool {
true
}
func start() { func start() {
let replyAction = UNTextInputNotificationAction(identifier: NotificationConstants.Action.inlineReply, let replyAction = UNTextInputNotificationAction(identifier: NotificationConstants.Action.inlineReply,
title: L10n.actionQuickReply, title: L10n.actionQuickReply,
@@ -46,6 +41,9 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
options: []) options: [])
notificationCenter.setNotificationCategories([replyCategory]) notificationCenter.setNotificationCategories([replyCategory])
notificationCenter.delegate = self notificationCenter.delegate = self
}
func requestAuthorization() {
Task { Task {
do { do {
let granted = try await notificationCenter.requestAuthorization(options: [.alert, .sound, .badge]) let granted = try await notificationCenter.requestAuthorization(options: [.alert, .sound, .badge])
@@ -60,7 +58,14 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
} }
func register(with deviceToken: Data) async -> Bool { func register(with deviceToken: Data) async -> Bool {
await setPusher(with: deviceToken, clientProxy: clientProxy) guard let clientProxy else {
return false
}
return await setPusher(with: deviceToken, clientProxy: clientProxy)
}
func setClientProxy(_ clientProxy: ClientProxyProtocol?) {
self.clientProxy = clientProxy
} }
func registrationFailed(with error: Error) { } func registrationFailed(with error: Error) { }
@@ -31,12 +31,14 @@ protocol NotificationManagerDelegate: AnyObject {
// MARK: - NotificationManagerProtocol // MARK: - NotificationManagerProtocol
protocol NotificationManagerProtocol { // sourcery: AutoMockable
var isAvailable: Bool { get } protocol NotificationManagerProtocol: AnyObject {
var delegate: NotificationManagerDelegate? { get set } var delegate: NotificationManagerDelegate? { get set }
func start() func start()
func register(with deviceToken: Data) async -> Bool func register(with deviceToken: Data) async -> Bool
func registrationFailed(with error: Error) func registrationFailed(with error: Error)
func showLocalNotification(with title: String, subtitle: String?) async func showLocalNotification(with title: String, subtitle: String?) async
func setClientProxy(_ clientProxy: ClientProxyProtocol?)
func requestAuthorization()
} }
@@ -22,6 +22,7 @@ enum NotificationConstants {
static let eventIdentifier = "event_id" static let eventIdentifier = "event_id"
static let unreadCount = "unread_count" static let unreadCount = "unread_count"
static let pusherNotificationClientIdentifier = "pusher_notification_client_identifier" static let pusherNotificationClientIdentifier = "pusher_notification_client_identifier"
static let receiverIdentifier = "receiver_id"
} }
enum Category { enum Category {
@@ -20,7 +20,7 @@ import UIKit
class UITestsAppCoordinator: AppCoordinatorProtocol { class UITestsAppCoordinator: AppCoordinatorProtocol {
private let navigationRootCoordinator: NavigationRootCoordinator private let navigationRootCoordinator: NavigationRootCoordinator
private var mockScreen: MockScreen? private var mockScreen: MockScreen?
var notificationManager: NotificationManagerProtocol? let notificationManager: NotificationManagerProtocol = NotificationManagerMock()
init() { init() {
UIView.setAnimationsEnabled(false) UIView.setAnimationsEnabled(false)
+1
View File
@@ -146,6 +146,7 @@ targets:
- package: SwiftState - package: SwiftState
- package: GZIP - package: GZIP
- package: Sentry - package: Sentry
- package: URLRouting
- package: Version - package: Version
sources: sources:
@@ -88,7 +88,8 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
// First process without a media proxy. // First process without a media proxy.
// After this some properties of the notification should be set, like title, subtitle, sound etc. // After this some properties of the notification should be set, like title, subtitle, sound etc.
guard let firstContent = try await itemProxy.process(with: roomId, guard let firstContent = try await itemProxy.process(receiverId: credentials.userID,
roomId: roomId,
mediaProvider: nil) else { mediaProvider: nil) else {
MXLog.error("\(tag) not even first content") MXLog.error("\(tag) not even first content")
@@ -109,7 +110,8 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
MXLog.info("\(tag) process with media") MXLog.info("\(tag) process with media")
// There is some media to load, process it again // There is some media to load, process it again
if let latestContent = try await itemProxy.process(with: roomId, if let latestContent = try await itemProxy.process(receiverId: credentials.userID,
roomId: roomId,
mediaProvider: createMediaProvider(with: credentials)) { mediaProvider: createMediaProvider(with: credentials)) {
// Processing finished, hopefully with some media // Processing finished, hopefully with some media
modifiedContent = latestContent modifiedContent = latestContent
@@ -48,10 +48,12 @@ extension NotificationItemProxy {
/// Process the receiver item proxy /// Process the receiver item proxy
/// - Parameters: /// - Parameters:
/// - receiverId: identifier of the user that has received the notification
/// - 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(with roomId: String, func process(receiverId: String,
roomId: String,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent? { mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent? {
// switch timelineItemProxy { // switch timelineItemProxy {
// case .event(let eventItem): // case .event(let eventItem):
@@ -75,7 +77,7 @@ extension NotificationItemProxy {
// For now we can't solve the sender ID nor get the type of message that we are displaying // For now we can't solve the sender ID nor get the type of message that we are displaying
// so we are just going to process all of them as a text notification saying "Notification" // so we are just going to process all of them as a text notification saying "Notification"
let content = TextMessageContent(body: L10n.notification, formatted: nil) let content = TextMessageContent(body: L10n.notification, formatted: nil)
return try await processText(content: content, senderId: "undefined", roomId: roomId, mediaProvider: mediaProvider) return try await processText(content: content, receiverId: receiverId, senderId: "undefined", roomId: roomId, mediaProvider: mediaProvider)
} }
// MARK: - Private // MARK: - Private
@@ -83,42 +85,50 @@ extension NotificationItemProxy {
// MARK: Common // MARK: Common
private func process(message: Message, private func process(message: Message,
receiverId: String,
senderId: String, senderId: String,
roomId: String, roomId: String,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent? { mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent? {
switch message.msgtype() { switch message.msgtype() {
case .text(content: let content): case .text(content: let content):
return try await processText(content: content, return try await processText(content: content,
receiverId: receiverId,
senderId: senderId, senderId: senderId,
roomId: roomId, roomId: roomId,
mediaProvider: mediaProvider) mediaProvider: mediaProvider)
case .image(content: let content): case .image(content: let content):
return try await processImage(content: content, return try await processImage(content: content,
receiverId: receiverId,
senderId: senderId, senderId: senderId,
roomId: roomId, roomId: roomId,
mediaProvider: mediaProvider) mediaProvider: mediaProvider)
case .audio(content: let content): case .audio(content: let content):
return try await processAudio(content: content, return try await processAudio(content: content,
receiverId: receiverId,
senderId: senderId, senderId: senderId,
roomId: roomId, roomId: roomId,
mediaProvider: mediaProvider) mediaProvider: mediaProvider)
case .video(content: let content): case .video(content: let content):
return try await processVideo(content: content, return try await processVideo(content: content,
receiverId: receiverId,
senderId: senderId, senderId: senderId,
roomId: roomId, roomId: roomId,
mediaProvider: mediaProvider) mediaProvider: mediaProvider)
case .file(content: let content): case .file(content: let content):
return try await processFile(content: content, return try await processFile(content: content,
receiverId: receiverId,
senderId: senderId, senderId: senderId,
roomId: roomId, roomId: roomId,
mediaProvider: mediaProvider) mediaProvider: mediaProvider)
case .notice(content: let content): case .notice(content: let content):
return try await processNotice(content: content, return try await processNotice(content: content,
receiverId: receiverId,
senderId: senderId, senderId: senderId,
roomId: roomId, roomId: roomId,
mediaProvider: mediaProvider) mediaProvider: mediaProvider)
case .emote(content: let content): case .emote(content: let content):
return try await processEmote(content: content, return try await processEmote(content: content,
receiverId: receiverId,
senderId: senderId, senderId: senderId,
roomId: roomId, roomId: roomId,
mediaProvider: mediaProvider) mediaProvider: mediaProvider)
@@ -127,10 +137,12 @@ extension NotificationItemProxy {
} }
} }
private func processCommon(senderId: String, private func processCommon(receiverId: String,
senderId: String,
roomId: String, roomId: String,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent { mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
var notification = UNMutableNotificationContent() var notification = UNMutableNotificationContent()
notification.receiverID = receiverId
notification.title = title notification.title = title
if let subtitle = subtitle { if let subtitle = subtitle {
notification.subtitle = subtitle notification.subtitle = subtitle
@@ -153,10 +165,12 @@ extension NotificationItemProxy {
// MARK: Message Types // MARK: Message Types
private func processText(content: TextMessageContent, private func processText(content: TextMessageContent,
receiverId: String,
senderId: String, senderId: String,
roomId: String, roomId: String,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent { mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
let notification = try await processCommon(senderId: senderId, let notification = try await processCommon(receiverId: receiverId,
senderId: senderId,
roomId: roomId, roomId: roomId,
mediaProvider: mediaProvider) mediaProvider: mediaProvider)
notification.body = content.body notification.body = content.body
@@ -165,10 +179,12 @@ extension NotificationItemProxy {
} }
private func processImage(content: ImageMessageContent, private func processImage(content: ImageMessageContent,
receiverId: String,
senderId: String, senderId: String,
roomId: String, roomId: String,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent { mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
var notification = try await processCommon(senderId: senderId, var notification = try await processCommon(receiverId: receiverId,
senderId: senderId,
roomId: roomId, roomId: roomId,
mediaProvider: mediaProvider) mediaProvider: mediaProvider)
notification.body = "📷 " + content.body notification.body = "📷 " + content.body
@@ -180,10 +196,12 @@ extension NotificationItemProxy {
} }
private func processVideo(content: VideoMessageContent, private func processVideo(content: VideoMessageContent,
receiverId: String,
senderId: String, senderId: String,
roomId: String, roomId: String,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent { mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
var notification = try await processCommon(senderId: senderId, var notification = try await processCommon(receiverId: receiverId,
senderId: senderId,
roomId: roomId, roomId: roomId,
mediaProvider: mediaProvider) mediaProvider: mediaProvider)
notification.body = "📹 " + content.body notification.body = "📹 " + content.body
@@ -195,10 +213,12 @@ extension NotificationItemProxy {
} }
private func processFile(content: FileMessageContent, private func processFile(content: FileMessageContent,
receiverId: String,
senderId: String, senderId: String,
roomId: String, roomId: String,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent { mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
let notification = try await processCommon(senderId: senderId, let notification = try await processCommon(receiverId: receiverId,
senderId: senderId,
roomId: roomId, roomId: roomId,
mediaProvider: mediaProvider) mediaProvider: mediaProvider)
notification.body = "📄 " + content.body notification.body = "📄 " + content.body
@@ -207,10 +227,12 @@ extension NotificationItemProxy {
} }
private func processNotice(content: NoticeMessageContent, private func processNotice(content: NoticeMessageContent,
receiverId: String,
senderId: String, senderId: String,
roomId: String, roomId: String,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent { mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
let notification = try await processCommon(senderId: senderId, let notification = try await processCommon(receiverId: receiverId,
senderId: senderId,
roomId: roomId, roomId: roomId,
mediaProvider: mediaProvider) mediaProvider: mediaProvider)
notification.body = "❕ " + content.body notification.body = "❕ " + content.body
@@ -219,10 +241,12 @@ extension NotificationItemProxy {
} }
private func processEmote(content: EmoteMessageContent, private func processEmote(content: EmoteMessageContent,
receiverId: String,
senderId: String, senderId: String,
roomId: String, roomId: String,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent { mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
let notification = try await processCommon(senderId: senderId, let notification = try await processCommon(receiverId: receiverId,
senderId: senderId,
roomId: roomId, roomId: roomId,
mediaProvider: mediaProvider) mediaProvider: mediaProvider)
notification.body = "🫥 " + content.body notification.body = "🫥 " + content.body
@@ -231,10 +255,12 @@ extension NotificationItemProxy {
} }
private func processAudio(content: AudioMessageContent, private func processAudio(content: AudioMessageContent,
receiverId: String,
senderId: String, senderId: String,
roomId: String, roomId: String,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent { mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
let notification = try await processCommon(senderId: senderId, let notification = try await processCommon(receiverId: receiverId,
senderId: senderId,
roomId: roomId, roomId: roomId,
mediaProvider: mediaProvider) mediaProvider: mediaProvider)
notification.body = "🔊 " + content.body notification.body = "🔊 " + content.body
+1
View File
@@ -89,3 +89,4 @@ targets:
- path: ../../ElementX/Sources/Other/Extensions/UTType.swift - path: ../../ElementX/Sources/Other/Extensions/UTType.swift
- path: ../../ElementX/Sources/Other/AvatarSize.swift - path: ../../ElementX/Sources/Other/AvatarSize.swift
- path: ../../ElementX/Sources/Other/InfoPlistReader.swift - path: ../../ElementX/Sources/Other/InfoPlistReader.swift
- path: ../../ElementX/Sources/Other/Extensions/UNNotificationContent.swift
@@ -30,7 +30,9 @@ final class NotificationManagerTests: XCTestCase {
private let settings = ServiceLocator.shared.settings private let settings = ServiceLocator.shared.settings
override func setUp() { override func setUp() {
notificationManager = NotificationManager(clientProxy: clientProxy, notificationCenter: notificationCenter) notificationManager = NotificationManager(notificationCenter: notificationCenter)
notificationManager.start()
notificationManager.setClientProxy(clientProxy)
} }
func test_whenRegistered_pusherIsCalled() async { func test_whenRegistered_pusherIsCalled() async {
@@ -95,7 +97,6 @@ final class NotificationManagerTests: XCTestCase {
} }
func test_whenStart_notificationCategoriesAreSet() throws { func test_whenStart_notificationCategoriesAreSet() throws {
notificationManager.start()
let replyAction = UNTextInputNotificationAction(identifier: NotificationConstants.Action.inlineReply, let replyAction = UNTextInputNotificationAction(identifier: NotificationConstants.Action.inlineReply,
title: L10n.actionQuickReply, title: L10n.actionQuickReply,
options: []) options: [])
@@ -107,13 +108,12 @@ final class NotificationManagerTests: XCTestCase {
} }
func test_whenStart_delegateIsSet() throws { func test_whenStart_delegateIsSet() throws {
notificationManager.start()
let delegate = try XCTUnwrap(notificationCenter.delegate) let delegate = try XCTUnwrap(notificationCenter.delegate)
XCTAssertTrue(delegate.isEqual(notificationManager)) XCTAssertTrue(delegate.isEqual(notificationManager))
} }
func test_whenStart_requestAuthorizationCalledWithCorrectParams() async throws { func test_whenStart_requestAuthorizationCalledWithCorrectParams() async throws {
notificationManager.start() notificationManager.requestAuthorization()
await Task.yield() await Task.yield()
XCTAssertEqual(notificationCenter.requestAuthorizationOptions, [.alert, .sound, .badge]) XCTAssertEqual(notificationCenter.requestAuthorizationOptions, [.alert, .sound, .badge])
} }
@@ -122,13 +122,13 @@ final class NotificationManagerTests: XCTestCase {
authorizationStatusWasGranted = false authorizationStatusWasGranted = false
notificationCenter.requestAuthorizationGrantedReturnValue = true notificationCenter.requestAuthorizationGrantedReturnValue = true
notificationManager.delegate = self notificationManager.delegate = self
notificationManager.start()
notificationManager.requestAuthorization()
try await Task.sleep(for: .milliseconds(100)) try await Task.sleep(for: .milliseconds(100))
XCTAssertTrue(authorizationStatusWasGranted) XCTAssertTrue(authorizationStatusWasGranted)
} }
func test_whenWillPresentNotificationsDelegateNotSet_CorrectPresentationOptionsReturned() async throws { func test_whenWillPresentNotificationsDelegateNotSet_CorrectPresentationOptionsReturned() async throws {
notificationManager.start()
let archiver = MockCoder(requiringSecureCoding: false) let archiver = MockCoder(requiringSecureCoding: false)
let notification = try XCTUnwrap(UNNotification(coder: archiver)) let notification = try XCTUnwrap(UNNotification(coder: archiver))
let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification) let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification)
@@ -138,7 +138,7 @@ final class NotificationManagerTests: XCTestCase {
func test_whenWillPresentNotificationsDelegateSetAndNotificationsShoudNotBeDisplayed_CorrectPresentationOptionsReturned() async throws { func test_whenWillPresentNotificationsDelegateSetAndNotificationsShoudNotBeDisplayed_CorrectPresentationOptionsReturned() async throws {
shouldDisplayInAppNotificationReturnValue = false shouldDisplayInAppNotificationReturnValue = false
notificationManager.delegate = self notificationManager.delegate = self
notificationManager.start()
let notification = try UNNotification.with(userInfo: [AnyHashable: Any]()) let notification = try UNNotification.with(userInfo: [AnyHashable: Any]())
let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification) let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification)
XCTAssertEqual(options, []) XCTAssertEqual(options, [])
@@ -147,7 +147,7 @@ final class NotificationManagerTests: XCTestCase {
func test_whenWillPresentNotificationsDelegateSetAndNotificationsShoudBeDisplayed_CorrectPresentationOptionsReturned() async throws { func test_whenWillPresentNotificationsDelegateSetAndNotificationsShoudBeDisplayed_CorrectPresentationOptionsReturned() async throws {
shouldDisplayInAppNotificationReturnValue = true shouldDisplayInAppNotificationReturnValue = true
notificationManager.delegate = self notificationManager.delegate = self
notificationManager.start()
let notification = try UNNotification.with(userInfo: [AnyHashable: Any]()) let notification = try UNNotification.with(userInfo: [AnyHashable: Any]())
let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification) let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification)
XCTAssertEqual(options, [.badge, .sound, .list, .banner]) XCTAssertEqual(options, [.badge, .sound, .list, .banner])
@@ -156,7 +156,6 @@ final class NotificationManagerTests: XCTestCase {
func test_whenNotificationCenterReceivedResponseInLineReply_delegateIsCalled() async throws { func test_whenNotificationCenterReceivedResponseInLineReply_delegateIsCalled() async throws {
handleInlineReplyDelegateCalled = false handleInlineReplyDelegateCalled = false
notificationManager.delegate = self notificationManager.delegate = self
notificationManager.start()
let response = try UNTextInputNotificationResponse.with(userInfo: [AnyHashable: Any](), actionIdentifier: NotificationConstants.Action.inlineReply) let response = try UNTextInputNotificationResponse.with(userInfo: [AnyHashable: Any](), actionIdentifier: NotificationConstants.Action.inlineReply)
await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), didReceive: response) await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), didReceive: response)
XCTAssertTrue(handleInlineReplyDelegateCalled) XCTAssertTrue(handleInlineReplyDelegateCalled)
@@ -165,7 +164,6 @@ final class NotificationManagerTests: XCTestCase {
func test_whenNotificationCenterReceivedResponseWithActionIdentifier_delegateIsCalled() async throws { func test_whenNotificationCenterReceivedResponseWithActionIdentifier_delegateIsCalled() async throws {
notificationTappedDelegateCalled = false notificationTappedDelegateCalled = false
notificationManager.delegate = self notificationManager.delegate = self
notificationManager.start()
let response = try UNTextInputNotificationResponse.with(userInfo: [AnyHashable: Any](), actionIdentifier: UNNotificationDefaultActionIdentifier) let response = try UNTextInputNotificationResponse.with(userInfo: [AnyHashable: Any](), actionIdentifier: UNNotificationDefaultActionIdentifier)
await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), didReceive: response) await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), didReceive: response)
XCTAssertTrue(notificationTappedDelegateCalled) XCTAssertTrue(notificationTappedDelegateCalled)
+1
View File
@@ -0,0 +1 @@
Notifications are now handled when the app is in a killed state.
+3
View File
@@ -98,6 +98,9 @@ packages:
SnapshotTesting: SnapshotTesting:
url: https://github.com/pointfreeco/swift-snapshot-testing url: https://github.com/pointfreeco/swift-snapshot-testing
minorVersion: 1.11.0 minorVersion: 1.11.0
URLRouting:
url: https://github.com/pointfreeco/swift-url-routing
minorVersion: 0.5.0
Version: Version:
url: https://github.com/mxcl/Version url: https://github.com/mxcl/Version
minorVersion: 2.0.0 minorVersion: 2.0.0