In-app messages in your app
What to add to your Android, iOS or web app so in-app messages appear, with every method and a test checklist.
Once the PushFyre SDK is in your app, in-app messages need no extra code to appear. This page covers what must be in place, the optional calls for showing a message on a particular screen or action, and how to test.
What you need
- The latest SDK version. In-app messages arrived after the first SDK release, so update the Android, iOS or web SDK first.
- `PushFyre.initialize` called at app start. Do it once, in
Application.onCreate(Android) ordidFinishLaunchingWithOptions(iOS). The web script does this for you. - A reachable API address over HTTPS. The same
apiBaseUrlyou use for push. iOS blocks plainhttp://addresses unless your app has an exception for them. - A screen in front of the user. A message is shown on the Activity or window that is visible. If the app is in the background, it waits briefly for the next screen, then gives up.
- A live campaign that targets the platform, the audience and the trigger. See In-app messages.
Nothing else is needed: no new permission, no manifest or entitlement change, no extra library, and no push permission from the user.
What the SDK does for you
- Registers the device, then asks the server for the messages that apply to it when the app opens.
- Shows one message at a time, in the format you chose, and records impressions, clicks and dismissals.
- Keeps frequency limits on the device, so a message marked "once" never comes back, even after a restart.
- Ignores any failure quietly. A network error or a bad image link never crashes or blocks your app.
Method reference
| Call | What it does |
|---|---|
initialize | Starts the SDK. Required once per launch. Fetches messages for the app-open trigger after registering the device. |
trigger(eventName, properties?) | Records a custom event and sets an in-app trigger whose key is that name and whose value is true. A live message with that In-App Trigger is shown if the person qualifies. See Custom events. |
addTrigger(key, value) | Sets an in-app trigger key to any value for this session. It does not record a custom event. Use it when the campaign value is not true. |
setInAppMessagesEnabled(false) | Stops showing in-app messages in this process, for example during onboarding or a payment flow. Call it with true to resume. |
setTapHandler | Lets your app handle a tapped link first. For in-app buttons the action id is in_app. Return true if you handled it, false to let the SDK open the link. On Android and iOS it also receives Action name click actions, with your action id and no link. |
PushFyre.onInAppClick(handler) (web) | Receives Action name click actions on the web as { actionId, messageId }. The same detail is sent as the pushfyre:inapp-click event on document. |
login, sendTag | Not specific to in-app messages, but segments and conditions use them. A message aimed at a segment or tag needs the device to have those set. |
Android
Add the SDK and initialize it as described in Android SDK setup. That is enough for app-open messages.
Show a message when a screen opens
override fun onResume() {
super.onResume()
PushFyre.trigger(this, "portfolio_screen_opened")
}In the campaign, open Display Settings, choose When certain conditions are satisfied during the session, and add an In-App Trigger with Key portfolio_screen_opened and Value true.
Handle the button yourself
If the button opens a link or screen inside your app, register a tap handler once, after initialize. Return true only for the links you handle.
PushFyre.setTapHandler { context, targetUrl, actionId ->
if (actionId == "in_app" && targetUrl != null && targetUrl.startsWith("myapp://")) {
context.startActivity(
Intent(context, MainActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)
putExtra("targetUrl", targetUrl)
}
)
true
} else {
false
}
}in_app and return false for everything else, so push keeps its normal behaviour.iOS
Add the package and initialize it as described in iOS SDK setup. That is enough for app-open messages.
Show a message when a screen opens
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
PushFyre.trigger("portfolio_screen_opened")
}Handle the button yourself
PushFyre.setTapHandler { targetUrl, actionId in
guard actionId == "in_app", let url = targetUrl, url.hasPrefix("myapp://") else {
return false
}
Router.open(url) // your own routing
return true
}http:// server, add an App Transport Security exception to your debug build only.Web
Pages that load your PushFyre script show messages with no extra code. To restrict a message to certain pages, use the Pages setting in the campaign.
window.PushFyreDeferred = window.PushFyreDeferred || [];
PushFyreDeferred.push(function (PushFyre) {
// Show a message whose trigger is this event
PushFyre.trigger("checkout_started");
});PushFyre.trigger from your router when the view changes, and use a custom event as the campaign trigger. The Pages setting ignores everything after a #, so it cannot tell #!/dashboard from #!/home.Handle click actions in your code
A campaign can give an image, a button, the background or the close button click actions. Tag user and Send outcome need no code: the SDK sets the tag and records the outcome itself. Action name hands your action id to your code, so you decide what happens.
window.PushFyreDeferred = window.PushFyreDeferred || [];
PushFyreDeferred.push(function (PushFyre) {
PushFyre.onInAppClick(function (event) {
// event.actionId is the Action name from the campaign, event.messageId the campaign
if (event.actionId === "open_cart") openCart();
});
});
// Or listen for the DOM event
document.addEventListener("pushfyre:inapp-click", function (event) {
console.log(event.detail.actionId, event.detail.messageId);
});in_app or with push button ids, such as open_cart or start_trial.Apps that show a website in a WebView
If your app is a shell around a website, the SDK sees one screen, not the pages inside it. Report each page change as an event, and make that event the campaign trigger. This example turns https://example.com/app/#!/dashboard into the event page_dashboard.
private var lastPageUrl: String? = null
private fun reportPage(url: String?) {
if (url == null || url == lastPageUrl) return
val route = Regex("#!/([A-Za-z0-9_]+)").find(url)?.groupValues?.get(1) ?: return
lastPageUrl = url
PushFyre.trigger(applicationContext, "page_" + route.lowercase())
}
webView.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) = reportPage(url)
override fun doUpdateVisitedHistory(view: WebView, url: String, isReload: Boolean) = reportPage(url)
}Then, in the campaign, open Display Settings, choose When certain conditions are satisfied during the session, and add an In-App Trigger with Key page_dashboard and Value true. If the website itself also loads the PushFyre web script, leave Web unticked in the campaign's platforms, or the message can appear twice.
Test it
- Run a debug build. With
environmentset from your build type, as in the setup guide, a debug build reports itself as a test device. - Create a campaign aimed only at test devices: under Target audience choose Custom conditions, then add the condition
environmentequalstest. Real users will not see it. - Set the platform and the display conditions (app open, or your In-App Trigger), then Launch campaign.
- Open the app. For an event trigger, go to the screen that fires it.
- Check In-App Messages, Analytics in the portal. An impression means the message was shown. Tap the button and a click appears.
If nothing appears
| Check | Why it matters |
|---|---|
| Is the campaign Live? | A draft, paused, ended or not-yet-started campaign is never sent to devices. |
| Is the platform ticked? | A campaign for Android is not sent to iOS or the web. |
| Is this web page in the list? | With Web ticked, an empty Web pages list allows every page. /pricing matches that path. /blog/* matches that folder. The text after ? is ignored. |
| Does the trigger key match? | The In-App Trigger key is compared exactly with the name passed to PushFyre.trigger. Dashboard_Opened and dashboard_opened are different keys. Custom events stored for segments and journeys are lowercase. |
| Is the device in the audience? | Segments and conditions need a registered device with the right tags. Try All Users to rule this out. |
| Was it shown already? | Frequency settings are kept on the device. Reinstall the app, or set the frequency to every time. |
Did initialize run before trigger? | Calls made before initialize are ignored. |
| Can the phone reach the API? | The SDK cannot fetch messages offline, and iOS needs HTTPS. |
| Is the app in the foreground? | A message waits a few seconds for a visible screen, then is dropped until the next trigger. |
| Is another message on screen? | Only one shows at a time. The higher priority goes first. |
| Is the image link public https? | A broken link still shows the message, without the picture. |
Related: In-app messages, Tags and Segments.