gpt4 book ai didi

ios - 使用 UNNotificationContentExtension 扩展 APNS 共享 UNNotificationServiceExtension 扩展中下载的数据的正确方法是什么

转载 作者:行者123 更新时间:2023-11-28 13:45:21 26 4
gpt4 key购买 nike

我使用代码在 UNNotificationServiceExtension 中下载接收到通知的图像

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

if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
if let urlImageString = request.content.userInfo["image"] as? String, let url = URL(string: urlImageString) as? URL {
guard let imageData = NSData(contentsOf: url) else {
contentHandler(bestAttemptContent)
return
}

guard let attachment = UNNotificationAttachment.saveImageToDisk(fileIdentifier: "image.jpg", data: imageData, options: nil) else {
contentHandler(bestAttemptContent)
return
}

bestAttemptContent.attachments = [attachment]
}
contentHandler(bestAttemptContent)
}
}

我有一个 UNNotificationAttachment 的扩展来将数据保存到磁盘

@available(iOSApplicationExtension 10.0, *)
extension UNNotificationAttachment {

static func saveImageToDisk(fileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
if let dir = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "some.group.identifier")
{
let fileURL = dir.appendingPathComponent("image.jpg", isDirectory: false)
do {
try data.write(to: fileURL, options: .atomicWrite)
let attachment = try UNNotificationAttachment(identifier: "image.jpg", url: fileURL, options: options)
return attachment
}
catch {
return nil
}
}

return nil
}
}

我的应用程序已经使用应用程序组,所以我使用主应用程序创建的应用程序组之一,并与 UNNotificationServiceExtensionUNNotificationContentExtension 共享它

最后,我尝试使用 UNNotificationContentExtension 中的 url 读取数据

func didReceive(_ notification: UNNotification) {
let attachment = notification.request.content.attachments[0]
let fileURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "some.group.identifier")
let imageURL = fileURL?.appendingPathComponent("image.jpg")
if attachment.identifier == "image.jpg" {
let image = UIImage(contentsOfFile: attachment.url.absoluteString)
self.notificationImageView.image = image
}
}

我得到的图像为 nil,在尝试使用 FileManager.default.fileExists(atPath: imageURL!.path) 查找文件是否存在时,它总是返回 false。

我在这里做错了什么?请帮忙

最佳答案

这样试试

UNNotificationAttachment *attachment = [content.attachments objectAtIndex:0];

if (attachment.URL.startAccessingSecurityScopedResource) {
NSData *data = [NSData dataWithContentsOfURL:attachment.URL];
if (data) {
pagerView.imgViewProductImage.image = [UIImage imageWithData:data];
} else {
[attachment.URL stopAccessingSecurityScopedResource];
return [UIView new];
}
[attachment.URL stopAccessingSecurityScopedResource];
}

关于ios - 使用 UNNotificationContentExtension 扩展 APNS 共享 UNNotificationServiceExtension 扩展中下载的数据的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55458599/

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