
首先,我想通知您,我的通知都是通过APNS和FCM成功发送和接收的.
我正在使用Pusher和Postman来测试一切.
CONTEXT
这是我通过邮递员发送的有效载荷
{ "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˚",}} 我喜欢这种有效载荷格式,我知道它有效,对我来说很清楚,也很容易理解.
解释:
用户可以通过将城市添加为收藏城市来订阅主题.主题是城市代码.
数据部分是从服务器发送的天气数据.
这是我的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”是天气图像的代码,作为附件显示在通知中.我已将所有图像保存在我的xcassets文件夹中(使用相同的代码名称)
我需要:
>获取通知中收到的图像名称
>在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 } 我希望它对你有帮助.
如果我有正确的,你想使用attch属性的值作为图像名称从本地存储(.xcassets)形成一个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",options: nil) content.attachments = [attachment]} else { //Unable to get/find image,use a default one. let attachment = try! UNNotificationAttachment(IDentifIEr: "image",options: nil) content.attachments = [attachment]} 总结 以上是内存溢出为你收集整理的swift – iOS / FCM – 如何使用Payload中的图像名称将XCAsset文件夹中的图像显示为通知附件?全部内容,希望文章能够帮你解决swift – iOS / FCM – 如何使用Payload中的图像名称将XCAsset文件夹中的图像显示为通知附件?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)