In-App Messages
The SDK shows in-app messages designed in the panel and reports when they are displayed, clicked and dismissed. How in-app messages work across platforms: In-App Messages.
Requirements
- The SDK initialized in
Application.onCreate, see Installation. - The FCM token passed to the SDK, see Push Token.
onMessageReceivedpassing the message tohandleNotification.
In-app messages do not need the notification permission. They also reach users who turned notifications off.
Setup
In-app messages arrive as data only FCM messages, which Android always passes to onMessageReceived. Forward them to the SDK:
- Kotlin
- Java
override fun onMessageReceived(message: RemoteMessage) {
if (UserCom.default?.handleNotification(message.data) == true) return
// your own messages
}
@Override
public void onMessageReceived(@NonNull RemoteMessage message) {
UserCom sdk = UserCom.getDefault();
if (sdk != null && sdk.handleNotification(message.getData())) return;
// your own messages
}
The SDK needs no other code to show messages. It follows your activities from the moment initialize runs, which is why initialize belongs in Application.onCreate.
With the app open, send an in-app message to your test contact. It appears on screen and is counted as displayed in the panel.
Handling
When a message is shown
- App in the foreground: the message is shown at once.
- App in the background, phone locked, or app closed: the SDK keeps the newest message and shows it the next time the user opens the app, if it arrived less than 6 hours earlier. A newer message replaces an older one, and each message is shown once.
logoutdiscards a waiting message.
"Displayed" is reported when the user can see the message: the phone is unlocked and no dialog or splash screen covers it.
If the SDK is not initialized when the message arrives, for example because initialize runs in an activity and the app was closed, the message is lost.
How a message is shown
The SDK draws the message as a layer over the screen of the current activity, not as a separate activity. When your app moves to another activity, for example from a splash screen to the main screen, the message moves with it. It stays on screen after a rotation, without a second "displayed" report.
- Your own dialogs appear above the message.
- The message respects the status bar, the navigation bar, the display cutout and the keyboard.
- The back button and the back gesture close the message, and the next back press goes to your app.
- TalkBack reads the message and its buttons. It can also move to your app's content under the message.
- Images and GIFs from the panel are shown. GIFs are animated on Android 9 and later. Older versions show the first frame. The SDK waits at most 5 seconds for images.
Pausing in-app messages
To keep messages away from a screen, for example a payment screen, or from your UI tests, turn them off:
- Kotlin
- Java
// entering the payment screen
UserCom.inAppMessagesEnabled = false
// leaving it
UserCom.inAppMessagesEnabled = true
UserCom.setInAppMessagesEnabled(false);
// ...
UserCom.setInAppMessagesEnabled(true);
While it is false, new messages wait, within the same 6 hours, and the newest one appears when you set it back to true. A message already on screen stays. The setting is kept only while the app process lives: at every start it is true again. For UI tests, set it to false at every start of the test app.
Button taps
A tap on a button always reports a click and closes the message. If the button has a link, the SDK passes it to your inAppMessageClickHandler first, on the main thread:
- return
true: you handled the link, the SDK does nothing more, - return
false, or no handler: the SDK opens the link. Anhttporhttpslink opens in Custom Tabs, any other link with anACTION_VIEWintent. If no app on the device can open it, nothing happens.
To open screens of your own app from a button, handle the link in inAppMessageClickHandler. See Opening App Screens.
- Kotlin
- Java
UserCom.default?.inAppMessageClickHandler = InAppMessageClickHandler { url ->
if (url.scheme != "myapp" && url.host != "www.example.com") {
return@InAppMessageClickHandler false // let the SDK open it
}
// route to the matching screen
true
}
UserCom sdk = UserCom.getDefault();
if (sdk != null) {
sdk.setInAppMessageClickHandler(url -> {
if (!"myapp".equals(url.getScheme()) && !"www.example.com".equals(url.getHost())) {
return false; // let the SDK open it
}
// route to the matching screen
return true;
});
}
The close button and the back button close the message and report "dismissed". A tap outside the message does nothing.
Custom fonts
Without a fontResolver, every message uses the default font. To use the fonts set in the panel, add them to your app and set fontResolver. When the resolver returns null, the SDK uses the default font.
Texts for accessibility
The SDK has two texts, used by TalkBack. Override and translate them in your app's res/values*/strings.xml:
| String | Default | Used for |
|---|---|---|
user_com_in_app_message_title | Message | The name of the message |
user_com_in_app_message_close_button | Close | The close button |
The SDK has no translations of its own.
Methods
inAppMessagesEnabled
A static Boolean property of UserCom, default true. While false, new in-app messages wait and are shown when it is set back to true. Kept only in memory, true at every start of the app process. In Java: UserCom.setInAppMessagesEnabled(boolean). See Pausing in-app messages.
inAppMessageClickHandler
Receives the link of every tapped button before the SDK opens it, on the main thread.
Optional unless marked required
- inAppMessageClickHandler
onClick(url: Uri): Booleanreceives the button's link, including custom schemes. Returntrueif you handled it.
The click is reported to the panel in both cases.
fontResolver
Provides fonts for text in in-app messages. Set it after initializing the SDK.
Optional unless marked required
- fontResolver
resolveFont(name: String, size: Float): Typeface?receives the font name and size in sp from the message. Returnnullto use the default font.
The SDK sets the size, bold and italic itself. Return the plain typeface of the font.
- Kotlin
- Java
UserCom.default?.fontResolver = InAppFontResolver { name, _ ->
when (name) {
"Inter" -> ResourcesCompat.getFont(this, R.font.inter)
else -> null
}
}
UserCom sdk = UserCom.getDefault();
if (sdk != null) {
sdk.setFontResolver((name, size) ->
"Inter".equals(name) ? ResourcesCompat.getFont(this, R.font.inter) : null);
}
customTabsIntent
The CustomTabsIntent the SDK uses to open http and https links of buttons. Default: a plain CustomTabsIntent. Set null to open every link with an ACTION_VIEW intent, usually in the default browser.
- Kotlin
- Java
UserCom.default?.customTabsIntent = CustomTabsIntent.Builder()
.setShowTitle(true)
.build()
UserCom sdk = UserCom.getDefault();
if (sdk != null) {
sdk.setCustomTabsIntent(new CustomTabsIntent.Builder().setShowTitle(true).build());
}
Deprecated methods
These stay until 3.0:
| Deprecated | Use instead |
|---|---|
setInAppNotificationClickHandler(ClickHandler) | inAppMessageClickHandler. The old handler takes every link: the SDK does not open links while it is set. |
setFontResolver(FontResolver) | fontResolver, which also gets the font size |
Builder.openLinksInChromeCustomTabs, Builder.setCustomTabsBuilder | customTabsIntent |
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Push notifications work, in-app messages never appear | onMessageReceived does not call handleNotification, or another library's messaging service receives the messages. | Forward the message as in Setup, and check which service handles com.google.firebase.MESSAGING_EVENT in the merged manifest. |
| Messages sent while the app was closed never appear | The SDK is initialized outside Application.onCreate, so it does not exist when the message arrives. | Initialize in Application.onCreate. |
| Messages appear only after the app comes back from the background | The SDK is initialized after the first activity resumed, for example by a cross platform wrapper. | Initialize in Application.onCreate. |
| No message appears at all | UserCom.inAppMessagesEnabled is false, or the message is older than 6 hours. | Check where your app sets the switch. |
| "Displayed" is reported only after your dialog closes | Your dialog is above the message. | Expected. "Displayed" is reported once the user can see the message. |
| Button does nothing except closing the message | No app can open the link, or your handler returned true without routing. | Handle the link in inAppMessageClickHandler. |
| Text uses the wrong font | No fontResolver, or it returns null for that name. | Set fontResolver and return the font for the name used in the panel. |
More cases: Android Troubleshooting.