9 firebase swift remote-notifications firebase-cloud-messaging
我正致力于通过FCM发送的天气应用程序的Rich Notification.
首先,我想通知您,我的通知是通过APNS和FCM成功发送和接收的.
CONTEXT
这是我发送的有效负载 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?",
}
}
Run Code Online (Sandbox Code Playgroud)
我喜欢这种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)
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Bundle中的图像名称直接测试,但它无法正常工作.
题
如何从xcassets文件夹中将UIImage的PNG表示保存到磁盘?
实际上,"attch": "7a"Payload 的键/值是天气图像的代码,作为附件显示在通知中.我已将所有图像保存在我的xcassets文件夹中(具有相同的代码名称)
我需要:
编辑:已解决!
我的问题得到了解决,这要归功于另一个问题的答案,这个问题甚至没有被标记为已接受的答案,但它对我这个问题有所帮助.
实际上,人们可能希望像我一样做,或者甚至没有考虑通知可以实现什么.
在我阅读的所有示例或指南中,他们都解释了如何下载图像并将其设置为附件以供您通知.
但它也可以使用xcassets文件夹中的本地图像完成.人们现在可以了解如何将其应用于此特定目的.
upvotes的数量告诉我,人们和我一样处于同样的境地,他们可能从这个问题/答案中学习.
这是我最后的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)
}
}
Run Code Online (Sandbox Code Playgroud)
我在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
}
Run Code Online (Sandbox Code Playgroud)
我希望它对你有帮助.
如果我的理解正确,您可能希望使用该attch属性的值作为图像名称来从本地存储 (.xcassets) 形成 url。
有效负载内容是 JSON,因此应该能够attch使用点语法进行访问:
let fileName: String = request.content.data.attch
Run Code Online (Sandbox Code Playgroud)
然后形成资源的 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]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
976 次 |
| 最近记录: |