I’m utilizing Notification Service and Notification Content material extensions to switch the notification. However in my notification, the content material icon or the thumbnail picture shouldn’t be exhibiting. However the content material are showing positive and dealing .
My Code :
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content material.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content material right here...
bestAttemptContent.title = "(bestAttemptContent.title) [modified]"
if let urlImageString = bestAttemptContent.userInfo["urlImageString"] as? String {
if let imageUrl = URL(string: urlImageString){
downloadImage(from: imageUrl) { lead to
change outcome {
case .success(let localURL):
print("--->>> Picture downloaded and saved at: (localURL)")
do{
let attachment = strive UNNotificationAttachment(identifier: "thumbnail", url: localURL, choices: nil)
bestAttemptContent.attachments = [attachment]
} catch {
print(error)
}
case .failure(let error):
print("--->>> Picture obtain error: (error)")
}
}
}
}
contentHandler(bestAttemptContent)
}
}
func downloadImage(from imageURL: URL, completion: @escaping (End result<URL, Error>) -> Void) {
let activity = URLSession.shared.dataTask(with: imageURL) { knowledge, response, error in
if let error = error {
completion(.failure(error))
return
}
guard let knowledge = knowledge, let response = response as? HTTPURLResponse, response.statusCode == 200 else {
let error = NSError(area: "ImageDownloadError", code: 0, userInfo: nil)
completion(.failure(error))
return
}
do {
let fileManager = FileManager.default
let documentsURL = strive fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let imageFileURL = documentsURL.appendingPathComponent(imageURL.lastPathComponent)
strive knowledge.write(to: imageFileURL)
completion(.success(imageFileURL))
} catch {
completion(.failure(error))
}
}
activity.resume()
}
override func serviceExtensionTimeWillExpire() {
// Known as simply earlier than the extension will probably be terminated by the system.
// Use this as a chance to ship your "finest try" at modified content material, in any other case the unique push payload will probably be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
Do I want to keep up some other process? Or which process I’m lacking?