Edit in GitHubLog an issue

Programmatically control the display of in-app messages

You can now implement a PresentationDelegate or MessagingDelegate(for older Android SDK versions and iOS SDK) in order to be alerted when specific events occur during the lifecycle of an in-app message.

Register the delegate

On Android 3.x, you must implement the PresentationDelegate interface to be alerted of in-app message events. The AEPUIService class, which implements the UIService interface, maintains an optional property that holds reference to the PresentationDelegate.

Kotlin

Copied to your clipboard
// accessed via the public class ServiceProvider which contains a getter for the UIService implementation
interface UIService {
fun setPresentationDelegate(presentationDelegate: PresentationDelegate)
}

On Android 3.x, the custom implementation of PresentationDelegate is registered with the UIService by accessing it using the ServiceProvider.

Kotlin

Copied to your clipboard
val myPresentationDelegate = CustomDelegate()
ServiceProvider.getInstance().uiService.setPresentationDelegate(myPresentationDelegate)

Java

Copied to your clipboard
CustomDelegate myPresentationDelegate = new CustomDelegate();
ServiceProvider.getInstance().getUIService().setPresentationDelegate(myPresentationDelegate);

MessagingDelegate definition

The PresentationDelegate interface extends the PresentationListener and PresentationLever interfaces. It is available in the com.adobe.marketing.mobile.services.ui package.

PresentationDelegate interface

Kotlin

Copied to your clipboard
/**
* A delegate that can be used to observe and control the lifecycle of [Presentation]'s managed by the SDK.
*/
interface PresentationDelegate : PresentationListener, PresentationLever

PresentationListener interface

Kotlin

Copied to your clipboard
/**
* A listener for observing the lifecycle of presentations managed by the SDK.
*/
interface PresentationListener {
/**
* Invoked when a the presentable is shown.
* @param presentable the [Presentable] that was shown
*/
fun onShow(presentable: Presentable<*>)
/**
* Invoked when a presentable is hidden.
* @param presentable the [Presentable] that was hidden
*/
fun onHide(presentable: Presentable<*>)
/**
* Invoked when a presentable is dismissed.
* @param presentable the [Presentable] that was dismissed
*/
fun onDismiss(presentable: Presentable<*>)
/**
* Invoked when the content in the presentable is loaded.
* @param presentable the [Presentable] into which that was loaded
* @param presentationContent optional [PresentationContent] that was loaded into the presentable
*/
fun onContentLoaded(presentable: Presentable<*>, presentationContent: PresentationContent?)
/**
* Defines the types of content that can be loaded into a [Presentable].
*/
sealed class PresentationContent {
/**
* Content loaded from a URL.
* @param url the URL from which the content was loaded
*/
class UrlContent(val url: String) : PresentationContent()
}
}

PresentationLever interface

Kotlin

Copied to your clipboard
/**
* A gating mechanism for implementers to restrict the display of a [Presentable] based on specific
* set of conditions.
*/
interface PresentationLever {
/**
* Returns true if [presentable] can be shown, false otherwise.
* @param presentable the [Presentable] to check if it can be shown
* @return true if [presentable] can be shown, false otherwise
*/
fun canShow(presentable: Presentable<*>): Boolean
}

Retrieve the Message object

The Message class has the business logic related to the in-app message to perform actions like sending tracking events on interactions and suppress tracking. The class that implements PresentationDelegate will receive a Presentable object, which is the Mobile Core class containing in-app message view. You will need to get the Message object associated with Presentable object using the MessagingUtils.getMessageForPresentable(Presentable) .

Kotlin

Copied to your clipboard
var currentMessagePresentable: Presentable<InAppMessage>? = null
override fun onShow(presentable: Presentable<*>) {
if (presentable.getPresentation() !is InAppMessage) {
return
}
currentMessagePresentable = presentable as Presentable<InAppMessage>
val message = MessagingUtils.getMessageForPresentable(currentMessagePresentable)
}

Java

Copied to your clipboard
Presentable<InAppMessage> currentMessagePresentable = null;
@Override
public void onShow(Presentable<?> presentable) {
if (!(presentable.getPresentation() instanceof InAppMessage)) {
return;
}
currentMessagePresentable = (Presentable<InAppMessage>) presentable;
Message message = MessagingUtils.getMessageForPresentable(currentMessagePresentable);
}

Controlling when a message should be shown to the end user

If a custom PresentationDelegate implementation has been set in the UIService, the delegate's canShow method will be called prior to displaying an in-app message for which the end user has qualified. You are responsible for returning true if the message should be shown, or false if the message should be suppressed. If you returned false in the canShow method, you can store a reference to the Message object, and call the show() method on it at a later time.

An example of when you may choose to suppress an in-app message due to the status of some other workflow within the app and show it at a later time upon completion of the other workflow can be seen below:

Kotlin

Copied to your clipboard
var currentMessage: Message? = null
var anotherWorkflowStatus: String = "inProgress"
// Invoked prior to displaying Presentable to check if it can be shown
// if true is returned, the Presentable will be shown immediately
// if false is returned, the message will not be shown
override fun canShow(presentable: Presentable<*>): Boolean {
// canShow is called for all Presentable types: Alert, FloatingButton, InAppMessage
// check the type of Presentable before deciding the return value
if (presentable.getPresentation() !is InAppMessage)
return true
if(anotherWorkflowStatus.equals("inProgress")) {
val currentMessagePresentable = presentable as Presentable<InAppMessage>
// optional : hold this reference for later use
// access the Message for Presentable<InAppMessage>
currentMessage = MessagingUtils.getMessageForPresentable(currentMessagePresentable)
println("message was suppressed: ${currentMessage?.id}")
currentMessage?.track("message suppressed", MessagingEdgeEventType.TRIGGER)
return false
}
return true
}
fun otherWorkflowFinished() {
anotherWorkflowStatus = "complete"
currentMessage?.show()
}

Java

Copied to your clipboard
Message currentMessage = null;
String anotherWorkflowStatus = "inProgress";
// Invoked prior to displaying Presentable to check if it can be shown
// if true is returned, the Presentable will be shown immediately
// if false is returned, the message will not be shown
@Override
public boolean canShow(Presentable<?> presentable) {
// canShow is called for all Presentable types: Alert, FloatingButton, InAppMessage
// check the type of Presentable before deciding the return value
if (!(presentable.getPresentation() instanceof InAppMessage)) {
return true;
}
if(anotherWorkflowStatus.equals("inProgress")) {
Presentable<InAppMessage> currentMessagePresentable = (Presentable<InAppMessage>) presentable;
// optional : hold this reference for later use
// access the Message for Presentable<InAppMessage>
currentMessage = MessagingUtils.getMessageForPresentable(currentMessagePresentable);
if (currentMessage != null) {
System.out.println("message was suppressed: " + currentMessage.getId());
message.track("message suppressed", MessagingEdgeEventType.TRIGGER);
}
return false;
}
return true;
}
public void otherWorkflowFinished() {
anotherWorkflowStatus = "complete";
if (currentMessage != null) {
currentMessage.show();
}
}

Examples

The test apps in this repository demonstrate using a MessagingDelegate:

Further reading

  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.