通知托盘中扩展图像的丰富通知

时间:2018-02-22 07:22:56

标签: ios swift unnotificationattachment

我正在尝试使用这样的展开图像在我的应用上显示丰富的通知。我已经使用Notification Service扩展在应用程序中实现了这一点。 enter image description here

但是当我收到通知时,我只会得到一个缩略图,看起来像这样。当我长时间按下具有3D触控功能的手机时,会出现展开的图像,否则只会在没有3D触控的手机上显示缩略图。

enter image description here

我无法在SO上找到任何文档或任何问题,如果可能的话,我会解释如何执行此操作。我想知道是否可以在iOS上执行此操作,如果没有,是否有任何可能的解决方法来实现此目的?这是我的NotificationSerivce扩展程序。任何帮助深表感谢!谢谢!

class NotificationService: UNNotificationServiceExtension {

    let fileManager = FileManager()
    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
            guard let content = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
                return self.contentHandler = contentHandler
            }

            bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"            

            guard let attachmentURL = content.userInfo["attachment-url"] as? String else {
                return self.contentHandler = contentHandler
            }
            guard let fileName = attachmentURL.components(separatedBy: "/").last else {
                return self.contentHandler = contentHandler
            }

            guard let imageData = try? Data(contentsOf: URL(string: attachmentURL)!) else {
                return self.contentHandler = contentHandler
            }


            if let thumbnailAttachment = UNNotificationAttachment.create(imageFileIdentifier: fileName, data: imageData, options: nil) {
                bestAttemptContent.attachments = [thumbnailAttachment]
            }

            contentHandler(bestAttemptContent)
        }
    }

    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: Data, options: [AnyHashable: Any]?) -> UNNotificationAttachment? {
        let fileManager = FileManager.default
        let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
        let tmpSubFolderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true)

        do {
            try fileManager.createDirectory(at: tmpSubFolderURL!, withIntermediateDirectories: true, attributes: nil)
            let fileURL = tmpSubFolderURL?.appendingPathComponent(imageFileIdentifier)

            try data.write(to: fileURL!, options: [])
            let imageAttachment = try UNNotificationAttachment(identifier: imageFileIdentifier, url: fileURL!, options: options)
            return imageAttachment
        } catch let error {
            print("error \(error)")
        }

        return nil
    }
}

1 个答案:

答案 0 :(得分:1)

这是一个非常老的问题,我想您现在已经发现,您要实现的目标在技术上不可行。 通知以折叠形式显示,只有用户3d按下通知(或在没有3d触摸的设备上长按)时,通知才会以展开形式显示。