I am adding a thumbnail to a rich notification. I generate the image to be used as an attachment like this:
let url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
let fileURL = url.appendingPathComponent("someImageName", isDirectory: false).appendingPathExtension("png")
do{
try UIImagePNGRepresentation(image)?.write(to: fileURL)
}catch{
print("Could not write: ", error)
}
This is successful. If I now run this code:
let content = try? FileManager.default.contentsOfDirectory(atPath: NSTemporaryDirectory())
print(content ?? "error")
it prints out this: ["someImageName.png"]
.
Then I create the attachment, like this:
let attachment = try? UNNotificationAttachment(identifier: "image", url: url, options: nil)
let content = UNMutableNotificationContent()
content.title = "Test"
content.attachments = [attachment!]
let request = UNNotificationRequest(identifier: "someID", content:content, trigger: /*someTrigger*/)
UNUserNotificationCenter.current().add(request, withCompletionHandler:...)
This is all working. The notification gets sent, and the attachment is attached. But if I now print out the contents of my temporary folder, it is empty! The image I saved earlier has been removed from this folder.
After a lot of trial and error, I have found out that the image is removed at the very time I add the notification. Why is my notification deleting my image? I can't find any documentation on this!
If I do everything above, but comment out this line: //content.attachments = [attachment!]
, meaning that my stored image will not be added as an attachment to my notification, then my image is not being removed from the temporary folder!
Are these files being cleaned up after use? Or do I have to look in a different folder after the notification has disappeared and clean them up manually? I don't want my app to create hundreds of images that will never be used again, without being able to delete them..