UNNotificationServiceExtension

UNNotificationServiceExtension

我在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
    }
}

我的应用程序已使用Appgroups,因此我使用主应用程序创建的应用程序组之一,并与UNNotificationServiceExtensionUNNotificationContentExtension共享它
最后,我尝试使用
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
        }
    }

我得到的图像为零,试图找到文件是否存在使用UNNotificationContentExtension它总是返回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];
}

10-08 07:44