3

我正在努力在 iOS 10 中向我的推送通知中添加图像。

我添加了一个通知服务扩展,并使用了以下代码:

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


    if let urlString = request.content.userInfo["image-url"] as? String, let fileUrl = URL(string: urlString) {
        URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
            if let location = location {
                let options = [UNNotificationAttachmentOptionsTypeHintKey: kUTTypePNG]
                if let attachment = try? UNNotificationAttachment(identifier: "", url: location, options: options) {
                    self.bestAttemptContent?.attachments = [attachment]
                }
            }
            self.contentHandler!(self.bestAttemptContent!)
            }.resume()
    }
}

我从下面的第一个答案中得到了这个代码。

我现在遇到的问题是收到通知,有短暂的延迟,表明必须进行下载,但没有显示附件。

我假设serviceExtensionTimeWillExpire()正在调用它并且只是显示bestAttempt

任何帮助是极大的赞赏。

我的 APNs 有效负载配置正确,我相信:

apns: {
  aps: { 
    alert: { 
      title: "Title", 
      subtitle: "Subtitle", 
      body: "Body"
    }, 
    "mutable-content": 1
  },
  "image-url": "https://helloworld.com/image.png" 
}
4

3 回答 3

4

您必须从通知的用户信息中提取 url,然后下载图像并将文件 url 提供给附件。尝试类似:

if let urlString = request.content.userInfo["image-url"] as? String, let fileUrl = URL(string: urlString) {
    URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
        if let location = location {
            let options = [UNNotificationAttachmentOptionsTypeHintKey: kUTTypePNG]
            if let attachment = try? UNNotificationAttachment(identifier: "", url: location, options: options) {
                self.bestAttemptContent.attachments = [attachment]
            }
        }

        self.contentHandler(self.bestAttemptContent)
    }.resume()
}  
于 2016-09-08T20:02:59.803 回答
0

另一种解决方案(和可测试的解决方案)可能是将图像写入临时位置:

    // NotificationRequestHandler
    func getImageURL(from userInfo: [AnyHashable: Any]) throws -> URL {
        // parse the image URL and return it, otherwise throws and error
    }

    func temporaryWriteData(from url: URL) throws -> (String, URL) {
        let temporaryDirectoryUrl = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
        let temporaryFileName = UUID().uuidString
        let temporaryFileUrl = temporaryDirectoryUrl.appendingPathComponent(temporaryFileName)

        let data = try Data(contentsOf: url)
        try data.write(to: temporaryFileUrl, options: .atomic)
        return (temporaryFileName, temporaryFileUrl)
    }

和上didReceive(_:withContentHandler:)

    self.contentHandler = contentHandler
    bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

    defer {
        contentHandler(bestAttemptContent ?? request.content)
    }

    do {
        let handler = NotificationRequestHandler()
        let imageUrl = try handler.getImageURL(from: request.content.userInfo)
        let (imageFileName, imageFileUrl) = try handler.temporaryWriteData(from: imageUrl)
        let attachment = try UNNotificationAttachment(identifier: imageFileName, url: imageFileUrl, options: [UNNotificationAttachmentOptionsTypeHintKey: "public.png"])
        bestAttemptContent?.attachments = [attachment]
    } catch {}

对于调试扩展 以及如何测试扩展也很有帮助

于 2019-10-24T10:01:07.017 回答
0

我已经设法使用以下代码解决了这个问题:

迅速:

if let PusherNotificationData = request.content.userInfo["data"] as? NSDictionary {
            if let urlString = PusherNotificationData["image-url"] as? String, let fileUrl = URL(string: urlString) {
                URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
                    if let location = location {
                        let options = [UNNotificationAttachmentOptionsTypeHintKey: kUTTypePNG]
                        if let attachment = try? UNNotificationAttachment(identifier: "", url: location, options: options) {
                            self.bestAttemptContent?.attachments = [attachment]
                        }
                    }

                    self.contentHandler!(self.bestAttemptContent!)
                    }.resume()
            }
        }

节点:

apns: {
    aps: { 
      alert: { 
        title: "title", 
        subtitle: "subtitle", 
        body: "body"
        }, 
        "mutable-content": 1,
        category: "test"
      },
    data: {
      "image-url": "www.helloworld.com/image.png"
    } 
  }

谢谢你的帮助!

于 2016-09-12T14:51:53.627 回答