UNNotificationResponse mutableCopy attachment invalid

107 Views Asked by At

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.

0

There are 0 best solutions below