使用UNNotificationServiceExtension进行丰富的远程通知

时间:2016-11-22 05:25:06

标签: ios ios10 rich-notifications

我正在尝试在ios 10中实现丰富的远程通知。我已经实现了这段代码。接收通知后的控件正在此处,但我不知道如何下载图像并显示在通知中。提前致谢。

class NotificationService: UNNotificationServiceExtension {

var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?

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

    if let bestAttemptContent = bestAttemptContent {
        // Modify the notification content here...
        //print("title for image = \(bestAttemptContent.title)")
        bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"

        contentHandler(bestAttemptContent)
    }

}



override func serviceExtensionTimeWillExpire() {
    // Called just before the extension will be terminated by the system.
    // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
    if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
        contentHandler(bestAttemptContent)
    }
}

}

2 个答案:

答案 0 :(得分:1)

你会在notificationData这样的

中获得附件
"attachment-url": "https://yourimage.png"

这就是你如何使用它

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

        // Get the custom data from the notification payload
        if let notificationData = request.content.userInfo["data"] as? [String: String] {
            // Grab the attachment
            if let urlString = notificationData["attachment-url"], let fileUrl = URL(string: urlString) {
                // Download the attachment
                URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
                    if let location = location {
                        // Move temporary file to remove .tmp extension
                        let tmpDirectory = NSTemporaryDirectory()
                        let tmpFile = "file://".appending(tmpDirectory).appending(fileUrl.lastPathComponent)
                        let tmpUrl = URL(string: tmpFile)!
                        try! FileManager.default.moveItem(at: location, to: tmpUrl)

                        // Add the attachment to the notification content
                        if let attachment = try? UNNotificationAttachment(identifier: "", url: tmpUrl) {
                            self.bestAttemptContent?.attachments = [attachment]
                        }
                    }
                    // Serve the notification content
                    self.contentHandler!(self.bestAttemptContent!)
                    }.resume()
            }
        }

引自here

答案 1 :(得分:0)

最后它的工作。这里的问题是我必须添加

  

NSAppTransportSecurity

扩展名plist中的

标记。添加此标记后,它开始显示图像。希望它可以帮到某人。