Konstantin Loginov Konstantin Loginov - 1 year ago 1129
iOS Question

Image from attachment from Local notification is not shown in UNNotificationContentExtension

I've been working on rich notification experience which has been introduced in iOS10 and stuck with passing images as attachments to


Here's my ContentExtension:

class NotificationViewController: UIViewController, UNNotificationContentExtension {

@IBOutlet weak var attachmentImage: UIImageView!

func didReceive(_ notification: UNNotification) {

if let attachment = notification.request.content.attachments.first {
if attachment.url.startAccessingSecurityScopedResource() {
attachmentImage.image = UIImage(contentsOfFile: attachment.url.path)

As a tutorial, I've been following Advanced Notifications video from WWDC.
I've checked -
I'm assigning to UIImageView:

  • is not

  • has proper

  • attachment.url.path equals
    /var/mobile/Library/SpringBoard/PushStore/Attachments/<bundle of app>/<...>.png

Here's how I send local notification from the app:

let content = UNMutableNotificationContent()
content.title = "Sample title"
content.body = "Sample body"
content.categoryIdentifier = "myNotificationCategory"

let attachement = try! UNNotificationAttachment(identifier: "image",
url: Bundle.main.url(forResource: "cat", withExtension: "png")!,
options: nil)

content.attachments = [ attachement ]

let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: nil)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request){(error) in
if (error != nil){

"cat.png" is just a dummy resource I've added to proj.
enter image description here

As you can see, notification shows the pic, so I assume, that I'm sending it correctly, but in the expanded state(in
) I've never succeed at showing the same image.

What am I doing wrong?

Answer Source

When you create an UIImage with contentsOfFile, the UIImage object read the image header only, which indicating basic info, such as image size, etc.

So, try move stopAccessingSecurityScopedResource to [NotificationViewController dealloc].

Or using following:

  • objective-c code:

    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage *image = [UIImage imageWithData:imageData];
  • swift code:

    let imageData = NSData(contentsOf: attachment.url)
    let image = UIImage(data: imageData! as Data)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download