0

I have a local notification working fine and it delivers notifications with a content attachment using a url referencing an image in the app bundle. That works fine.

The issue I have is when I have an action that reissues the notification some time later.

Within the delegate function for Notification Center (didReceive response), I make a mutable copy of the response content and create a new tigger before creating a new request like this snippet shows.

let content = response.notification.request.content
let newContent = content.mutableCopy() as! UNMutableNotificationContent
// If I uncomment this next line, the notification gets added but without the attachment
// newContent.attachments = []
let newTrigger = UNTimeIntervalNotificationTrigger(timeInterval: snooozeInterval, repeats: false)
let request = UNNotificationRequest(identifier: response.notification.request.identifier,
                                    content: newContent,
                                    trigger: newTrigger)
do {
    try await UNUserNotificationCenter.current().add(request)
} catch {
    print(error)
}

When I do this, I get the following error:

Error Domain=UNErrorDomain Code=100 "Invalid attachment file URL" UserInfo={NSLocalizedDescription=Invalid attachment file URL}

if, before forming the request, I replace the newContent.attachments with no attachments, the request is formed OK and I get no error and the request is added. However, obviously, no attachment. Is there a way to get a properly formed attachment so that I an add it and reissue with with the new request.

Stewart Lynch
  • 875
  • 9
  • 26
  • My guess is that when original `content` is being deallocated, it removes files from attachments as well. Have you tried to copy attachments files from `content` to some local folder (the app's temp or documents), create new instances of `UNNotificationAttachment` with new URLs and add them to `newContent` instead? – lazarevzubov May 25 '22 at 04:50
  • The image is still in the bundle so no need to create a duplicate as I can recreate the `UNNotificationAttachment` and add it to `newContent` so perhaps I should just add the image name to the `content.userInfo` and then I can recreate it on receipt – Stewart Lynch May 25 '22 at 05:42
  • 1
    Yes, that is what I had to do. I added a new key to the dictionary and used the same name for the image in the bundle for value. Then I recreated the attachment if the response userInfo had that key. – Stewart Lynch May 25 '22 at 05:53
  • Perhaps, even if it's the same image from the original bundle, it's copied somewhere else for using in a notification. Notification extensions have different from main bundles and don't share directories between each other. I might be wrong, but what you described seems to be essentially the same thing I suggested. – lazarevzubov May 25 '22 at 06:36

0 Answers0