首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用UNNotificationServiceExtension进行富远程通知

使用UNNotificationServiceExtension进行富远程通知
EN

Stack Overflow用户
提问于 2016-11-22 05:25:06
回答 2查看 1.2K关注 0票数 1

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

代码语言:javascript
复制
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)
    }
}

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-22 05:43:10

您将在您的notificationData中获得如下附件

代码语言:javascript
复制
"attachment-url": "https://yourimage.png"

这就是你可以使用它的方法

代码语言:javascript
复制
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()
            }
        }

引用自这里

票数 1
EN

Stack Overflow用户

发布于 2016-11-22 09:31:05

终于起作用了。这里的问题是我必须补充

NSAppTransportSecurity

在扩展的plist中标记。添加此标记后,它开始显示图像。希望它能帮到别人。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40734356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档