Media Attachment in iOS 10 Push Notifications

情到浓时终转凉″ 提交于 2019-12-02 00:17:36

You have to pull the url out of the notification's user info, then download the image and give a file url to the attachment. Try something like:

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

I've managed to work this out with the following code:

Swift:

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

Node:

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

Thanks for your help!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!