On Android, the delegate is registered with the Adobe Service Provider. The ServiceProvider
class maintains an optional property that holds reference to the FullscreenMessaageDelegate
.
Java
Copied to your clipboard// defined in public class ServiceProviderpublic void setMessageDelegate(MessagingDelegate messageDelegate)
On Android, the delegate is registered with Mobile Core. The MobileCore
contains a method to set theMessagingDelegate
withing the ServiceProvider
class.
Copied to your clipboardCustomDelegate myMessagingDelegate = new CustomDelegate();MobileCore.setMessagingDelegate(myMessagingDelegate);
On iOS, the delegate is registered with Mobile Core. The MobileCore
framework maintains an optional property that holds reference to the MessagingDelegate
.
Swift
Copied to your clipboard/// defined in MobileCore.swift@objc public static var messagingDelegate: MessagingDelegate?
Assuming that InAppMessagingHandler
is a class that implements MessagingDelegate
, execute the following code to set the delegate in MobileCore
:
Copied to your clipboardlet myMessagingDelegate = InAppMessagingHandler()MobileCore.messagingDelegate = myMessagingDelegate
The MessagingDelegate
interface, which is implemented in the AEPServices
framework, is defined below:
Java
Copied to your clipboard/*** UI Message delegate which is used to listen for current message lifecycle events and control if* the message should be displayed.*/public interface MessagingDelegate {/*** Invoked when a message is displayed.** @param message {@link FullscreenMessage} that is being displayed*/default void onShow(final FullscreenMessage message) {Log.debug(ServiceConstants.LOG_TAG, "MessagingDelegate", "Fullscreen message shown.");}/*** Invoked when a message is dismissed.** @param message {@link FullscreenMessage} that is being dismissed*/default void onDismiss(final FullscreenMessage message) {Log.debug(ServiceConstants.LOG_TAG, "MessagingDelegate", "Fullscreen message dismissed.");}/*** Used to determine if a message should be shown.** @param message {@link FullscreenMessage} that is about to get displayed* @return true if the message should be displayed, false otherwise*/boolean shouldShowMessage(final FullscreenMessage message);/*** Called when the {@link FullscreenMessage} loads a url.** @param url {@code String} being loaded by the {@code FullscreenMessage}* @param message {@link FullscreenMessage} loading a url {@code String}*/default void urlLoaded(final String url, final FullscreenMessage message) {Log.debug(ServiceConstants.LOG_TAG,"MessagingDelegate","Fullscreen message loaded url: %s",url);}}
On iOS, the MessagingDelegate
protocol, which is implemented in the AEPServices
framework, is defined below:
Swift
Copied to your clipboard/// UI Message delegate which is used to listen for current message lifecycle events@objc(AEPMessagingDelegate)public protocol MessagingDelegate {/// Invoked when a message is displayed/// - Parameters:/// - message: UIMessaging message that is being displayed@objc(onShow:)func onShow(message: Showable)/// Invoked when a message is dismissed/// - Parameters:/// - message: UIMessaging message that is being dismissed@objc(onDismiss:)func onDismiss(message: Showable)/// Used to find whether messages should be shown or not////// IMPORTANT! - this method is called on a background thread./// Any direct interactions with the Message's WKWebView made by the delegate/// should be dispatched back to the main thread.////// - Parameters:/// - message: UIMessaging message that is about to get displayed/// - Returns: true if the message should be shown else false@objc(shouldShowMessage:)func shouldShowMessage(message: Showable) -> Bool/// Called when `message` loads a URL/// - Parameters:/// - url: the `URL` being loaded by the `message`/// - message: the Message loading a `URL`@objc(urlLoaded:byMessage:)optional func urlLoaded(_ url: URL, byMessage message: Showable)}
The user interface methods (except for onShowFailure()
) in a MessagingDelegate
implementation will be passed an AEPMessage
object. An AEPMessage
object is the Android Core implementation of the FullscreenMessage
interface. It contains a reference to the parent Message
class and is your primary way to interact with the message.
A reference to the AEPMessage
object can be obtained by calling fullscreenMessage.getParent()
. An example of how to access the Message
in the onShow
delegate method can be seen below:
Java
Copied to your clipboard@Overridepublic void onShow(FullscreenMessage fullscreenMessage) {Message message = (Message) fullscreenMessage.getParent();System.out.println("message was shown: " + message.id);}
Each of the methods implemented in the MessagingDelegate
will be passed a Showable
object. In the AEPMessaging SDK, the class implementing Showable
is FullscreenMessage
. A FullscreenMessage
object is wrapped in the Message
class, and is your primary way to interact with the message.
To get a reference to the Message
object:
- Convert the
Showable
message parameter toFullscreenMessage
- Access the
parent
variable (note thatparent
is variable defined inFullscreenMessage+Message.swift
, an extension in the AEPMessaging framework)
An example of how to access the Message
in the onShow
delegate method can be seen below:
Swift
Copied to your clipboardfunc onShow(message: Showable) {let fullscreenMessage = message as? FullscreenMessagelet message = fullscreenMessage?.parentprint("message was shown \(message?.id ?? "undefined")")}
If a custom MessagingDelegate
has been set in the ServiceProvider
, this delegate's shouldShowMessage
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.
An example of when you may choose to suppress an in-app message due to the status of some other workflow within the app can be seen below:
Java
Copied to your clipboard@Overridepublic boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {if (someOtherWorkflowStatus == "inProgress") {return false;}return true;}
Another option is to store a reference to the FullscreenMessage
object, and call the show()
method on it at a later time.
Continuing with the above example, after you have stored the message that was triggered initially, you can choose to show it upon completion of the other workflow:
Copied to your clipboardMessage currentMessage = null;String anotherWorkflowStatus;public void otherWorkflowFinished() {anotherWorkflowStatus = "complete";currentMessage.show();}@Overridepublic boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {if (someOtherWorkflowStatus.equals("inProgress")) {// store the current message for later usecurrentMessage = (Message) fullscreenMessage.getParent();return false;}return true;}
If a MessagingDelegate
has been provided to MobileCore
, the delegate's shouldShowMessage
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.
An example of when you may choose to suppress an in-app message due to the status of some other workflow within the app can be seen below:
Swift
Copied to your clipboardfunc shouldShowMessage(message: Showable) -> Bool {if someOtherWorkflowStatus == "inProgress" {return false}return true}
Another option is to store a reference to the Message
object, and call the show()
method on it at a later time.
Continuing with the above example, after you have stored the message that was triggered initially, you can choose to show it upon completion of the other workflow:
Copied to your clipboardvar currentMessage: Message?func otherWorkflowFinished() {anotherWorkflowStatus = "complete"currentMessage?.show()}func shouldShowMessage(message: Showable) -> Bool {if someOtherWorkflowStatus == "inProgress" {let fullscreenMessage = message as? FullscreenMessage// store the current message for later usecurrentMessage = fullscreenMessage?.parentreturn false}return true}
If you would like to manually integrate the View
that contains the UI for an in-app message, you can do so by accessing the WebView
directly in a MessagingDelegate
method.
In the example below, you can decide whether or not the in-app message should be directly integrated into your existing UI. If so, you capture a reference to the message's WebView
and return false
to prevent the message from being shown by the SDK:
Java
Copied to your clipboardprivate Message currentMessage = null;private boolean shouldIntegrateMessageDirectly = true;private MessageWebView inAppMessageView;@Overridepublic boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {if (shouldIntegrateMessageDirectly) {this.currentMessage = (Message) fullscreenMessage.getParent();// cast to MessageWebView to access the startInAppMessage functioninAppMessageView = (MessageWebView) currentMessage.getWebView();return false;}return true;}
If you would like to manually integrate the View
that contains the UI for an in-app message, you can do so by accessing the WKWebView
directly in a MessagingDelegate
method.
IMPORTANT! - The shouldShowMessage
delegate method is called on a background thread. Any direct interactions with the Message's WKWebView
made by the delegate should be dispatched back to the main thread.
In the example below, you can decide whether or not the in-app message should be directly integrated into your existing UI. If so, you capture a reference to the message's WKWebView
and return false
to prevent the message from being shown by the SDK:
Swift
Copied to your clipboardvar inAppMessageView: WKWebView?func shouldShowMessage(message: Showable) -> Bool {if shouldIntegrateMessageDirectly {let fullscreenMessage = message as? FullscreenMessagelet message = fullscreenMessage?.parentinAppMessageView = message?.view as? WKWebViewreturn false}return true}