iOS 10推送通知中的媒体附件

时间:2016-09-08 19:53:03

标签: ios apple-push-notifications ios10

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

我添加了Notification Service Extension,并使用了以下代码:

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

3 个答案:

答案 0 :(得分:3)

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

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()
}  

答案 1 :(得分: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"
    } 
  }

感谢您的帮助!

答案 2 :(得分: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 {}

还有一些对debug extensions很有帮助的内容 和how to test extentions