使用UIImage或远程URL进行UNNotificationAttachment
在我的Notification Service Extension
我从URL中下载一个图像,在通知中显示为UNNotificationAttachment
。
所以我把这个图像作为UIImage,并没有看到需要写在我的应用程序目录/组容器光盘只是为了设置通知。
用UIImage创build一个UNNotificationAttachment
有什么好方法吗? (应该适用于本地和远程通知)
- 在tmp文件夹中创build目录
- 将
UIImage
的NSData
表示写入新创build的目录中 - 使用url创buildUNNotificationAttachment到tmp文件夹中的文件
- 清理tmp文件夹
我在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
与UNUserNotificationAttachment
创buildUNUserNotificationRequest
,你可以简单地这样做
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
将图像文件复制到自己的位置。
我已经创build了一个关于这个主题的博客,专注于GIF图像。 但是应该很容易重写我的代码为简单的图像。
您需要创build一个通知服务扩展:
并且包含这个代码:
final class NotificationService: UNNotificationServiceExtension { private var contentHandler: ((UNNotificationContent) -> Void)? private var bestAttemptContent: UNMutableNotificationContent? override internal func didReceiveNotificationRequest(request: UNNotificationRequest, withContentHandler contentHandler: (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() } guard let attachmentURL = content.userInfo["attachment-url"] as? String else { return failEarly() } guard let imageData = NSData(contentsOfURL:NSURL(string: attachmentURL)!) else { return failEarly() } guard let attachment = UNNotificationAttachment.create("image.gif", data: imageData, options: nil) else { return failEarly() } content.attachments = [attachment] contentHandler(content.copy() as! UNNotificationContent) } override func serviceExtensionTimeWillExpire() { // Called just before the extension will be terminated by the system. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { contentHandler(bestAttemptContent) } } } extension UNNotificationAttachment { /// Save the image to disk static func create(imageFileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? { let fileManager = NSFileManager.defaultManager() let tmpSubFolderName = NSProcessInfo.processInfo().globallyUniqueString let tmpSubFolderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(tmpSubFolderName, isDirectory: true) do { try fileManager.createDirectoryAtURL(tmpSubFolderURL!, withIntermediateDirectories: true, attributes: nil) let fileURL = tmpSubFolderURL?.URLByAppendingPathComponent(imageFileIdentifier) try data.writeToURL(fileURL!, options: []) let imageAttachment = try UNNotificationAttachment.init(identifier: imageFileIdentifier, URL: fileURL!, options: options) return imageAttachment } catch let error { print("error \(error)") } return nil } }
欲了解更多信息,你可以在这里检查我的博客post: http : //www.avanderlee.com/ios-10/rich-notifications-ios-10/