Каков правильный способ поделиться данными, загруженными в расширении UNNotificationServiceExtension, с APNS расширения UNNotificationContentExtension

#ios #swift

#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
    }
}
  

Мое приложение уже использует Appgroups, поэтому я использую одну из групп приложений, созданных основным приложением, и предоставляю ей общий доступ как к UNNotificationServiceExtension , так и UNNotificationContentExtension

Наконец, я пытаюсь прочитать данные из URL в 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:

Попробуйте таким образом

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

Комментарии:

1. Конечно. Дайте мне знать, если будет какая-либо другая помощь. Спасибо

2. Потрясающий ответ, чувак. Я даже не знал об этом API, написал его и работал как шарм. Следовательно 1. P.S: Если возможно, обновите ответ в Swift, поскольку мой вопрос в swift, кто-нибудь в будущем придет сюда, высока вероятность, что они будут ожидать ответа в swift