firebase - 如何使用 react-native-firebase 在 iOS 通知中添加自定义图像
问题描述
我想在 firebase 云消息传递中使用数据有效负载在通知中显示图像。图像被指定为托管图像的网站的 URL。
看来我想做的是将图像添加为附件,请参见下面的第 8 行。然而,除了应用程序图标之外,没有图像存在。
const notification = new firebase.notifications.Notification()
.setNotificationId("notification_id")
.setTitle(notification.data.title)
.setBody(notification.data.body)
.setData({ url: notification.data.url })
.ios.setLaunchImage(notification.data.icon)
.android.setBigPicture(notification.data.icon)
.ios.addAttachment("some_id", notification.data.icon, {});
问题是没有可以帮助我的错误消息。通知按预期显示标题和正文,但不存在图像。从我可以阅读的文档中,我想做的事情是可能的。
解决方案
简短的回答是 iOS 上的 react-native 不支持“丰富的推送通知”,即带有图像的通知。
更长的答案是,如果您添加一点 swift 代码,那么向 react-native 项目添加对图像的支持是相当简单的。
解决方法:
打开您的 xcode 项目并转到“编辑器”->“添加目标...”。选择名为“通知服务扩展”的“应用程序扩展”。
您可以随意命名它,但如果您使用的是 CocoaPods,请确保选择了正确的项目。
创建后,将其内容替换为override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void)
:
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
// get the variables that is needed later.
guard let bestAttemptContent = bestAttemptContent,
let attachmentURLAsString = bestAttemptContent.userInfo["icon"] as? String,
// "icon" is the key for the image url in the notification. It
// could be named whatever you want.
let attachmentURL = URL(string: attachmentURLAsString) else {
return
}
// call a custom function to download the image before attaching
// it to the notification and presenting it.
downloadImageFrom(url: attachmentURL) { (attachment) in
if let attachment = attachment {
bestAttemptContent.attachments = [attachment]
contentHandler(bestAttemptContent)
}
}
然后downloadImageFrom
需要创建函数:
private func downloadImageFrom(url: URL, with completionHandler: @escaping (UNNotificationAttachment?) -> Void) {
let task = URLSession.shared.downloadTask(with: url) { (downloadedUrl, response, error) in
//verify that a url exists.
guard let downloadedUrl = downloadedUrl else {
completionHandler(nil)
return
}
// create a local unique filepath.
var urlPath = URL(fileURLWithPath: NSTemporaryDirectory())
let uniqueURLEnding = ProcessInfo.processInfo.globallyUniqueString + ".png"
urlPath = urlPath.appendingPathComponent(uniqueURLEnding)
// fetch the image from the url
try? FileManager.default.moveItem(at: downloadedUrl, to: urlPath)
// if successful, return the image as an attachment.
do {
let attachment = try UNNotificationAttachment(identifier: "picture", url: urlPath, options: nil)
completionHandler(attachment)
} catch {
completionHandler(nil)
}
}
task.resume()
}
当您构建应用程序时,它将使用此代码来加载通知。
发送通知时,您必须记住包含“图标”值。发送通知所需的示例:
"notification": {
"body": "body",
"title": "title"
"mutable_content": true // this row is required for the notification to work!
},
"data": {
"icon":"https://pusher.com/static_logos/320x320.png", // change to your image url.
},
推荐阅读
- angular - 如何使用角度获取对象键?
- discord.py - 有没有办法在python中用一个名字写两个值
- javascript - 检查循环值以查看是否匹配?
- java - 我有一个 12 小时格式的时间值(hh:mm:ss a)。现在我想用给定的语言本地化“a”(AM/PM)的值
- sql - SQL如何在不匹配第22个等的情况下搜索第2个文本字段?
- sql - 如何从查询窗口将 unicode 文本更新到 SQL Server
- flutter - 我被火库困住了
- javascript - 如何格式化jquery数据表中的日期列
- android - 如何从我的应用程序中删除 READ_EXTERNAL_STORAGE 权限?
- node.js - 弹性搜索更新不返回任何 node.js 的结果