iOS 10带图片的远程通知

iOS 10带图片的远程通知,ios,swift3,rich-notifications,Ios,Swift3,Rich Notifications,首先,我是Swift的新开发人员,我有一个客观的C背景,但我主要是一个Android开发人员 实际上,我正在用Swift 3和Xcode 8开发一个应用程序。我需要在这个应用程序中添加苹果新的富推送通知系统 我已经能够添加一个带有图片、视频和GIF的本地富通知示例。但是,我需要在典型的Internet服务器上显示带有PIC的远程通知 要启动本地通知,我使用以下代码: @IBAction func launchPicNotification(sender: UIButton) { let

首先,我是Swift的新开发人员,我有一个客观的C背景,但我主要是一个Android开发人员

实际上,我正在用Swift 3和Xcode 8开发一个应用程序。我需要在这个应用程序中添加苹果新的富推送通知系统

我已经能够添加一个带有图片、视频和GIF的本地富通知示例。但是,我需要在典型的Internet服务器上显示带有PIC的远程通知

要启动本地通知,我使用以下代码:

@IBAction func launchPicNotification(sender: UIButton) {
    let content = UNMutableNotificationContent()
    content.title = "Title"
    content.body = "Body"
    content.sound = UNNotificationSound.default()

    let url = Bundle.main.url(forResource:"foto", withExtension: "jpg")

    let attachment = try? UNNotificationAttachment(identifier: "Notification",
                                                   url: url!,
                                                   options: [:])

    if let attachment = attachment {
        print("Yes")
        content.attachments.append(attachment)
    }else{
        print("No")
    }

    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 10.0, repeats: false)
    let request = UNNotificationRequest(identifier:"identificador", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request){(error) in

        if (error != nil){

            //handle here

        }

    }
}
我需要加载一个远程jpg文件作为附加图像。有人知道如何加载远程文件而不是加载本地图片吗


谢谢

我为UIImage添加了一个扩展来处理这个问题。从下载的数据创建UIImage后,可以调用此函数创建本地URL

extension UIImage {

func createLocalURL() -> URL? {

    guard let data = UIImagePNGRepresentation(self) else {
        print("Coule not get UIImagePNGRepresentation Data for photo")
        return nil
    }

    let localUrl = self.getDocumentsDirectory().appendingPathComponent("copy.png")

    do {
        try data.write(to: localUrl)
    } catch let error {
        print("Failed to write to URL")
        print(error)
    }
    return localUrl
}

}

让url=url(字符串:https://.....jpg”?或者url必须指向本地文件。如果是这样的话,我想你需要先下载这个文件。我已经试过了,也许我需要像你告诉我的那样先下载。你能给我推荐一个在Swift 3上下载图片的好教程吗?谢谢。@gabicuesta您需要先下载文件,然后才能在通知中显示图像。。