首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用UNNotificationServiceExtension扩展APNS共享UNNotificationServiceExtension扩展中下载的数据的正确方法是什么?

用UNNotificationServiceExtension扩展APNS共享UNNotificationServiceExtension扩展中下载的数据的正确方法是什么?
EN

Stack Overflow用户
提问于 2019-04-01 15:25:18
回答 1查看 375关注 0票数 0

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

代码语言:javascript
复制
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提供了一个扩展,用于将数据保存到磁盘

代码语言:javascript
复制
@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
    }
}

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

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

代码语言:javascript
复制
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
        }
    }

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-02 11:23:36

试着这样做

代码语言:javascript
复制
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];
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55458599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档