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
@@ -36,8 +36,8 @@ extension FileManager {
try createDirectory(at: url, withIntermediateDirectories: withIntermediateDirectories)
}
func copyFileToTemporaryDirectory(file url: URL) throws -> URL {
let newURL = URL.temporaryDirectory.appendingPathComponent(url.lastPathComponent)
func copyFileToTemporaryDirectory(file url: URL, with filename: String? = nil) throws -> URL {
let newURL = URL.temporaryDirectory.appendingPathComponent(filename ?? url.lastPathComponent)
try? removeItem(at: newURL)
try copyItem(at: url, to: newURL)
@@ -15,6 +15,7 @@
//
import Foundation
import Intents
import UserNotifications
extension UNNotificationContent {
@@ -32,4 +33,94 @@ extension UNMutableNotificationContent {
userInfo[NotificationConstants.UserInfoKey.receiverIdentifier] = newValue
}
}
func addMediaAttachment(using mediaProvider: MediaProviderProtocol?,
mediaSource: MediaSourceProxy) async -> UNMutableNotificationContent {
guard let mediaProvider else {
return self
}
switch await mediaProvider.loadFileFromSource(mediaSource) {
case .success(let file):
do {
let identifier = ProcessInfo.processInfo.globallyUniqueString
let newURL = try FileManager.default.copyFileToTemporaryDirectory(file: file.url, with: "\(identifier).\(file.url.pathExtension)")
let attachment = try UNNotificationAttachment(identifier: identifier,
url: newURL,
options: nil)
attachments.append(attachment)
} catch {
MXLog.error("Couldn't add media attachment:: \(error)")
return self
}
case .failure(let error):
MXLog.error("Couldn't load the file for media attachment: \(error)")
}
return self
}
// swiftlint:disable:next function_parameter_count
func addSenderIcon(using mediaProvider: MediaProviderProtocol?,
senderId: String,
receiverId: String,
senderName: String,
groupName: String?,
mediaSource: MediaSourceProxy?,
roomId: String) async throws -> UNMutableNotificationContent {
var image: INImage?
if let mediaSource {
switch await mediaProvider?.loadFileFromSource(mediaSource) {
case .success(let mediaFile):
image = try INImage(imageData: Data(contentsOf: mediaFile.url))
case .failure(let error):
MXLog.error("Couldn't add sender icon: \(error)")
case .none:
break
}
}
let senderHandle = INPersonHandle(value: senderId, type: .unknown)
let sender = INPerson(personHandle: senderHandle,
nameComponents: nil,
displayName: senderName,
image: image,
contactIdentifier: nil,
customIdentifier: nil)
// These are required to show the group name as subtitle
var speakableGroupName: INSpeakableString?
var recipients: [INPerson]?
if let groupName {
let meHandle = INPersonHandle(value: receiverId, type: .unknown)
let me = INPerson(personHandle: meHandle, nameComponents: nil, displayName: nil, image: nil, contactIdentifier: nil, customIdentifier: nil, isMe: true)
speakableGroupName = INSpeakableString(spokenPhrase: groupName)
recipients = [sender, me]
}
let intent = INSendMessageIntent(recipients: recipients,
outgoingMessageType: .outgoingMessageText,
content: nil,
speakableGroupName: speakableGroupName,
conversationIdentifier: roomId,
serviceName: nil,
sender: sender,
attachments: nil)
intent.setImage(image, forParameterNamed: \.conversationIdentifier)
// Use the intent to initialize the interaction.
let interaction = INInteraction(intent: intent, response: nil)
// Interaction direction is incoming because the user is
// receiving this message.
interaction.direction = .incoming
// Donate the interaction before updating notification content.
try await interaction.donate()
// Update notification content before displaying the
// communication notification.
let updatedContent = try updating(from: intent)
// swiftlint:disable:next force_cast
return updatedContent.mutableCopy() as! UNMutableNotificationContent
}
}