【问题标题】:Using UNNotificationServiceExtension for rich remote notifications使用 UNNotificationServiceExtension 进行丰富的远程通知
【发布时间】:2016-11-22 05:25:06
【问题描述】:

我正在尝试在 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)
    }
}

}

【问题讨论】:

    标签: ios ios10 rich-notifications


    【解决方案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

    【讨论】:

    • 感谢您的回复@Rajat。我试过这个,但它仍然没有下载图像。仍然只显示文本数据。我不知道我做错了什么
    • 检查文件是否正在下载,同时检查你是否从通知数据中获取了有效的url
    • 是的,该网址有效。但是它们将存储在什么位置?我浏览了照片,没有。
    • 您将图像保存在 NSTemporaryDirectory 临时目录中,而不是照片上
    • 是的,但是我应该在哪个文件夹中搜索图像。如果我听起来很傻,我很抱歉。
    【解决方案2】:

    终于开始工作了。这里的问题是我必须添加

    NSAppTransportSecurity

    扩展 plist 中的标记。添加此标签后,它开始显示图像。希望它可以帮助某人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 2017-03-01
      • 1970-01-01
      相关资源
      最近更新 更多