Support notifications with multiple attachments

This commit is contained in:
LittleSheep 2025-02-18 00:07:20 +08:00
parent 54a59aa470
commit 04875eb164

View File

@ -123,48 +123,59 @@ class NotificationService: UNNotificationServiceExtension {
} }
if let imageIdentifier = metadata["image"] as? String { if let imageIdentifier = metadata["image"] as? String {
attachMedia(to: content, withIdentifier: imageIdentifier, fileType: UTType.jpeg, doScaleDown: true) attachMedia(to: content, withIdentifier: [imageIdentifier], fileType: UTType.jpeg, doScaleDown: true)
} else if let avatarIdentifier = metadata["avatar"] as? String { } else if let avatarIdentifier = metadata["avatar"] as? String {
attachMedia(to: content, withIdentifier: avatarIdentifier, fileType: UTType.jpeg, doScaleDown: true) attachMedia(to: content, withIdentifier: [avatarIdentifier], fileType: UTType.jpeg, doScaleDown: true)
} else if let imagesIdentifier = metadata["images"] as? Array<String> {
attachMedia(to: content, withIdentifier: imagesIdentifier, fileType: UTType.jpeg, doScaleDown: true)
} else { } else {
contentHandler?(content) contentHandler?(content)
} }
} }
private func attachMedia(to content: UNMutableNotificationContent, withIdentifier identifier: String, fileType type: UTType?, doScaleDown scaleDown: Bool = false) { private func attachMedia(to content: UNMutableNotificationContent, withIdentifier identifier: Array<String>, fileType type: UTType?, doScaleDown scaleDown: Bool = false) {
let attachmentUrl = getAttachmentUrl(for: identifier) let attachmentUrls = identifier.compactMap { element in
return getAttachmentUrl(for: element)
guard let remoteUrl = URL(string: attachmentUrl) else { }
print("Invalid URL for attachment: \(attachmentUrl)")
guard !attachmentUrls.isEmpty else {
print("Invalid URLs for attachments: \(attachmentUrls)")
return return
} }
let targetSize = 800 let targetSize = 800
let scaleProcessor = ResizingImageProcessor(referenceSize: CGSize(width: targetSize, height: targetSize), mode: .aspectFit) let scaleProcessor = ResizingImageProcessor(referenceSize: CGSize(width: targetSize, height: targetSize), mode: .aspectFit)
KingfisherManager.shared.retrieveImage(with: remoteUrl, options: scaleDown ? [ for attachmentUrl in attachmentUrls {
.processor(scaleProcessor) guard let remoteUrl = URL(string: attachmentUrl) else {
] : nil) { [weak self] result in print("Invalid URL for attachment: \(attachmentUrl)")
guard let self = self else { return } continue // Skip this URL and move to the next one
}
switch result {
case .success(let retrievalResult): KingfisherManager.shared.retrieveImage(with: remoteUrl, options: scaleDown ? [
// The image is either retrieved from cache or downloaded .processor(scaleProcessor)
let tempDirectory = FileManager.default.temporaryDirectory ] : nil) { [weak self] result in
let cachedFileUrl = tempDirectory.appendingPathComponent(identifier) guard let self = self else { return }
do { switch result {
// Write the image data to a temporary file for UNNotificationAttachment case .success(let retrievalResult):
try retrievalResult.image.pngData()?.write(to: cachedFileUrl) // The image is either retrieved from cache or downloaded
self.attachLocalMedia(to: content, fileType: type?.identifier, from: cachedFileUrl, withIdentifier: identifier) let tempDirectory = FileManager.default.temporaryDirectory
} catch { let cachedFileUrl = tempDirectory.appendingPathComponent(UUID().uuidString) // Unique identifier for each file
print("Failed to write media to temporary file: \(error.localizedDescription)")
do {
// Write the image data to a temporary file for UNNotificationAttachment
try retrievalResult.image.pngData()?.write(to: cachedFileUrl)
self.attachLocalMedia(to: content, fileType: type?.identifier, from: cachedFileUrl, withIdentifier: attachmentUrl)
} catch {
print("Failed to write media to temporary file: \(error.localizedDescription)")
self.contentHandler?(content)
}
case .failure(let error):
print("Failed to retrieve image: \(error.localizedDescription)")
self.contentHandler?(content) self.contentHandler?(content)
} }
case .failure(let error):
print("Failed to retrieve image: \(error.localizedDescription)")
self.contentHandler?(content)
} }
} }
} }