与UNNotificationContentExtension扩展APNS共享在UNNotificationServiceExtension扩展中下载的数据的正确方法是什么

时间:2019-04-01 15:25:19

标签: ios swift

我使用代码在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共享它

最后,我尝试使用{p>来从UNNotificationContentExtension中的网址读取数据

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。

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

1 个答案:

答案 0 :(得分:1)

以这种方式尝试

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];