Menu

Images and delivery receipts

Show an image from a URL in a push, and see on the device that a push arrived, with a drop-in Notification Service Extension.

A Notification Service Extension is a small piece of your app that iOS runs when a push arrives, before it appears. Pushwren’s helper for it does two things: it downloads an image from a URL you give in the send and attaches it, and it tells Pushwren that the push reached the phone, which the portal, the API and the AI tools show as Delivered. Pushwren stores no images; the URL is yours and must be public.

Both need version 0.5.0 or later of the Swift package, on iOS and iPadOS.

Add the extension

  1. In Xcode, choose File > New > Target > Notification Service Extension, name it, and finish. Xcode makes the target and a NotificationService class.
  2. Select the new target, open General > Frameworks and Libraries, and add the PushwrenNotificationService library from the Pushwren package. Add that library to the extension, not Pushwren: the main library uses APIs that iOS refuses in an extension.
  3. Replace the class Xcode generated with:
import PushwrenNotificationService

class NotificationService: PushwrenNotificationService {}

That is all. There is no App Group, no key and no other capability. The extension needs its own App ID and provisioning like any extension, which automatic signing makes for you.

Keep the main app on Pushwren 0.5.0 or later. At launch it looks for the helper in your extension, by the Pushwren_PushwrenNotificationService.bundle resource Xcode copies into it, and reports at registration whether it is there. Pushwren asks a device for a receipt only after the app has reported the helper, so an app without it gets exactly the push it gets today. The app remembers a helper it found, per app version and build; a “no” is checked again at each launch.

Images

Add image to a send, an https URL of up to 1,024 characters:

{
  "target": { "userIds": ["u_123"] },
  "alert": { "title": "Order shipped", "body": "Your order A1001 is on its way." },
  "image": "https://example.com/orders/a1001.jpg"
}

The helper downloads the image in the extension and attaches it. If anything goes wrong, such as a bad URL, a file of the wrong type, one that is too big, or a download that isn’t done in time, the notification still shows, without the image.

Apple’s limits:

  • JPEG, PNG or GIF (a GIF can be animated); the type is checked from the file, not the URL.
  • At most 10 MB.
  • https only, because the extension follows App Transport Security.
  • The download has to finish in the short time iOS gives an extension, or the push shows without it.

image needs an alert push with alert; a background push, or a send with a raw apns payload, answers 422. The same send can’t use aps, _h, _i or _r as data keys: those are reserved and answer 422. The extension runs only for pushes marked mutable-content, which image sets for you.

Image guidance

These are common practice, not Apple’s rules:

  • Keep the file under about 1 MB. A smaller image downloads sooner, which matters on a slow connection with a short time limit.
  • Make it landscape, about 2:1, for example 1440 × 720.
  • Keep the subject centered. The collapsed notification shows a small square thumbnail cut from the middle of the image, so anything near the left or right edge of a wide image is cut off there.

What Delivered means

When a push that asked for a receipt reaches the extension, the helper posts a receipt to Pushwren, and the send’s Delivered count goes up by one for that device. Read it as a lower bound: it is never more than the true number, and can be less.

  • Only alert pushes with alert, to devices whose app has the helper, are asked for a receipt. Silent pushes never run the extension. A send’s counts.receiptsRequested is how many were asked, so Delivered reads as “N of M that can confirm”.
  • A big send, to over about 1,000 devices, asks a sample of about 1,000 of them, so the server isn’t flooded by a broadcast. The portal says when a figure is from a sample.
  • A receipt that is lost, because the phone went offline before it posted, isn’t retried. The extension has nowhere to keep it.
  • A device that opens the notification counts as delivered for a push that asked for a receipt, even if its receipt was lost.
  • A device that never reports the helper can’t confirm, and a push to it is only ever “accepted”, which means Apple took it.

Each delivery in the log has a deliveredAt time when its receipt arrived, and receiptRequested when it was asked.

If you already have an extension

Don’t subclass Pushwren’s class. Link the PushwrenNotificationService library to your extension, and call it from your own class:

import PushwrenNotificationService
import UserNotifications

class NotificationService: UNNotificationServiceExtension {
    private var pushwren: PushwrenExtension.Handle?

    override func didReceive(_ request: UNNotificationRequest,
                             withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        pushwren = PushwrenExtension.handle(request, contentHandler: contentHandler)
    }

    override func serviceExtensionTimeWillExpire() {
        pushwren?.timeWillExpire()
    }
}

handle posts the receipt and downloads the image at the same time, then calls your contentHandler once, with the image attached if it arrived. A push with neither a receipt nor an image passes straight through. If you change the content yourself, wrap the handler you give it.

Receipts turn on for any extension that links the helper library, so link it only if you call it.

If an image or receipt is missing

See Troubleshooting for what to check when an image doesn’t show or Delivered stays at zero, and POST /v1/receipts for the call the helper makes.