diff --git a/ios/SolianNotificationService/NotificationService.swift b/ios/SolianNotificationService/NotificationService.swift index 0b70c8b2..124f3856 100644 --- a/ios/SolianNotificationService/NotificationService.swift +++ b/ios/SolianNotificationService/NotificationService.swift @@ -9,6 +9,7 @@ import UserNotifications import Intents import Kingfisher import UniformTypeIdentifiers +import KingfisherWebP enum ParseNotificationPayloadError: Error { case missingMetadata(String) @@ -24,6 +25,11 @@ class NotificationService: UNNotificationServiceExtension { _ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void ) { + KingfisherManager.shared.defaultOptions += [ + .processor(WebPProcessor.default), + .cacheSerializer(WebPSerializer.default) + ] + self.contentHandler = contentHandler guard let bestAttemptContent = request.content.mutableCopy() as? UNMutableNotificationContent else { contentHandler(request.content) @@ -64,40 +70,12 @@ class NotificationService: UNNotificationServiceExtension { let handle = INPersonHandle(value: "\(metaCopy["user_id"] ?? "")", type: .unknown) - if let pfpUrl = pfpUrl, let url = URL(string: pfpUrl) { - let targetSize = 512 - let scaleProcessor = ResizingImageProcessor(referenceSize: CGSize(width: targetSize, height: targetSize), mode: .aspectFit) - - KingfisherManager.shared.retrieveImage(with: url, options: [.processor(scaleProcessor)], completionHandler: { result in - var image: Data? - switch result { - case .success(let value): - image = value.image.pngData() - case .failure(let error): - print("Unable to get pfp url: \(error)") - } - - let sender = INPerson( - personHandle: handle, - nameComponents: PersonNameComponents(nickname: "\(metaCopy["sender_name"] ?? "")"), - displayName: content.title, - image: image == nil ? nil : INImage(imageData: image!), - contactIdentifier: nil, - customIdentifier: nil - ) - - let intent = self.createMessageIntent(with: sender, meta: metaCopy, body: content.body) - self.donateInteraction(for: intent) - - content.categoryIdentifier = "CHAT_MESSAGE" - self.contentHandler?(content) - }) - } else { + let completeNotificationProcessing: (Data?) -> Void = { imageData in let sender = INPerson( personHandle: handle, nameComponents: PersonNameComponents(nickname: "\(metaCopy["sender_name"] ?? "")"), displayName: content.title, - image: nil, + image: imageData == nil ? nil : INImage(imageData: imageData!), contactIdentifier: nil, customIdentifier: nil ) @@ -105,8 +83,37 @@ class NotificationService: UNNotificationServiceExtension { let intent = self.createMessageIntent(with: sender, meta: metaCopy, body: content.body) self.donateInteraction(for: intent) - content.categoryIdentifier = "CHAT_MESSAGE" - self.contentHandler?(content) + if let updatedContent = try? request.content.updating(from: intent) { + if let mutableContent = updatedContent.mutableCopy() as? UNMutableNotificationContent { + mutableContent.categoryIdentifier = "CHAT_MESSAGE" + self.contentHandler?(mutableContent) + } else { + self.contentHandler?(updatedContent) + } + } else { + content.categoryIdentifier = "CHAT_MESSAGE" + self.contentHandler?(content) + } + } + + if let pfpUrl = pfpUrl, let url = URL(string: pfpUrl) { + let targetSize = 512 + let scaleProcessor = ResizingImageProcessor(referenceSize: CGSize(width: targetSize, height: targetSize), mode: .aspectFit) + + KingfisherManager.shared.retrieveImage(with: url, options: [ + .processor(scaleProcessor) + ], completionHandler: { result in + var image: Data? + switch result { + case .success(let value): + image = value.image.pngData() + case .failure(let error): + print("Unable to get pfp url: \(error)") + } + completeNotificationProcessing(image) + }) + } else { + completeNotificationProcessing(nil) } }