首先,我想通知您,我的通知都是通过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 }
我希望它对你有帮助.
有效负载内容是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] }