🐛 Trying to fix NSE attachment doesn't work as expected on the default types

This commit is contained in:
2026-01-08 23:10:22 +08:00
parent 9cba568e47
commit 64c5ee0fe9

View File

@@ -5,7 +5,7 @@
// Created by LittleSheep on 2025/5/31. // Created by LittleSheep on 2025/5/31.
// //
import UserNotifications @preconcurrency import UserNotifications
import Intents import Intents
import Kingfisher import Kingfisher
import UniformTypeIdentifiers import UniformTypeIdentifiers
@@ -149,7 +149,7 @@ class NotificationService: UNNotificationServiceExtension {
let dispatchGroup = DispatchGroup() let dispatchGroup = DispatchGroup()
var attachments: [UNNotificationAttachment] = [] var attachments: [UNNotificationAttachment] = []
let lock = NSLock() // To synchronize access to the attachments array let queue = DispatchQueue(label: "dev.solsynth.solian.nse")
for attachmentUrl in attachmentUrls { for attachmentUrl in attachmentUrls {
guard let remoteUrl = URL(string: attachmentUrl) else { guard let remoteUrl = URL(string: attachmentUrl) else {
@@ -163,7 +163,7 @@ class NotificationService: UNNotificationServiceExtension {
.processor(scaleProcessor) .processor(scaleProcessor)
] : nil) { [weak self] result in ] : nil) { [weak self] result in
defer { dispatchGroup.leave() } defer { dispatchGroup.leave() }
guard let self = self else { return } guard self != nil else { return }
switch result { switch result {
case .success(let retrievalResult): case .success(let retrievalResult):
@@ -176,12 +176,12 @@ class NotificationService: UNNotificationServiceExtension {
try retrievalResult.image.pngData()?.write(to: cachedFileUrl) try retrievalResult.image.pngData()?.write(to: cachedFileUrl)
if let attachment = try? UNNotificationAttachment(identifier: attachmentUrl, url: cachedFileUrl, options: [ if let attachment = try? UNNotificationAttachment(identifier: attachmentUrl, url: cachedFileUrl, options: [
UNNotificationAttachmentOptionsTypeHintKey: type?.identifier as Any, UNNotificationAttachmentOptionsTypeHintKey: UTType.png.identifier,
UNNotificationAttachmentOptionsThumbnailHiddenKey: 0, UNNotificationAttachmentOptionsThumbnailHiddenKey: 0,
]) { ]) {
lock.lock() queue.async {
attachments.append(attachment) attachments.append(attachment)
lock.unlock() }
} }
} catch { } catch {
print("Failed to write media to temporary file: \(error.localizedDescription)") print("Failed to write media to temporary file: \(error.localizedDescription)")