'iOS: Swift: Remove the image view space when image is not available in Notification content

I have implemented Service and Content for push notifications. When the image is not available the image view space is still showing as empty. I need only notification text when no image is available.

Here is my code

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

and when i click view button of push notification it is showing like

enter image description here



Solution 1:[1]

You've to add "UNNotificationExtensionInitialContentSizeRatio" to 0 in your .plist file as the image below here.

UNNotificationExtensionInitialContentSizeRatio

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1