首页 > 解决方案 > 如何使用 Firebase 云消息设置图像?

问题描述

我正在尝试通过 Firebase 云消息发送推送通知,并成功发送仅针对文本的推送通知,但在发送带有图像的通知时遇到问题。我尝试了一些来自 medium 和 raywenderlich 的文档,但无法发送带有图像的通知。我在我的项目中添加了通知扩展并尝试了这段代码:

    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)

    guard let bestAttemptContent = bestAttemptContent,
    let attachmentURLAsString = bestAttemptContent.userInfo["image"] as? String,
    let attachmentURL = URL(string: attachmentURLAsString) else{
        return
    }
    downloadImageFrom(url: attachmentURL) { (attachment) in
        if let attachment = attachment {
            bestAttemptContent.attachments = [attachment]
            contentHandler(bestAttemptContent)
        }
    }

}

private func downloadImageFrom(url: URL, with completionHandler: @escaping (UNNotificationAttachment?) -> Void){
    let task = URLSession.shared.downloadTask(with: url) { (downloadedUrl, response, error) in
        guard let downloadUrl = downloadedUrl else{
            completionHandler(nil)
            return
        }
        var urlPath = URL(fileURLWithPath: NSTemporaryDirectory())
        let uniqueURLEnding = ProcessInfo.processInfo.globallyUniqueString + ".jpg"
        urlPath = urlPath.appendingPathComponent(uniqueURLEnding)
        try? FileManager.default.moveItem(at: downloadUrl, to: urlPath)
        do{
            let attachment = try UNNotificationAttachment(identifier: "picture", url: urlPath, options: nil)
            completionHandler(attachment)
        }catch{
            completionHandler(nil)
        }
    }
    task.resume()
}

但仍然没有显示图像,请注意:我正在尝试从 firebase 控制台发送图像。我不知道如何找出问题或解决问题。

标签: iosswiftfirebasefirebase-cloud-messaging

解决方案


必须通过添加断点来检查您的问题,并且当应用程序收到通知时,逐行检查变量,直到您看到不正确的内容。

我正在分享对我来说完美的代码。

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

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

并为UNNotificationAttachment

extension UNNotificationAttachment {
static func saveImageToDisk(fileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
    let fileManager = FileManager.default
    let folderName = ProcessInfo.processInfo.globallyUniqueString
    let folderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(folderName, isDirectory: true)
    do {
        try fileManager.createDirectory(at: folderURL!, withIntermediateDirectories: true, attributes: nil)
        let fileURL = folderURL?.appendingPathComponent(fileIdentifier)
        try data.write(to: fileURL!, options: [])
        let attachment = try UNNotificationAttachment(identifier: fileIdentifier, url: fileURL!, options: options)
        return attachment
    } catch let error {
        print("error \(error)")
    }
    
    return nil
} }

推荐阅读