0

我目前正在一个项目中工作,我们已经在应用程序的主要目标中设置了推送通知,现在我们想要添加丰富的推送功能。我在互联网上做了很多研究,没有在项目中添加新目标(通知服务扩展)的情况下找不到实现丰富推送通知的方法。

正如我在标题中所说,是否可以在不使用这个新目标的情况下将图像添加到推送通知中?

4

1 回答 1

0

技术上是的,但实际上不是。只要您的图像足够小,对图像进行 base64 编码之类的操作仍然可以使其适合有效负载,您就可以这样做。

但是,考虑到添加通知服务扩展是多么简单,您为什么要避免它呢?您的扩展程序看起来像这样:

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
  guard let urlPath = request.content.userInfo["media-url"] as? String,
    let url = URL(string: urlPath) else {
      return
  }

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

  defer { contentHandler(bestAttemptContent!) }

  let destination = URL(fileURLWithPath: NSTemporaryDirectory())
      .appendingPathComponent(url.lastPathComponent)

  do {
    let data = try Data(contentsOf: url)
    try data.write(to: destination)

    let attachment = try UNNotificationAttachment(identifier: "",
                                                  url: destination)
    bestAttemptContent!.attachments = [attachment]
  } catch {
  }
}

请明确注意,您需要在此方法中进行同步下载。

于 2018-03-24T16:31:18.310 回答