簡體   English   中英

通知托盤中帶有擴展圖像的豐富通知

[英]Rich notification with expanded image in notification tray

我正在嘗試使用這樣的擴展圖像在我的應用程序上顯示豐富的通知。 我已經使用通知服務擴展在應用程序中實現了這一點。 在此處輸入圖片說明

但是當我收到通知時,我只會得到一個縮略圖,看起來像這樣。 當我長按支持 3D 觸摸功能的手機時會出現擴展圖像,否則它只會在沒有 3D 觸摸功能的手機上顯示縮略圖。

在此處輸入圖片說明

我無法在 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 觸摸的設備的情況下長按)通知時,它們才會以展開形式顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM