iOS/FCM-如何使用有效负载中的图像名称显示XCAsset文件夹中的图像作为通知附件? [英] iOS / FCM - How to use Image name from Payload to display an Image from XCAsset folder as Notification Attachment?

查看:71
本文介绍了iOS/FCM-如何使用有效负载中的图像名称显示XCAsset文件夹中的图像作为通知附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究通过FCM发送的Weather App的Rich Notification.

首先,我想通知您,我的通知已通过APNS和FCM成功发送和接收.

我正在使用 Pusher

我喜欢这种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"是要作为附件显示在通知中的天气图像的代码.我已将所有图像保存在xcassets文件夹中(具有相同的代号)

我需要:

  • 获取通知中收到的图像名称
  • 在xcassets文件夹中获取关联的图像
  • 将图像的PNG表示形式复制到FileManager(磁盘)
  • 将其设置为通知的附件

已解决!

我的问题得到了解决,这要归功于另一个问题的答案,这个问题甚至都没有被标记为已接受的答案,但是它对我有帮助.

实际上,人们可能想做与我相同的事情,或者甚至没有考虑过使用Notifications可以实现的目标.

在我阅读的所有示例或指南中,他们都说明了如何下载图像并将其设置为通知的附件.

但是它也可以通过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",
         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]
}

I am working on Rich Notification for a Weather App which is sent via FCM.

First of all, I would like to inform you that my notifications are sent and received successfully both with APNS and FCM.

I am using Pusher and Postman in order to test everything.

CONTEXT

Here is the Payload I am sending through 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˚",
    }
}

I like this Payload format, I know it's working, it feels clear to me and easy to understand.

EXPLANATIONS:

Users can subscribe to a topic by adding a city as a Favorite city. The topic is the city code.

The data part is the weather data sending from the server.

Here is my didReceive method :

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)
}

I am directly testing with an image name from Bundle, but it is not working.

QUESTION

How can I save to the disk the PNG Representation of an UIImage from the xcassets folder?

Indeed, the key/value "attch": "7a" from the Payload is the the code for the weather image to display in the notification as attachment. I have saved all the images in my xcassets folder (with same code name)

I need to:

  • Get the image name received in the Notification
  • Get the associated image in xcassets folder
  • Copy the PNG representation of the image to FileManager (disk)
  • Set it as Attachment for the Notification

EDIT: RESOLVED!

My question is resolved thanks to an answer from an other question which is not even marked as accepted answer, but it helped me, for this one.

Indeed, people might want to do the same as I did, or didn't even think about what can be achieve with Notifications.

In all the examples or guides that I read, they explain how to download an image and set it as attachment for you notification.

But it can also be done with local image from xcassets folder. People can now learn how it can be applied for this specific purpose.

The number of upvotes showed me that people were in the same situation as I did, and they probably learned from this question/answer.

Here is my final didReceive method :

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)
        }
    }

And the method I found on 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
    }

I hope it helped you.

解决方案

If I have this correctly, you would like to use the attch property's value as an image name to form a url from local storage (.xcassets).

The payload content is JSON, so should be able to access attch using dot syntax:

let fileName: String = request.content.data.attch

Then form a URL to the Resource and attach it. (I'm assuming its "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]
}

这篇关于iOS/FCM-如何使用有效负载中的图像名称显示XCAsset文件夹中的图像作为通知附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆