简体   繁体   English

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

[英]Rich notification with expanded image in notification tray

I'm trying to display rich notifications on my app with an expanded image like this.我正在尝试使用这样的扩展图像在我的应用程序上显示丰富的通知。 I have used Notification Service extension to implement this in the app.我已经使用通知服务扩展在应用程序中实现了这一点。 在此处输入图片说明

But I'm only getting a thumbnail when I receive a notification, which looks something like this.但是当我收到通知时,我只会得到一个缩略图,看起来像这样。 The expanded image appears when when I long press on a 3D-touch capable phone, otherwise it just displays a thumbnail on phones which doesn't have 3D-touch.当我长按支持 3D 触摸功能的手机时会出现扩展图像,否则它只会在没有 3D 触摸功能的手机上显示缩略图。

在此处输入图片说明

I wasn't able to find any documentation or any questions on SO which explains how to do this if it is possible.我无法在 SO 上找到任何文档或任何问题,这些文档或问题解释了如何在可能的情况下执行此操作。 I would like to know if it is possible to do this on iOS, if not is there any possible workaround to accomplish this?我想知道是否可以在 iOS 上执行此操作,如果不能,是否有任何可能的解决方法来完成此操作? Here is my NotificationSerivce extension.这是我的NotificationSerivce扩展。 Any help is much appreciated!非常感谢任何帮助! Thanks!谢谢!

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

It is a very old question and I guess you'd have discovered by now, what you are trying to achieve is not technically feasible.这是一个非常古老的问题,我想您现在已经发现,您要实现的目标在技术上是不可行的。 The notifications appear in their collapsed form and only if the user 3d presses (or long-presses in case of devices without 3d-touch) on the notification will they be shown in the expanded form.通知以折叠形式显示,并且仅当用户 3d 按下(或在没有 3d 触摸的设备的情况下长按)通知时,它们才会以展开形式显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM