Skip to content
Menu

Push notifications

Rich push

Markdown version

Show images in push notifications on iOS with a Notification Service Extension and on Android with a big picture style, and add action buttons from Herald.

A push message in Herald can carry an image for iOS and an image for Android. This page shows what each platform needs to display it, and how action buttons work on the device.

Before you start

  • Push set up on the platform: iOS setup or Android setup.
  • Image URLs served over HTTPS. iOS blocks plain HTTP downloads in extensions by default.

Images on iOS

When a message has an iOS image, Herald adds mutable-content: 1 and an image_url key to the APNs payload. iOS then hands the notification to your Notification Service Extension, which has about 30 seconds to download the image and attach it. Without an extension, the notification shows as text.

  1. In Xcode, choose File > New > Target > Notification Service Extension, name it (for example LumenNotificationService) and embed it in your app.
  2. Add the HeraldNotificationService library to the extension target only. It depends on Foundation and UserNotifications, not on HeraldSDK, so it is safe to link into an app extension.
  3. Replace the generated NotificationService.swift with the code below.
  4. Set the extension's deployment target to iOS 16 or later.
swift
import HeraldNotificationService

final class NotificationService: HeraldNotificationService, @unchecked Sendable {}

The class reads image_url (and image, if a sender uses that key), downloads the image over HTTPS within 20 seconds, and attaches it. It skips the image and shows the text notification when the URL is not HTTPS, the server answers with an error, the file is larger than 10 MB, the download runs out of time, or the file is not JPEG, PNG, GIF or HEIC. To wait a different time, override imageTimeout.

If you already have a service extension, call the same logic from it:

swift
import HeraldNotificationService
import UserNotifications

final class NotificationService: UNNotificationServiceExtension {
    override func didReceive(
        _ request: UNNotificationRequest,
        withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
    ) {
        nonisolated(unsafe) let content = request.content
        nonisolated(unsafe) let handler = contentHandler
        Task {
            let prepared = await HeraldRichPush.prepare(content)
            handler(prepared)
        }
    }
}

HeraldRichPush.prepare returns the content unchanged when the push has no image and no buttons. With this form, handle serviceExtensionTimeWillExpire yourself; the HeraldNotificationService class already does.

Images on Android

When a message has an Android image, Herald sets notification.image in the FCM message. You do not need extra code:

  • App in the background. Android and Firebase display the notification, and expanding it shows the image.
  • App in the foreground. herald-fcm downloads the image (5-second connect and read timeouts, 8 seconds in total, up to 5 MB, scaled down to fit 1,024 by 1,024 pixels) and shows it with a big picture style, with the image as the large icon too. If the download fails, the notification shows its full text instead.

Video

A push can carry a video as well as its image. The image stays the poster, so the push looks right everywhere a video can't play. Pick a video in the media library; you need SDK 0.6.0.

  • iOS. Herald adds a video_url key beside image_url to the APNs payload. The Notification Service Extension from Images on iOS attaches the video when the URL is HTTPS, the server says it is video/mp4 or video/quicktime, and the file is at most 10 MB (both the Content-Length and the bytes received are checked). It gets 60% of a 25-second budget. If the video fails (an error, too slow, too big or the wrong type), the extension attaches the poster in image_url instead, and if that fails too the notification shows its text. The notification is always delivered. Without the extension the push shows as text.
  • Android. Android shows the poster. herald-fcm ignores video_url.
  • Payload size. APNs allows 4,096 bytes. If a long video URL and a long body don't fit, Herald shortens the body first, then drops video_url. The poster is never dropped.

When you pick a video for a push, the editor warns "This video is too large for iOS push. iOS will show the poster." if it is over 10 MB.

Action buttons

Both SDKs can show up to three action buttons on a push and report which one the user tapped. You author the buttons in Herald, and the SDKs draw them.

Author buttons in Herald

  1. Open a push variant with Edit, then open Delivery options (mobile SDK).
  2. Under Action buttons, choose Add button and fill in Button 1 title (up to 30 characters) and Deep link (optional). Add up to three buttons. A button with no deep link opens the push's own deep link.
  3. Choose Save delivery options. The variant's own Save does not store them.

Buttons apply to pushes sent after you save.

How buttons reach each platform

PlatformWhat Herald sendsWhat the app needs
iOS (APNs)A herald_actions key with the buttons, and mutable-content: 1. Herald sets no aps.category.The Notification Service Extension from Images on iOS. It adds the buttons before the notification shows. Without it, the push shows with no buttons.
Android (FCM)A data message that carries the title, body and herald_actions, with no notification block.herald-fcm, which builds the notification with its buttons whether the app is in the foreground or the background.
iOS device through FCMThe normal notification, without buttons.None. Send iOS pushes through APNs to get buttons.

On iOS, a category your app registered with registerPushCategories is kept. A category whose id starts with herald. is the one the extension created for earlier buttons, and the extension replaces it with the buttons in the push.

What the SDKs read

  • Android. herald-fcm reads a herald_actions data key, a JSON array such as [{"id":"start","title":"Start workout","deeplink":"lumen://workout"}]. Malformed entries are skipped and only the first three are kept.
  • iOS. HeraldNotificationService turns the same herald_actions key into a notification category.

A button tap opens the app. The SDK logs a push open, then a button interaction with the button's ID, both linked to the decision. It then opens the button's own deep link, or the push's deep link when the button has none.

For buttons you define in the app instead, register a category and send its id in aps.category yourself:

swift
Herald.shared.registerPushCategories([
    HeraldPushCategory(id: "workout_reminder", buttons: [
        .init(id: "start", title: "Start workout"),
        .init(id: "snooze", title: "Remind me later"),
    ]),
])

Verify it works

  1. Create a push message in Herald with an iOS image and an Android image, both HTTPS.
  2. Send it to a test user (see Testing push).
  3. On iOS, long-press or expand the notification to see the image. On Android, expand the notification, once with the app in the background and once in the foreground.

Troubleshooting

  • No buttons on iOS. The extension is missing or not embedded, or the push went through FCM instead of APNs.
  • No buttons on Android. herald-fcm is not in the app, or another messaging service receives FCM messages. See Android setup.
  • iOS shows text only. The extension target is missing, not embedded in the app, its principal class is not your HeraldNotificationService subclass, or its deployment target is above the device's iOS version. Run the extension scheme and attach the debugger to check didReceive is called.
  • iOS shows text only for some images. The URL is plain HTTP, the file is larger than 10 MB, or it is WebP or another type iOS does not accept as an attachment. Serve compressed JPEG or PNG images over HTTPS.
  • Android shows text only in the foreground. The image did not download within 8 seconds, the server returned an error, or the file is larger than 5 MB.

Next

See Herald decide live →