Swift iOS/FCM-如何使用有效负载中的图像名称将XCAsset文件夹中的图像显示为通知附件?
我正在为通过FCM发送的天气应用程序编写丰富的通知 首先,我想通知您,我的通知已通过APNS和FCM成功发送和接收 我使用和来测试一切 上下文 这是我通过邮递员发送的有效载荷Swift iOS/FCM-如何使用有效负载中的图像名称将XCAsset文件夹中的图像显示为通知附件?,swift,firebase,firebase-cloud-messaging,remote-notifications,Swift,Firebase,Firebase Cloud Messaging,Remote Notifications,我正在为通过FCM发送的天气应用程序编写丰富的通知 首先,我想通知您,我的通知已通过APNS和FCM成功发送和接收 我使用和来测试一切 上下文 这是我通过邮递员发送的有效载荷 { "to": "/topics/03772", "content_available": true, "mutable_content": true, "priority": "high", "notification": { "title": "Daily fo
{
"to": "/topics/03772",
"content_available": true,
"mutable_content": true,
"priority": "high",
"notification": {
"title": "Daily forecast",
"body": "Blue sky, no clouds",
"sound": "default",
"click_action": "weather"
},
"data": {
"timestamp": "1506103200",
"code": "03772",
"city": "London",
"attch": "7a",
"t": "15˚",
}
}
我喜欢这种有效载荷
格式,我知道它工作正常,我觉得它清晰易懂
解释:
用户可以通过将城市添加为喜爱的城市来订阅主题。主题是城市代码
数据
部分是从服务器发送的天气数据
这是我的didceive
方法:
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
func failEarly() {
contentHandler(request.content)
}
guard let content = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
return failEarly()
}
let attachment = try! UNNotificationAttachment(identifier: "image",
url: Bundle.main.url(forResource: "12_c", withExtension: "png")!,
options: nil)
let category = UNNotificationCategory(identifier: "weather", actions: [], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
content.attachments = [attachment]
contentHandler(content.copy() as! UNNotificationContent)
}
我正在使用Bundle中的一个图像名称直接进行测试,但它不起作用
问题
如何将xcassets文件夹中UIImage的PNG表示形式保存到磁盘
实际上,来自有效载荷的键/值“attch”:“7a”是天气图像在通知中显示为附件的代码。我已将所有图像保存在我的xcassets文件夹中(使用相同的代码名)
我需要:
- 获取在通知中收到的图像名称
- 在xcassets文件夹中获取关联的映像
- 将图像的PNG表示形式复制到FileManager(磁盘)
- 将其设置为通知的附件
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttempContent = bestAttemptContent {
// Modify the notification content here
let userInfo : [AnyHashable: Any] = bestAttempContent.userInfo
if let imgName = userInfo["attch"] {
if let url = createLocalUrl(forImageNamed: imgName as! String) {
do {
let attachment = try UNNotificationAttachment(identifier: "weather", url: url, options: nil)
defer {
self.bestAttemptContent?.attachments = [attachment]
}
}
catch {
print("Could not set UNNotificationAttachment")
}
}
}
contentHandler(bestAttempContent)
}
}
我在上面找到的方法是:
func createLocalUrl(forImageNamed name: String) -> URL? {
let fileManager = FileManager.default
let cacheDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let url = cacheDirectory.appendingPathComponent("\(name).png")
let path = url.path
guard fileManager.fileExists(atPath: path) else {
guard
let image = UIImage(named: name),
let data = UIImagePNGRepresentation(image)
else { return nil }
fileManager.createFile(atPath: path, contents: data, attributes: nil)
return url
}
return url
}
我希望它对您有所帮助。如果我没有弄错,您希望使用
attch
属性的值作为图像名称,从本地存储(.xcsets)形成url
有效负载内容是JSON,因此应该能够使用点语法访问attch
:
let fileName: String = request.content.data.attch
然后在资源上形成一个URL并附加它。(我假设它是“7a.jpg”)
谢谢你的回答,我正在实施。“图像”标识符是纯声明性的,它是特定附件的唯一标识符。稍后用它来识别附件。对你不起作用吗?是的,我写了一个答案,解释说这个答案就是我要找的答案,但它被删除并被否决了,所以…是的,你不应该写链接到其他答案的答案。无论如何,一旦悬赏期结束,这个问题将以重复的形式结束。可能的重复
if let url = Bundle.main.url(forResource: fileName, withExtension: "jpg") {
//The Image exists
let attachment = try! UNNotificationAttachment(identifier: "image",
url: url, options: nil)
content.attachments = [attachment]
} else {
//Unable to get/find image, use a default one.
let attachment = try! UNNotificationAttachment(identifier: "image",
url: Bundle.main.url(forResource: "12_c", withExtension: "png")!, options: nil)
content.attachments = [attachment]
}