A plain push notification is a title and a line of text. Add a photo, a product shot or a map thumbnail and people actually look at it. On iOS that's done with a Notification Service Extension (NSE): a small target that gets a chance to modify each incoming alert before it's shown. This guide walks through the whole thing, from payload to debugging, with code you can paste.

1. How it works

When APNs delivers an alert notification whose aps dictionary contains "mutable-content": 1, iOS wakes your NSE first. Your extension receives the notification content, can change the title, body or badge, download a file and attach it, and then hands the result back to the system. If your code doesn't finish in time (Apple gives it roughly 30 seconds), iOS calls a fallback method and shows whatever you hand back there, or the original notification.

Two rules trip people up immediately:

  • The NSE only runs for alert notifications. A silent push (content-available only, no alert) never reaches it.
  • mutable-content must be the number 1, not the string "1" and not true.

2. Add the extension target

In Xcode: File → New → Target → Notification Service Extension. Name it something like NotificationService and let Xcode activate the scheme.

Check three things in the new target:

  1. Bundle ID must be prefixed by your app's bundle ID, e.g. com.example.app.NotificationService.
  2. Deployment target must not be higher than the iOS version on your test device. This is the number one reason an NSE "silently never runs": Xcode often sets the newest iOS by default.
  3. The extension is embedded in the app target (Xcode does this for you under Frameworks, Libraries and Embedded Content / Embed Foundation Extensions).

The extension doesn't need its own Push Notifications capability; the host app's entitlement is what matters.

3. The payload

Your server (or test tool) sends something like this:

{ "aps": { "alert": { "title": "Your order has shipped", "body": "Tap to track the package" }, "mutable-content": 1, "sound": "default" }, "image_url": "https://example.com/images/package.jpg" } 

The custom image_url key sits outside aps; you can name it anything, your extension reads it from userInfo.

4. The extension code

Replace the template's NotificationService.swift with this:

import UserNotifications final class NotificationService: UNNotificationServiceExtension { private var contentHandler: ((UNNotificationContent) -> Void)? private var bestAttempt: UNMutableNotificationContent? override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { self.contentHandler = contentHandler bestAttempt = request.content.mutableCopy() as? UNMutableNotificationContent guard let content = bestAttempt, let urlString = request.content.userInfo["image_url"] as? String, let url = URL(string: urlString) else { contentHandler(request.content) return } downloadAttachment(from: url) { attachment in if let attachment { content.attachments = [attachment] } contentHandler(content) } } override func serviceExtensionTimeWillExpire() { // Called just before the system kills the extension. // Deliver what we have so the user still sees the text. if let contentHandler, let bestAttempt { contentHandler(bestAttempt) } } private func downloadAttachment(from url: URL, completion: @escaping (UNNotificationAttachment?) -> Void) { let task = URLSession.shared.downloadTask(with: url) { tempURL, response, error in guard let tempURL, error == nil else { completion(nil) return } // The file needs a real extension or iOS can't tell what type it is. let ext = url.pathExtension.isEmpty ? "jpg" : url.pathExtension let destination = FileManager.default.temporaryDirectory .appendingPathComponent(UUID().uuidString) .appendingPathExtension(ext) do { try FileManager.default.moveItem(at: tempURL, to: destination) let attachment = try UNNotificationAttachment(identifier: "image", url: destination, options: nil) completion(attachment) } catch { completion(nil) } } task.resume() } } 

A few details worth calling out:

  • Always call the content handler, on every path, exactly once. If you return without calling it, the user gets the original notification only after the timeout.
  • Move the downloaded file before the completion block ends. URLSession deletes its temporary file afterwards.
  • Keep a file extension. UNNotificationAttachment uses it to work out the type; a file named B3F2... with no extension fails with an "unrecognized file format" error.
  • Use HTTPS. App Transport Security applies to extensions too.
  • Keep images small. Extensions have a tight memory budget and attachments have size limits (Apple documents 10 MB for images), so serve a resized thumbnail rather than the original photo.

5. Test it on a real device

You need the full APNs path to see the extension work properly, so test on a real iPhone:

  1. Run the app scheme on the device once, accept the notification prompt, and log the device token from application(_:didRegisterForRemoteNotificationsWithDeviceToken:).
  2. A debug build gets a sandbox token, so send to the development environment (api.sandbox.push.apple.com).
  3. Send the payload above with any APNs sender: your backend, a curl call with a JWT, Apple's Push Notifications Console, or a Mac tester app.
  4. Lock the phone or background the app. You should see the notification with a thumbnail; long-press it to see the full image.

6. Debugging when nothing changes

If the notification arrives but without the image, the extension either didn't run or failed. Work through this list:

  • Attach the debugger to the extension. Select the NotificationService scheme, run it, pick your app when Xcode asks which app to run, then send a push. Breakpoints in didReceive will hit if the extension launches.
  • No breakpoint hit at all? Check the deployment target, that mutable-content is the integer 1, and that the payload has an alert.
  • Breakpoint hits but no image? Step through the download. The usual causes are an HTTP (not HTTPS) URL, a URL that redirects to a login page, or a missing file extension.
  • Use Console.app. Filter by your extension's process name to see crashes or memory kills, which otherwise look like "nothing happened".

Wrapping up

The recipe is small: mutable-content: 1, an alert in the payload, an NSE that downloads the file, renames it with an extension, attaches it and always calls the content handler, plus a fallback in serviceExtensionTimeWillExpire. Once that works, the same extension is the place to decrypt end-to-end-encrypted payloads or localise text before it's shown.

For sending test payloads while you build this, I built a free Mac app, Push Notification – Tester: pick your push certificate from Keychain, paste the device token, choose Development or Production and send any custom JSON, including mutable-content payloads like the one above. It's on the App Store: https://apps.apple.com/us/app/push-notification-tester/id1516154713

This post was written with AI assistance and reviewed before publishing.