From da0dcbfe1c3b5527e6675b61a3034529a2dfb727 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Sun, 8 Jan 2023 11:13:17 +0100 Subject: [PATCH] Handle image in push notifications --- .../NotificationService.swift | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/IceCubesNotifications/NotificationService.swift b/IceCubesNotifications/NotificationService.swift index 24ab73e0..32b5a7a9 100644 --- a/IceCubesNotifications/NotificationService.swift +++ b/IceCubesNotifications/NotificationService.swift @@ -3,6 +3,7 @@ import KeychainSwift import Env import CryptoKit import Models +import UIKit @MainActor class NotificationService: UNNotificationServiceExtension { @@ -24,7 +25,7 @@ class NotificationService: UNNotificationServiceExtension { contentHandler(bestAttemptContent) return } - + guard let encodedPublicKey = bestAttemptContent.userInfo["k"] as? String, let publicKeyData = Data(base64Encoded: encodedPublicKey.URLSafeBase64ToBase64()), let publicKey = try? P256.KeyAgreement.PublicKey(x963Representation: publicKeyData) else { @@ -53,7 +54,29 @@ class NotificationService: UNNotificationServiceExtension { bestAttemptContent.body = notification.body.escape() bestAttemptContent.userInfo["plaintext"] = plaintextData - contentHandler(bestAttemptContent) + if let urlString = notification.icon, + let url = URL(string: urlString) { + let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("notification-attachments") + try? FileManager.default.createDirectory(at: temporaryDirectoryURL, withIntermediateDirectories: true, attributes: nil) + let filename = url.lastPathComponent + let fileURL = temporaryDirectoryURL.appendingPathComponent(filename) + + Task { + if let (data, _) = try? await URLSession.shared.data(for: .init(url: url)) { + if let image = UIImage(data: data) { + try? image.pngData()?.write(to: fileURL) + if let attachment = try? UNNotificationAttachment(identifier: filename, url: fileURL, options: nil) { + bestAttemptContent.attachments = [attachment] + } + } + contentHandler(bestAttemptContent) + } else { + contentHandler(bestAttemptContent) + } + } + } else { + contentHandler(bestAttemptContent) + } } }