Implement a MessagingDelegate
You can now implement a MessagingDelegate in order to be alerted when specific events occur during the lifecycle of an in-app message.
iOS
Android

Register the delegate with MobileCore

The MobileCore framework maintains an optional property that holds reference to the MessagingDelegate.
Swift
1
/// defined in MobileCore.swift
2
@objc public static var messagingDelegate: MessagingDelegate?
Copied!
Assuming that InAppMessagingHandler is a class that implements MessagingDelegate, execute the following code to set the delegate in MobileCore:
Swift
1
let myMessagingDelegate = InAppMessagingHandler()
2
3
MobileCore.messagingDelegate = myMessagingDelegate
Copied!

Register the delegate with the Adobe Service Provider

The ServiceProvider class maintains an optional property that holds reference to the FullscreenMessaageDelegate.
Java
1
// defined in public class ServiceProvider
2
public void setMessageDelegate(FullscreenMessageDelegate messageDelegate)
Copied!
Java
1
CustomDelegate myMessagingDelegate = new CustomDelegate();
2
ServiceProvider.getInstance().setMessageDelegate(myMessagingDelegate);
Copied!
iOS
Android

MessagingDelegate protocol (iOS)

The MessagingDelegate protocol, which is implemented in the AEPServices framework, is defined below:
Swift
1
/// UI Message delegate which is used to listen for current message lifecycle events
2
@objc(AEPMessagingDelegate)
3
public protocol MessagingDelegate {
4
/// Invoked when any message is displayed
5
/// - Parameters:
6
/// - message: UIMessaging message that is being displayed
7
@objc
8
func onShow(message: Showable)
9
10
/// Invoked when any message is dismissed
11
/// - Parameters:
12
/// - message: UIMessaging message that is being dismissed
13
@objc
14
func onDismiss(message: Showable)
15
16
/// Used to find whether messages should be shown or not
17
/// - Parameters:
18
/// - message: UIMessaging message that is about to get displayed
19
/// - Returns: true if the message should be shown else false
20
@objc
21
func shouldShowMessage(message: Showable) -> Bool
22
23
/// Called when `message` loads a URL
24
/// - Parameters:
25
/// - url: the `URL` being loaded by the `message`
26
/// - message: the Message loading a `URL`
27
@objc
28
optional func urlLoaded(_ url: URL, byMessage message: Showable)
29
}
Copied!

FullscreenMessageDelegate interface (Android)

The FullscreenMessageDelegate interface, which is implemented in the Android Messaging extension in the MessagingDelegate class, is defined below:
Java
1
/**
2
* Delegate for Messaging extension in-app message events.
3
*/
4
public interface FullscreenMessageDelegate {
5
/**
6
* Invoked when the in-app message is displayed.
7
*
8
* @param message FullscreenMessage the in-app message being displayed
9
*/
10
void onShow(final FullscreenMessage message);
11
12
/**
13
* Invoked when the in-app message is dismissed.
14
*
15
* @param message FullscreenMessage the in-app message being dismissed
16
*/
17
void onDismiss(final FullscreenMessage message);
18
19
/**
20
* Used to determine if the in-app message should be shown.
21
*
22
* @param message FullscreenMessage the in-app message that is about to get displayed
23
*/
24
boolean shouldShowMessage(final FullscreenMessage message);
25
26
/**
27
* Invoked when the in-app message is attempting to load a url.
28
*
29
* @param message FullscreenMessage the in-app message attempting to load the url
30
* @param url String the url being loaded by the message
31
*
32
* @return True if the core wants to handle the URL (and not the fullscreen message view implementation)
33
*/
34
boolean overrideUrlLoad(final FullscreenMessage message, final String url);
35
36
/**
37
* Invoked when the in-app message failed to be displayed.
38
*/
39
void onShowFailure();
40
}
Copied!
iOS
Android

Using the Showable object in the protocol methods

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:
  1. 1.
    Convert the Showable message parameter to FullscreenMessage
  2. 2.
    Access the parent variable (note that parent is variable defined in FullscreenMessage+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
1
func onShow(message: Showable) {
2
let fullscreenMessage = message as? FullscreenMessage
3
let message = fullscreenMessage?.parent
4
print("message was shown \(message?.id ?? "undefined")")
5
}
Copied!

Retrieving the Message object from the implemented interface methods

The user interface methods (except for onShowFailure()) in a FullscreenMessageDelegate 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
1
@Override
2
public void onShow(FullscreenMessage fullscreenMessage) {
3
Message message = (Message) fullscreenMessage.getParent();
4
System.out.println("message was shown: " + message.id);
5
}
Copied!

Controlling when a message should be shown to the end user

iOS
Android
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
1
func shouldShowMessage(message: Showable) -> Bool {
2
if someOtherWorkflowStatus == "inProgress" {
3
return false
4
}
5
6
return true
7
}
Copied!
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:
Swift
1
var currentMessage: Message?
2
3
func otherWorkflowFinished() {
4
anotherWorkflowStatus = "complete"
5
currentMessage?.show()
6
}
7
8
func shouldShowMessage(message: Showable) -> Bool {
9
if someOtherWorkflowStatus == "inProgress" {
10
let fullscreenMessage = message as? FullscreenMessage
11
12
// store the current message for later use
13
currentMessage = fullscreenMessage?.parent
14
15
return false
16
}
17
18
return true
19
}
Copied!
If a custom FullscreenMessageDelegate 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
1
@Override
2
public boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {
3
if (someOtherWorkflowStatus == "inProgress") {
4
return false;
5
}
6
return true;
7
}
Copied!
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:
Java
1
Message currentMessage = null;
2
String anotherWorkflowStatus;
3
4
public void otherWorkflowFinished() {
5
anotherWorkflowStatus = "complete";
6
currentMessage.show();
7
}
8
9
@Override
10
public boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {
11
if (someOtherWorkflowStatus.equals("inProgress")) {
12
// store the current message for later use
13
currentMessage = (Message) fullscreenMessage.getParent();
14
return false;
15
}
16
17
return true;
18
}
Copied!

Integrating the message into an existing UI

iOS
Android
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.
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
1
var inAppMessageView: WKWebView?
2
3
func shouldShowMessage(message: Showable) -> Bool {
4
if shouldIntegrateMessageDirectly {
5
let fullscreenMessage = message as? FullscreenMessage
6
let message = fullscreenMessage?.parent
7
8
inAppMessageView = message?.view as? WKWebView
9
10
return false
11
}
12
13
return true
14
}
Copied!
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
1
private Message currentMessage = null;
2
private boolean shouldIntegrateMessageDirectly = true;
3
private MessageWebView inAppMessageView;
4
5
@Override
6
public boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {
7
if (shouldIntegrateMessageDirectly) {
8
this.currentMessage = (Message) fullscreenMessage.getParent();
9
10
// cast to MessageWebView to access the startInAppMessage function
11
inAppMessageView = (MessageWebView) currentMessage.getWebView();
12
13
return false;
14
}
15
16
return true;
17
}
Copied!

Further reading