簡體   English   中英

iOS:Swift:當圖像在通知內容中不可用時刪除圖像視圖空間

[英]iOS: Swift: Remove the image view space when image is not available in Notification content

我已經為推送通知實施了服務和內容。 當圖像不可用時,圖像視圖空間仍顯示為空。 當沒有圖像可用時,我只需要通知文本。

這是我的代碼

func didReceive(_ notification: UNNotification) {
        let content = notification.request.content
        
        if let urlImageString = content.userInfo["image"] as? String {
            if let url = URL(string: urlImageString) {
                URLSession.downloadImage(atURL: url) { [weak self] (data, error) in
                    if let _ = error {
                        return
                    }
                    guard let data = data else {
                        return
                    }
                    DispatchQueue.main.async {
                        self?.imageView.image = UIImage(data: data)
                    }
                }
            }
        }
    }
    
}

extension URLSession {
    
    class func downloadImage(atURL url: URL, withCompletionHandler completionHandler: @escaping (Data?, NSError?) -> Void) {
        let dataTask = URLSession.shared.dataTask(with: url) { (data, urlResponse, error) in
            completionHandler(data, nil)
        }
        dataTask.resume()
    }
}

當我單擊推送通知的查看按鈕時,它顯示為

在此處輸入圖像描述

您必須在 .plist 文件中將“UNNotificationExtensionInitialContentSizeRatio”添加為 0,如下圖所示。

UNNotificationExtensionInitialContentSizeRatio

暫無
暫無

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

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