In-app messages
Controlling display
Use the in-app message delegate to show, hold or drop a message, show held messages with showNext, and keep messages away from onboarding and checkout.
Set a delegate to decide, message by message, whether it shows now, waits, or is dropped. Use it to keep messages out of flows where an interruption costs you, such as onboarding, checkout or a video.
Before you start
- The default presenter, or your own, is installed. See the overview.
1. Set a delegate
The delegate returns one of three choices:
| Choice | iOS | Android | Effect |
|---|---|---|---|
| Show now | .display | InAppDisplayDecision.DISPLAY | The message appears. |
| Later | .later | InAppDisplayDecision.LATER | The message stays pending. It is offered again automatically, or at once with showNext(). |
| Drop | .discard | InAppDisplayDecision.DISCARD | This occurrence is dropped. Nothing is recorded, so the message stays eligible for its next trigger. |
With no delegate, every message shows.
import HeraldSDK
@MainActor
final class MessageGate: InAppMessageDelegate {
static let shared = MessageGate()
var isBusy = false
func shouldDisplay(_ message: InAppMessage) -> InAppMessageDisplayChoice {
if isBusy { return .later }
// Never interrupt with a full-screen takeover; a banner is fine.
if message.type == .full { return .discard }
return .display
}
}
// At launch, after configure. The SDK holds the delegate weakly: keep a strong reference.
Herald.shared.inAppMessageDelegate = MessageGate.sharedimport com.herald.sdk.Herald
import com.herald.sdk.InAppDisplayDecision
import com.herald.sdk.InAppMessage
import com.herald.sdk.InAppMessageDelegate
import com.herald.sdk.InAppMessageType
object MessageGate : InAppMessageDelegate {
@Volatile var isBusy = false
override fun shouldDisplay(message: InAppMessage): InAppDisplayDecision = when {
isBusy -> InAppDisplayDecision.LATER
// Never interrupt with a full-screen takeover; a banner is fine.
message.type == InAppMessageType.FULL -> InAppDisplayDecision.DISCARD
else -> InAppDisplayDecision.DISPLAY
}
}
// In Application.onCreate, after configure.
Herald.inAppMessageDelegate = MessageGateThreading. The delegate runs on the main thread on both platforms (the main actor on iOS). Return quickly. If an Android delegate throws, the SDK shows the message and logs the error.
When it is asked. The delegate is asked when a message is about to appear, not when its trigger fires, so it sees your app's state at that moment. It can be asked more than once for the same message: after a later, and when it answered show now but there was no screen to draw on. Keep it free of side effects such as analytics calls.
2. Hold messages during onboarding or checkout
Set your flag when the sensitive flow starts. When it ends, clear the flag and call showNext() to show the message that waited.
func checkoutDidStart() {
MessageGate.shared.isBusy = true
}
func checkoutDidFinish() {
MessageGate.shared.isBusy = false
Herald.shared.showNext()
}fun onCheckoutStarted() {
MessageGate.isBusy = true
}
fun onCheckoutFinished() {
MessageGate.isBusy = false
Herald.showNext()
}showNext() offers the highest-priority waiting message now, ignoring the minimum interval; on a tie, the one that has waited longest. Your delegate is asked again. It does nothing while another message is on screen or when nothing is waiting.
You do not have to call showNext(). A message held with later is also offered again on its own the next time the app comes to the foreground, a trigger fires or a message closes. showNext() makes it show as soon as your flow ends instead.
3. Route message clicks (iOS)
Button and body clicks open their URL through Herald.shared.openDeepLink(_:). Set deepLinkHandler to route your own links inside the app; return true when you handled the URL, false to let the system open it.
Herald.shared.deepLinkHandler = { url in
guard url.scheme == "lumen" else { return false }
AppRouter.shared.open(url)
return true
}On Android, clicks go to Herald.deepLinkHandler first, with the same semantics. When it is null or returns false, the SDK opens the link with an ACTION_VIEW intent, so register your scheme with an intent filter on the activity that should handle it.
Verify it works
- Set the busy flag and trigger a message (for example with a session start). Nothing appears.
- Clear the flag and call
showNext(). The message appears. - With debug logging on, a dropped message logs
discarded by delegateon Android. On iOS nothing is logged for a drop.
Troubleshooting
- The delegate is never called (iOS). The SDK keeps a weak reference. If nothing else holds your delegate object, it is released right after you assign it.
- A held message shows later than expected. It is offered again at the next foreground, trigger or close. Call
showNext()when your flow ends to show it at once. - A message shows during checkout anyway. The message was already on screen when the flow started. Set the flag before the flow's first screen appears.