Implement In-App Messaging on Android
To use certain in-app messaging features, such as button actions, you must change some SDK configurations.
Required Methods for Button Actions
Marketers can configure the action that occurs when an end user taps a button on an in-app message. The SDK handles actions for Notification Settings and Location Settings, while actions for Web URL and App URL require that you implement UrlHandler as a new SDK initialization method.
The following example shows how you can configure the SDK to handle button clicks in in-app messages.
1SFMCSdk.configure(applicationContext as Application, SFMCSdkModuleConfig.build {
2 inAppMessagingFeatureModuleConfig = InAppMessagingFeatureConfig.builder().apply {
3 // Other configuration options
4 // Tell the SDK how to handle button clicks in an In-App Message
5 setUrlHandler { context, url, _ ->
6 getActivity(
7 context,
8 Random().nextInt(),
9 Intent(Intent.ACTION_VIEW, url.toUri()),
10 PendingIntent.FLAG_UPDATE_CURRENT
11 )
12 }
13 }.build()
14}) {
15 // TODO handle initialization status
16}Optional Methods
To control aspects of message display and to get information about the in-app message display lifecycle, use the SDK’s optional in-app messaging EventListener
After the SDK’s initialization is complete, you can register your in-app message EventListener with InAppMessageManager.
1// Set at configuration time
2SFMCSdk.configure(applicationContext as Application, SFMCSdkModuleConfig.build {
3 inAppMessagingFeatureModuleConfig = InAppMessagingFeatureConfig.builder().apply {
4 setEventListener(object : InAppMessageManager.EventListener {
5 override fun shouldShowMessage(message: InAppMessage): Boolean {
6 return if (shouldShowMessage /* Your Custom logic confirming to show in-app message */) {
7 true
8 } else {
9 // Store message id for later
10 val blockedMessageId = message.id
11 false
12 }
13 }
14 override fun didShowMessage(message: InAppMessage) = Unit
15 override fun didCloseMessage(message: InAppMessage, action: InAppMessageCloseAction) = Unit
16 })
17 }.build()
18})
19
20// Or set at anytime after configuration
21InAppMessagingFeature.requestSdk { inAppMessagingFeatureModule ->
22 inAppMessagingFeatureModule.getInAppMessageManager().setInAppMessageListener(...)
23}didShowMessage and didCloseMessage
The didShowMessage and didCloseMessage callback methods help ensure that you can appropriately manage your app’s view state. In-app messages are shown in an activity controlled by the SDK within your app’s activity stack. Your app is required to respond to a view appearing or disappearing.
-
didShowMessageis invoked when the in-app message is initially presented. -
didCloseMessageis invoked after the user closes the message and includes the close reason.
Prevent or Delay Message Display
You can delay or prevent an in-app message’s display. For example, you can choose to prevent an in-app message from displaying during the loading process, sign-in flow, and other situations. To prevent or delay message display, set the shouldShowMessage method to return false.
Furthermore, you can capture data of any in-app message and use it for displaying the specific message at a later time. For example, you can present the message after an end user has successfully signed in by capturing the ID of the message and showing that message later, as shown in this example.
1InAppMessagingFeature.requestSdk { inAppMessagingFeatureModule ->
2 inAppMessagingFeatureModule.getInAppMessageManager().showMessage(blockedMessageInd)
3}Customize Display
By default, in-app messages use your device’s system font. However, you can override the default font face to customize the appearance of an in-app message’s title, body, and button labels.
You can’t alter the font size since the message’s design defines it.
Note
To set the display font, pass the SDK a valid TypeFace for the device’s installed fonts, or your app’s custom fonts as shown in this example.
1// set at configuration time
2SFMCSdk.configure(applicationContext as Application, SFMCSdkModuleConfig.build {
3 inAppMessagingFeatureModuleConfig = InAppMessagingFeatureConfig.builder().apply {
4 setTypeface(appFontFace)
5 }.build()
6})
7
8// OR set after configuration time
9InAppMessagingFeature.requestSdk { inAppMessagingFeatureModule ->
10 inAppMessagingFeatureModule.getInAppMessageManager().setTypeface(appFontFace)
11}Set Status Bar Color
The SDK doesn’t use AppCompatActivity and can’t inherit your app’s status bar color automatically. Instead, you must define a custom status bar color. The SDK applies this color on Android API versions that support the status bar color, which is Lollipop and newer.
1// set at configuration time
2SFMCSdk.configure(applicationContext as Application, SFMCSdkModuleConfig.build {
3 inAppMessagingFeatureModuleConfig = InAppMessagingFeatureConfig.builder().apply {
4 setStatusBarColor(
5 ContextCompat.getColor(this as Context, R.color.colorPrimary)
6 )
7 }.build()
8})
9
10// OR set after configuration time
11InAppMessagingFeature.requestSdk { inAppMessagingFeatureModule ->
12 inAppMessagingFeatureModule.getInAppMessageManager().setStatusBarColor(
13 ContextCompat.getColor(this as Context, R.color.colorPrimary)
14 )
15}