我正在为通过 FCM 发送的天气应用程序开发丰富的通知。
首先,我想通知您,我的通知已通过 APNS 和 FCM 成功发送和接收。
语境
这是我发送的有效载荷Postman
{
"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˚",
}
}
我喜欢这种Payload
格式,我知道它很有效,我觉得它很清晰,也很容易理解。
说明:
用户可以通过将城市添加为收藏城市来订阅主题。主题是城市代码。
该data
部分是从服务器发送的天气数据。
这是我的didReceive
方法:
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"
来自 Payload 的键/值是作为附件显示在通知中的天气图像的代码。我已将所有图像保存在我的 xcassets 文件夹中(具有相同的代号)
我需要:
- 获取 Notification 中收到的图片名称
- 获取 xcassets 文件夹中的关联图像
- 将图像的 PNG 表示复制到 FileManager(磁盘)
- 将其设置为通知的附件
编辑:已解决!
我的问题得到了解决,这要归功于另一个问题的答案,该问题甚至没有被标记为已接受的答案,但它帮助了我,对于这个问题。
确实,人们可能想做和我一样的事情,或者甚至没有想过使用通知可以实现什么。
在我阅读的所有示例或指南中,它们解释了如何下载图像并将其设置为通知的附件。
但也可以使用 xcassets 文件夹中的本地图像来完成。人们现在可以了解如何将其应用于此特定目的。
赞成票的数量向我表明,人们和我处于同样的境地,他们可能从这个问题/答案中学到了东西。
这是我最终的didReceive方法:
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)
}
}
我在 SO 上找到的方法:
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
}
我希望它对你有所帮助。