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

Rich notification with expanded image in notification tray

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

但是当我收到通知时,我只得到一个缩略图,它看起来像这样。当我长按支持 3D 触摸的 phone 时,展开的图像会出现,否则它只会在没有 3D 触摸的 phone 上显示缩略图。

我无法在 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
    }
}

这是一个很老的问题,我想你现在应该已经发现了,你想要实现的目标在技术上是不可行的。 通知以折叠形式显示,只有当用户在通知上进行 3d 按下(或在没有 3d-touch 的设备的情况下长按)时,它们才会以展开形式显示。