mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
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:
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
class MockNotificationServiceProxy: NotificationServiceProxyProtocol {
|
||||
func notificationItem(roomId: String, eventId: String) async throws -> NotificationItemProxy? {
|
||||
func notificationItem(roomId: String, eventId: String) async throws -> NotificationItemProxyProtocol? {
|
||||
nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,59 +15,337 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UserNotifications
|
||||
|
||||
import MatrixRustSDK
|
||||
|
||||
struct NotificationItemProxy {
|
||||
// let notificationItem: NotificationItem
|
||||
//
|
||||
// init(notificationItem: NotificationItem) {
|
||||
// self.notificationItem = notificationItem
|
||||
// }
|
||||
//
|
||||
// var timelineItemProxy: TimelineItemProxy {
|
||||
// .init(item: notificationItem.item)
|
||||
// }
|
||||
//
|
||||
// var title: String {
|
||||
// notificationItem.title
|
||||
// }
|
||||
//
|
||||
// var subtitle: String? {
|
||||
// notificationItem.subtitle
|
||||
// }
|
||||
//
|
||||
// var isNoisy: Bool {
|
||||
// notificationItem.isNoisy
|
||||
// }
|
||||
//
|
||||
// var avatarURL: URL? {
|
||||
// notificationItem.avatarUrl
|
||||
// }
|
||||
//
|
||||
// var avatarMediaSource: MediaSourceProxy? {
|
||||
// guard let avatarUrl else {
|
||||
// return nil
|
||||
// }
|
||||
// return .init(urlString: avatarUrl)
|
||||
// }
|
||||
protocol NotificationItemProxyProtocol {
|
||||
var event: TimelineEventProxyProtocol { get }
|
||||
|
||||
var title: String {
|
||||
InfoPlistReader(bundle: .app).bundleDisplayName
|
||||
var roomID: String { get }
|
||||
|
||||
var senderDisplayName: String? { get }
|
||||
|
||||
var senderAvatarMediaSource: MediaSourceProxy? { get }
|
||||
|
||||
var roomDisplayName: String { get }
|
||||
|
||||
var roomAvatarMediaSource: MediaSourceProxy? { get }
|
||||
|
||||
var isNoisy: Bool { get }
|
||||
|
||||
var isDirect: Bool { get }
|
||||
|
||||
var isEncrypted: Bool { get }
|
||||
}
|
||||
|
||||
struct NotificationItemProxy: NotificationItemProxyProtocol {
|
||||
let notificationItem: NotificationItem
|
||||
|
||||
var event: TimelineEventProxyProtocol {
|
||||
TimelineEventProxy(timelineEvent: notificationItem.event)
|
||||
}
|
||||
|
||||
var subtitle: String? {
|
||||
nil
|
||||
var roomID: String {
|
||||
notificationItem.roomId
|
||||
}
|
||||
|
||||
var senderDisplayName: String? {
|
||||
notificationItem.senderDisplayName
|
||||
}
|
||||
|
||||
var roomDisplayName: String {
|
||||
notificationItem.roomDisplayName
|
||||
}
|
||||
|
||||
var isNoisy: Bool {
|
||||
false
|
||||
notificationItem.isNoisy
|
||||
}
|
||||
|
||||
var avatarURL: URL? {
|
||||
nil
|
||||
var isDirect: Bool {
|
||||
notificationItem.isDirect
|
||||
}
|
||||
|
||||
var avatarMediaSource: MediaSourceProxy? {
|
||||
nil
|
||||
var isEncrypted: Bool {
|
||||
notificationItem.isEncrypted
|
||||
}
|
||||
|
||||
var senderAvatarMediaSource: MediaSourceProxy? {
|
||||
if let senderAvatarURLString = notificationItem.senderAvatarUrl,
|
||||
let senderAvatarURL = URL(string: senderAvatarURLString) {
|
||||
return MediaSourceProxy(url: senderAvatarURL, mimeType: nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var roomAvatarMediaSource: MediaSourceProxy? {
|
||||
if let roomAvatarURLString = notificationItem.roomAvatarUrl,
|
||||
let roomAvatarURL = URL(string: roomAvatarURLString) {
|
||||
return MediaSourceProxy(url: roomAvatarURL, mimeType: nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// The mock and the protocol are just temporary until we can handle
|
||||
// and decrypt notifications both in background and in foreground
|
||||
// but they should not be necessary in the future
|
||||
struct MockNotificationItemProxy: NotificationItemProxyProtocol {
|
||||
let eventID: String
|
||||
|
||||
var event: TimelineEventProxyProtocol {
|
||||
MockTimelineEventProxy(eventID: eventID)
|
||||
}
|
||||
|
||||
let roomID: String
|
||||
|
||||
var senderDisplayName: String? { nil }
|
||||
|
||||
var senderAvatarURL: String? { nil }
|
||||
|
||||
var roomDisplayName: String { "" }
|
||||
|
||||
var roomAvatarURL: String? { nil }
|
||||
|
||||
var isNoisy: Bool { false }
|
||||
|
||||
var isDirect: Bool { false }
|
||||
|
||||
var isEncrypted: Bool { false }
|
||||
|
||||
var senderAvatarMediaSource: MediaSourceProxy? { nil }
|
||||
|
||||
var roomAvatarMediaSource: MediaSourceProxy? { nil }
|
||||
}
|
||||
|
||||
extension NotificationItemProxyProtocol {
|
||||
var requiresMediaProvider: Bool {
|
||||
if senderAvatarMediaSource != nil || roomAvatarMediaSource != nil {
|
||||
return true
|
||||
}
|
||||
switch event.type {
|
||||
case .state, .none:
|
||||
return false
|
||||
case let .messageLike(content):
|
||||
switch content {
|
||||
case let .roomMessage(messageType):
|
||||
switch messageType {
|
||||
case .image, .video, .audio:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// swiftlint: disable cyclomatic_complexity
|
||||
/// Process the receiver item proxy
|
||||
/// - Parameters:
|
||||
/// - receiverId: identifier of the user that has received the notification
|
||||
/// - roomId: Room identifier
|
||||
/// - 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.
|
||||
func process(receiverId: String,
|
||||
roomId: String,
|
||||
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent? {
|
||||
if self is MockNotificationItemProxy {
|
||||
return processMock(receiverId: receiverId, roomId: roomId)
|
||||
} else {
|
||||
switch event.type {
|
||||
case .none, .state:
|
||||
return nil
|
||||
case let .messageLike(content):
|
||||
switch content {
|
||||
case .roomMessage(messageType: let messageType):
|
||||
switch messageType {
|
||||
case .emote(content: let content):
|
||||
return try await processEmote(content: content, receiverId: receiverId, senderId: event.senderID, roomId: roomId, mediaProvider: mediaProvider)
|
||||
case .image(content: let content):
|
||||
return try await processImage(content: content, receiverId: receiverId, senderId: event.senderID, roomId: roomId, mediaProvider: mediaProvider)
|
||||
case .audio(content: let content):
|
||||
return try await processAudio(content: content, receiverId: receiverId, senderId: event.senderID, roomId: roomId, mediaProvider: mediaProvider)
|
||||
case .video(content: let content):
|
||||
return try await processVideo(content: content, receiverId: receiverId, senderId: event.senderID, roomId: roomId, mediaProvider: mediaProvider)
|
||||
case .file(content: let content):
|
||||
return try await processFile(content: content, receiverId: receiverId, senderId: event.senderID, roomId: roomId, mediaProvider: mediaProvider)
|
||||
case .notice(content: let content):
|
||||
return try await processNotice(content: content, receiverId: receiverId, senderId: event.senderID, roomId: roomId, mediaProvider: mediaProvider)
|
||||
case .text(content: let content):
|
||||
return try await processText(content: content, receiverId: receiverId, senderId: event.senderID, roomId: roomId, mediaProvider: mediaProvider)
|
||||
}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// swiftlint: enable cyclomatic_complexity
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
// To be removed once we don't need the mock anymore
|
||||
private func processMock(receiverId: String,
|
||||
roomId: String) -> UNMutableNotificationContent {
|
||||
let notification = UNMutableNotificationContent()
|
||||
notification.receiverID = receiverId
|
||||
notification.title = InfoPlistReader(bundle: .app).bundleDisplayName
|
||||
notification.body = L10n.notification
|
||||
notification.threadIdentifier = roomId
|
||||
notification.categoryIdentifier = NotificationConstants.Category.reply
|
||||
notification.sound = isNoisy ? UNNotificationSound(named: UNNotificationSoundName(rawValue: "message.caf")) : nil
|
||||
return notification
|
||||
}
|
||||
|
||||
private func processCommon(receiverId: String,
|
||||
senderId: String,
|
||||
roomId: String,
|
||||
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
|
||||
var notification = UNMutableNotificationContent()
|
||||
notification.receiverID = receiverId
|
||||
// These are fallbacks since the senderIcon also sets the title and the subtitle
|
||||
notification.title = senderDisplayName ?? roomDisplayName
|
||||
if notification.title != roomDisplayName {
|
||||
notification.subtitle = roomDisplayName
|
||||
}
|
||||
// We can store the room identifier into the thread identifier since it's used for notifications
|
||||
// that belong to the same group
|
||||
notification.threadIdentifier = roomId
|
||||
notification.categoryIdentifier = NotificationConstants.Category.reply
|
||||
notification.sound = isNoisy ? UNNotificationSound(named: UNNotificationSoundName(rawValue: "message.caf")) : nil
|
||||
|
||||
let senderName = senderDisplayName ?? roomDisplayName
|
||||
var groupName: String?
|
||||
var mediaSource: MediaSourceProxy?
|
||||
if !isDirect {
|
||||
groupName = senderName != roomDisplayName ? roomDisplayName : nil
|
||||
mediaSource = roomAvatarMediaSource
|
||||
} else {
|
||||
mediaSource = senderAvatarMediaSource
|
||||
}
|
||||
|
||||
notification = try await notification.addSenderIcon(using: mediaProvider,
|
||||
senderId: senderId,
|
||||
receiverId: receiverId,
|
||||
senderName: senderName,
|
||||
groupName: groupName,
|
||||
mediaSource: mediaSource,
|
||||
roomId: roomId)
|
||||
return notification
|
||||
}
|
||||
|
||||
// MARK: Message Types
|
||||
|
||||
private func processText(content: TextMessageContent,
|
||||
receiverId: String,
|
||||
senderId: String,
|
||||
roomId: String,
|
||||
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
|
||||
let notification = try await processCommon(receiverId: receiverId,
|
||||
senderId: senderId,
|
||||
roomId: roomId,
|
||||
mediaProvider: mediaProvider)
|
||||
notification.body = content.body
|
||||
|
||||
return notification
|
||||
}
|
||||
|
||||
private func processImage(content: ImageMessageContent,
|
||||
receiverId: String,
|
||||
senderId: String,
|
||||
roomId: String,
|
||||
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
|
||||
var notification = try await processCommon(receiverId: receiverId,
|
||||
senderId: senderId,
|
||||
roomId: roomId,
|
||||
mediaProvider: mediaProvider)
|
||||
notification.body = "📷 " + content.body
|
||||
|
||||
notification = await notification.addMediaAttachment(using: mediaProvider,
|
||||
mediaSource: .init(source: content.source,
|
||||
mimeType: content.info?.mimetype))
|
||||
|
||||
return notification
|
||||
}
|
||||
|
||||
private func processVideo(content: VideoMessageContent,
|
||||
receiverId: String,
|
||||
senderId: String,
|
||||
roomId: String,
|
||||
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
|
||||
var notification = try await processCommon(receiverId: receiverId,
|
||||
senderId: senderId,
|
||||
roomId: roomId,
|
||||
mediaProvider: mediaProvider)
|
||||
notification.body = "📹 " + content.body
|
||||
|
||||
notification = await notification.addMediaAttachment(using: mediaProvider,
|
||||
mediaSource: .init(source: content.source,
|
||||
mimeType: content.info?.mimetype))
|
||||
|
||||
return notification
|
||||
}
|
||||
|
||||
private func processFile(content: FileMessageContent,
|
||||
receiverId: String,
|
||||
senderId: String,
|
||||
roomId: String,
|
||||
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
|
||||
let notification = try await processCommon(receiverId: receiverId,
|
||||
senderId: senderId,
|
||||
roomId: roomId,
|
||||
mediaProvider: mediaProvider)
|
||||
notification.body = "📄 " + content.body
|
||||
|
||||
return notification
|
||||
}
|
||||
|
||||
private func processNotice(content: NoticeMessageContent,
|
||||
receiverId: String,
|
||||
senderId: String,
|
||||
roomId: String,
|
||||
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
|
||||
let notification = try await processCommon(receiverId: receiverId,
|
||||
senderId: senderId,
|
||||
roomId: roomId,
|
||||
mediaProvider: mediaProvider)
|
||||
notification.body = "❕ " + content.body
|
||||
|
||||
return notification
|
||||
}
|
||||
|
||||
private func processEmote(content: EmoteMessageContent,
|
||||
receiverId: String,
|
||||
senderId: String,
|
||||
roomId: String,
|
||||
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
|
||||
let notification = try await processCommon(receiverId: receiverId,
|
||||
senderId: senderId,
|
||||
roomId: roomId,
|
||||
mediaProvider: mediaProvider)
|
||||
notification.body = "🫥 " + content.body
|
||||
|
||||
return notification
|
||||
}
|
||||
|
||||
private func processAudio(content: AudioMessageContent,
|
||||
receiverId: String,
|
||||
senderId: String,
|
||||
roomId: String,
|
||||
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
|
||||
var notification = try await processCommon(receiverId: receiverId,
|
||||
senderId: senderId,
|
||||
roomId: roomId,
|
||||
mediaProvider: mediaProvider)
|
||||
notification.body = "🔊 " + content.body
|
||||
|
||||
notification = await notification.addMediaAttachment(using: mediaProvider,
|
||||
mediaSource: .init(source: content.source,
|
||||
mimeType: content.info?.mimetype))
|
||||
|
||||
return notification
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ class NotificationServiceProxy: NotificationServiceProxyProtocol {
|
||||
// service = NotificationService(basePath: basePath, userId: userId)
|
||||
}
|
||||
|
||||
func notificationItem(roomId: String, eventId: String) async throws -> NotificationItemProxy? {
|
||||
NotificationItemProxy()
|
||||
func notificationItem(roomId: String, eventId: String) async throws -> NotificationItemProxyProtocol? {
|
||||
MockNotificationItemProxy(eventID: eventId, roomID: roomId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,5 +17,5 @@
|
||||
import Foundation
|
||||
|
||||
protocol NotificationServiceProxyProtocol {
|
||||
func notificationItem(roomId: String, eventId: String) async throws -> NotificationItemProxy?
|
||||
func notificationItem(roomId: String, eventId: String) async throws -> NotificationItemProxyProtocol?
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user