In-app messages
Custom views
Replace the default view for any in-app message type, write HTML messages with the JavaScript bridge and its security rules, or replace the presenter entirely.
You can replace the view for one message type and keep everything else the default presenter does: the overlay, transitions, focus handling and interaction logging. For full control, replace the presenter itself.
Before you start
- You use the default presenter. See the overview.
1. Replace the view for a type
Pass a view provider when you install the presenter. It receives the message and an InAppMessageActions object. Return your view for the types you replace; on iOS, return nil to use the default view for that message.
import SwiftUI
import HeraldSDK
import HeraldUI
Herald.useDefaultInAppUI(theme: lumenTheme) { message, actions in
guard message.type == .modal else { return nil }
return AnyView(LumenModal(message: message, actions: actions))
}
struct LumenModal: View {
let message: InAppMessage
let actions: InAppMessageActions
var body: some View {
VStack(spacing: 16) {
Text(message.content.header ?? "").font(.title2.bold())
Text(message.content.body ?? "")
ForEach(message.content.buttons) { button in
Button(button.text) { actions.buttonClicked(button) }
.frame(minHeight: 44)
}
Button("Not now") { actions.dismiss() }.frame(minHeight: 44)
}
.padding(24)
.background(Color(.systemBackground), in: RoundedRectangle(cornerRadius: 20))
.padding(24)
}
}val lumenModal: InAppMessageContent = { message, actions -> LumenModal(message, actions) }
HeraldInAppPresenter.install(
theme = LumenTheme,
customViews = mapOf(InAppMessageType.MODAL to lumenModal),
)
@Composable
fun LumenModal(message: InAppMessage, actions: InAppMessageActions) {
Box(Modifier.fillMaxSize().background(Color(0x99000000)), contentAlignment = Alignment.Center) {
Column(Modifier.padding(24.dp).background(Color.White, RoundedCornerShape(20.dp)).padding(24.dp)) {
Text(message.content.header.orEmpty(), style = MaterialTheme.typography.titleLarge)
Text(message.content.body.orEmpty())
message.content.buttons.forEach { button ->
Button(onClick = { actions.buttonClicked(button) }, modifier = Modifier.heightIn(min = 48.dp)) {
Text(button.text)
}
}
TextButton(onClick = actions::dismiss, modifier = Modifier.heightIn(min = 48.dp)) { Text("Not now") }
}
}
}Your view replaces everything the default view draws for that type, including a modal's dimmed background and close button. The presenter still places it (slideups at their edge, other types centered), animates it in and out, blocks the app behind modal, full-screen and HTML messages, and handles the escape gesture (iOS) and the back button (Android).
Report every action
Call one InAppMessageActions method for each user action. Each one logs the interaction and, except clicked, takes the message down.
| Method | Use it when | Logs | Closes |
|---|---|---|---|
buttonClicked(button) | The user taps one of message.content.buttons. Opens the button's link. | Button click with the button's id | Yes |
bodyClicked(url) | The user taps the message itself. Opens url when you pass one. | Click | Yes |
clicked(buttonId) | The user taps a control that should not close the message | Button click with that id | No |
dismiss() | The user closes the message without acting | Dismissal, only if nothing was clicked | Yes |
After the message is down, further calls are ignored, so a double tap cannot log twice. The default building blocks are public if you want to wrap rather than replace them: SlideupMessageView, ModalMessageView, FullScreenMessageView, HTMLMessageView (iOS) / HtmlMessageView (Android).
HTML messages
An html message is a complete HTML document that the default presenter shows full screen in a web view. JavaScript is turned on for that web view only, and the page gets a herald object:
| Call | Effect |
|---|---|
herald.close() | Closes the message. Logs a dismissal if nothing was clicked. |
herald.click(id) | Logs a button click with id and keeps the message open. An empty id is ignored. |
A link the user taps is logged as a click, closes the message and opens outside it: on iOS through Herald.shared.openDeepLink(_:), on Android with an ACTION_VIEW intent. The page itself never navigates away.
<!doctype html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>body { font: -apple-system-body; font-family: system-ui; padding: 24px; }</style>
</head>
<body>
<h1>Strength week</h1>
<p>Five sessions, 20 minutes each.</p>
<button onclick="herald.click('show_plan'); document.getElementById('plan').hidden = false">See the plan</button>
<ol id="plan" hidden><li>Squats</li><li>Push-ups</li><li>Rows</li></ol>
<p><a href="lumen://challenge/strength">Join the challenge</a></p>
<button onclick="herald.close()">Not now</button>
</body>
</html>What the SDK enforces
- No file access. The page is loaded with no base URL. Android turns off file and content access. File,
javascript:,data:,about:andblob:URLs are never handed to your app. - No stored state. On iOS the page gets a non-persistent website data store, so cookies and local storage end with the message. On Android, DOM storage is off.
- No pop-ups or extra windows. Scripts cannot open windows. On iOS, a tapped
target="_blank"link is treated like any other tapped link. - Only link taps leave the page. A navigation counts as a click only when the user tapped a link. Redirects, meta refreshes, script navigations and frame loads are blocked without logging anything.
- Bridge from the top frame (iOS). Calls from iframes are ignored, and so are
herald.clickids longer than 200 characters. - HTTPS content only (Android). Mixed content is blocked. On iOS, App Transport Security applies as it does to the rest of your app.
- A way out. On both platforms the message always has a native close button in the top corner. On iOS it closes on its own if the page fails to load or the web content process ends. On Android it closes if the web view's renderer process dies, and your app keeps running.
Rules for the page author
- Call the bridge only from user actions.
herald.clickandherald.closedo not check for a tap. A page that calls them on load reports clicks that never happened. - Do not navigate from script. It is blocked on both platforms. Use plain links.
- Include a visible close control that calls
herald.close(). Both SDKs add their own close button in the top trailing corner, so leave room for it there. - Include a viewport tag. iOS adds
width=device-widthwhen the page has none; Android does not. - Keep it self-contained. Inline styles and small images. The message syncs ahead of time, but images and fonts the page loads from the network are fetched when it opens, and fail offline.
2. Replace the presenter
To draw messages with your own window or navigation system, implement InAppMessagePresenter and assign it to Herald.shared.inAppMessagePresenter (iOS) or Herald.inAppMessagePresenter (Android). The SDK still decides which message shows and when.
| Rule | iOS | Android |
|---|---|---|
| Method | present(_:onClose:) -> Bool, on the main actor | present(activity, message, callbacks): Boolean, on the main thread |
| Return value | true only when the message is on screen. false keeps it pending. | Same |
| Impression | Logged by the SDK when you return true | Call callbacks.onImpression() when it is visible |
| Clicks | Herald.shared.logClick(message, buttonId:) | callbacks.onClick(buttonId) |
| Dismissal | Herald.shared.logDismissal(message) if the user closed it without clicking | Handled by callbacks.onDismiss() |
| Close | Call onClose exactly once when it leaves the screen | Call callbacks.onDismiss() exactly once |
| Forced removal | Not used | Implement dismissCurrent(): the SDK calls it on sign-out or a user switch |
Verify it works
Load test messages with debugLoadMessages and show each type with debugShowInAppMessage (debug builds only). Tap each control and confirm that exactly one interaction is logged per tap and that the message closes when it should.
Troubleshooting
- The message stays on screen forever (custom view). Your view never calls
dismiss(),buttonClickedorbodyClicked. - No more messages appear after a custom one (custom presenter).
onCloseorcallbacks.onDismiss()was not called, so the SDK still thinks a message is on screen. - An HTML link does nothing. The navigation did not come from a link tap (for example a script set
location), so it was blocked. heraldis undefined in the page. You are testing in a browser. The object exists only inside the SDK's web view.