ios - 如何使用 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 控制台发送图像。我不知道如何找出问题或解决问题。
解决方案
必须通过添加断点来检查您的问题,并且当应用程序收到通知时,逐行检查变量,直到您看到不正确的内容。
我正在分享对我来说完美的代码。
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
} }
推荐阅读
- google-cloud-platform - 我可以从同一个 windows 2012 服务器同时使用多个 gcloud 帐户吗?
- mysql - RDS InnoDB RAM 使用问题
- python - AttributeError:“‘numpy.ndarray’对象没有属性‘plot’。”
- google-sheets - 使用 Query & Importrange 在 2 个日期和 if 条件之间过滤以根据条件更改日期的值
- c# - 无法使用 System.Net.Mail 类使用内部电子邮件服务器发送邮件
- android - Android:将触摸传递给浮动视图下的视图(另一个应用程序)
- reactjs - 从子文件夹提供 React 应用程序时出错
- c# - C# - Selenium 无法从 WebDriver 服务中检索 SessionId
- python - 我的神经网络没有学习成本没有改变
- css - 如何在将前两个单词向左移动时保持正文居中