Image from local notification attachment (UNNotificationAttachment) is not showing sometimes

Viewed 248

I have an iOS app where I use only local notifications. One of the features for this app is that I can create several notifications at once (the number of notifications can be from 1 to 20). Notification trigger is time stamp only. I always add an attachment to each notification - a picture that is always being presented in the application database. When a certain time comes, the notification trigger is triggered and this notification is shown to the user, usually once a day. Sometimes (about 20% of cases) a picture is not displayed in the notification (does not matter if the screen is locked or not). This behavior is present in iOS12 - iOS14. I did the following checks, which were successful:

  • no errors occurred while creating and adding an notification to UNUserNotificationCenter
  • check if a temporary URL image exists before creating UNNotificationAttachment
  • check if the picture exceeds 10Mb
  • check if an attachment exists for each pending Notification Requests that has already been added to the notification center
  • check if there is an access to the attachment of each pending Notification Requests that has already been added to the notification center

I've investigated an interesting case on iOS 13, after restarting the device, no one previously generated notification will show pictures. The debug shows that all UNNotificationAttachments in pending Notification Requests are present, but I do not have access to them. Obviously, not only I but also the OS do not have access. Very strange undocumented behavior. I assume that over time the OS loses access to the UNNotificationAttachment files, but how to understand this? OS is not coping with its own security?

// how I check access for files in attachments
private func checkSavedAttachments() {
    UNUserNotificationCenter.current().getPendingNotificationRequests {
        (allScheduled) in
        let attachments = allScheduled.map{ $0.content.attachments }.reduce([], +)
        var accessDeniedCount = 0
        attachments.forEach {
            (attach) in
            if attach.url.startAccessingSecurityScopedResource() {
                print(attach.url)
            }
            else {
                accessDeniedCount += 1
            }
            attach.url.stopAccessingSecurityScopedResource()
        }
        if accessDeniedCount > 0 {
            fatalError()
        }
    }
}

Thanks for any help!

0 Answers
Related