gpt4 book ai didi

ios - UNNotificationServiceExtension 中的附件文件 URL 无效

转载 作者:行者123 更新时间:2023-11-28 15:09:09 25 4
gpt4 key购买 nike

我写了一个 UNNotificationServiceExtension,我试图在其中下载文件并附加它。每次我运行我都会得到一个异常说

Invalid attachment file URL.

我这辈子都想不通为什么会失败。

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
let bestAttemptContent = request.content.mutableCopy() as! UNMutableNotificationContent

guard let urlPath = request.content.userInfo["media-url"] as? String,
let url = URL(string: urlPath) else {
contentHandler(bestAttemptContent)
return
}

let semaphore = DispatchSemaphore(value: 0)

URLSession.shared.downloadTask(with: url) {
location, _, _ in

defer { semaphore.signal() }

guard let location = location else { return }

var destination: URL!

do {
destination = try FileManager.default.url(for: .itemReplacementDirectory,
in: .userDomainMask,
appropriateFor: location,
create: true)
destination.appendPathComponent(url.lastPathComponent)
let attachment = try UNNotificationAttachment(identifier: "",
url: destination)
bestAttemptContent.attachments = [attachment]
} catch let error {
bestAttemptContent.body = error.localizedDescription + "\n" + destination.absoluteString
}
}.resume()

_ = semaphore.wait(timeout: .distantFuture)

contentHandler(bestAttemptContent)
}

最佳答案

如果您仍在寻找代码,就在这里,

class NotificationService: UNNotificationServiceExtension {

var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {

self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

func failEarly() {
contentHandler(request.content)
}

guard let content = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
return failEarly()

}
guard let apnsData = content.userInfo["data"] as? [String: Any] else {
return failEarly()

}

guard let attachmentURL = apnsData["media-url"] as? String else {
return failEarly()

}

guard let imageData = NSData(contentsOf:NSURL(string: attachmentURL)! as URL) else {
return failEarly()

}

bestAttemptContent?.attachments = [create(imageFileIdentifier: "image.jpg", data: imageData, options: nil)!]
contentHandler(bestAttemptContent!)

}

override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}

}

func create(imageFileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {

let tmpSubFolderName: String = ProcessInfo.processInfo.globallyUniqueString
let fileURLPath: String = NSTemporaryDirectory()
let tmpSubFolderURL: String = URL(fileURLWithPath: fileURLPath).appendingPathComponent(tmpSubFolderName).absoluteString

if ((try? FileManager.default.createDirectory(atPath: tmpSubFolderURL, withIntermediateDirectories: true, attributes: nil)) != nil) {
let fileURL = URL(fileURLWithPath: tmpSubFolderURL).appendingPathComponent(imageFileIdentifier)
data.write(to: fileURL, atomically: true)
let attachment = try? UNNotificationAttachment(identifier: imageFileIdentifier, url: fileURL, options: options)
return attachment!
}
return nil

}

关于ios - UNNotificationServiceExtension 中的附件文件 URL 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47949857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com