UNNotificationAttachment

UNNotificationAttachment

本文介绍了UNNotificationAttachment with UIImage或Remote URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的通知服务扩展程序我正在从URL下载图像,以在通知中显示为 UNNotificationAttachment

In my Notification Service Extension I am downloading an image from a URL to show as UNNotificationAttachment in a notification.

所以我将此图像作为UIImage,并且没有看到需要在光盘上的app目录/组容器中写入它来设置通知。

So I have this image as UIImage and don't see the need to write it in my app directory / group container on disc just to set up the notification.

使用UIImage创建 UNNotificationAttachment 是否有好方法?
(应该适用于本地和远程通知)

Is there a good way create an UNNotificationAttachment with a UIImage ?(should be appliable to local and remote notifications)

推荐答案


  1. 在tmp文件夹中创建目录

  2. UIImage NSData 表示写入新创建的目录

  3. 创建带有url的UNNotificationAttachment到tmp文件夹中的文件

  4. 清理tmp文件夹

  1. create directory in tmp folder
  2. write the NSData representation of the UIImage into the newly created directory
  3. create the UNNotificationAttachment with url to the file in tmp folder
  4. clean up tmp folder

我在 UINotificationAttachment上写了一个扩展名

extension UNNotificationAttachment {

    static func create(identifier: String, image: UIImage, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
        let fileManager = FileManager.default
        let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
        let tmpSubFolderURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true)
        do {
            try fileManager.createDirectory(at: tmpSubFolderURL, withIntermediateDirectories: true, attributes: nil)
            let imageFileIdentifier = identifier+".png"
            let fileURL = tmpSubFolderURL.appendingPathComponent(imageFileIdentifier)
            guard let imageData = UIImagePNGRepresentation(image) else {
                return nil
            }
            try imageData.write(to: fileURL)
            let imageAttachment = try UNNotificationAttachment.init(identifier: imageFileIdentifier, url: fileURL, options: options)
            return imageAttachment
        } catch {
            print("error " + error.localizedDescription)
        }
        return nil
    }
}

所以从 UIImage 创建 UNUserNotificationRequest UNUserNotificationAttachment 你可以这样做......

So to create UNUserNotificationRequest with UNUserNotificationAttachment from a UIImage you can simply do sth like this

let identifier = ProcessInfo.processInfo.globallyUniqueString
let content = UNMutableNotificationContent()
content.title = "Hello"
content.body = "World"
if let attachment = UNNotificationAttachment.create(identifier: identifier, image: myImage, options: nil) {
    // where myImage is any UIImage that follows the 
    content.attachments = [attachment] 
}
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 120.0, repeats: false)
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
    // handle error
}

这应该有效,因为 UNNotificationAttachment 会将图像文件复制到自己的位置。

This should work since UNNotificationAttachment will copy the image file to an own location.

这篇关于UNNotificationAttachment with UIImage或Remote URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 03:42