Skip to content

Commit

Permalink
Bug(MC-1719): App function not working with OTB templates
Browse files Browse the repository at this point in the history
- sets correct type to copied inapp notification
- reformatting
  • Loading branch information
CTLalit committed Jun 5, 2024
1 parent f3f192c commit f0a89ff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,13 @@ CTInAppNotification initWithJSON(JSONObject jsonObject, boolean videoSupported)

}

CTInAppNotification copy() {
CTInAppNotification copyAsCustomCode() {
//TODO implement copying of all fields instead of parsing the json
return new CTInAppNotification().initWithJSON(jsonDescription, videoSupported);
CTInAppNotification notification = new CTInAppNotification()
.initWithJSON(jsonDescription, videoSupported);
notification.type = InAppActionType.CUSTOM_CODE.name();
notification.excludeFromCaps = true;
return notification;
}

void setCustomTemplateData(CustomTemplateInAppData inAppData) {
Expand Down Expand Up @@ -479,49 +483,33 @@ void prepareForDisplay(InAppResourceProvider inAppResourceProvider) {

private void configureWithJson(JSONObject jsonObject) {
try {
this.id = jsonObject.has(Constants.INAPP_ID_IN_PAYLOAD) ? jsonObject
.getString(Constants.INAPP_ID_IN_PAYLOAD) : "";
this.campaignId = jsonObject.has(Constants.NOTIFICATION_ID_TAG) ? jsonObject
.getString(Constants.NOTIFICATION_ID_TAG) : "";
this.id = jsonObject.has(Constants.INAPP_ID_IN_PAYLOAD) ? jsonObject.getString(Constants.INAPP_ID_IN_PAYLOAD) : "";
this.campaignId = jsonObject.has(Constants.NOTIFICATION_ID_TAG) ? jsonObject.getString(Constants.NOTIFICATION_ID_TAG) : "";
this.type = jsonObject.getString(Constants.KEY_TYPE);
this.isLocalInApp = jsonObject.has(IS_LOCAL_INAPP) && jsonObject.getBoolean(IS_LOCAL_INAPP);
this.fallBackToNotificationSettings = jsonObject.has(FALLBACK_TO_NOTIFICATION_SETTINGS) &&
jsonObject.getBoolean(FALLBACK_TO_NOTIFICATION_SETTINGS);
this.excludeFromCaps = jsonObject.optInt(Constants.KEY_EFC, -1) == 1 ||
jsonObject.optInt(Constants.KEY_EXCLUDE_GLOBAL_CAPS, -1) == 1;
this.fallBackToNotificationSettings = jsonObject.has(FALLBACK_TO_NOTIFICATION_SETTINGS) && jsonObject.getBoolean(FALLBACK_TO_NOTIFICATION_SETTINGS);
this.excludeFromCaps = jsonObject.optInt(Constants.KEY_EFC, -1) == 1 || jsonObject.optInt(Constants.KEY_EXCLUDE_GLOBAL_CAPS, -1) == 1;
this.totalLifetimeCount = jsonObject.has(Constants.KEY_TLC) ? jsonObject.getInt(Constants.KEY_TLC) : -1;
this.totalDailyCount = jsonObject.has(Constants.KEY_TDC) ? jsonObject.getInt(Constants.KEY_TDC) : -1;
this.maxPerSession = jsonObject.has(Constants.INAPP_MAX_DISPLAY_COUNT) ? jsonObject
.getInt(Constants.INAPP_MAX_DISPLAY_COUNT) : -1;
this.maxPerSession = jsonObject.has(Constants.INAPP_MAX_DISPLAY_COUNT) ? jsonObject.getInt(Constants.INAPP_MAX_DISPLAY_COUNT) : -1;
this.inAppType = CTInAppType.fromString(this.type);
this.isTablet = jsonObject.has(Constants.KEY_IS_TABLET) && jsonObject.getBoolean(Constants.KEY_IS_TABLET);
this.backgroundColor = jsonObject.has(Constants.KEY_BG) ? jsonObject.getString(Constants.KEY_BG)
: Constants.WHITE;
this.isPortrait = !jsonObject.has(Constants.KEY_PORTRAIT) || jsonObject
.getBoolean(Constants.KEY_PORTRAIT);
this.isLandscape = jsonObject.has(Constants.KEY_LANDSCAPE) && jsonObject
.getBoolean(Constants.KEY_LANDSCAPE);
this.timeToLive = jsonObject.has(Constants.WZRK_TIME_TO_LIVE) ? jsonObject
.getLong(Constants.WZRK_TIME_TO_LIVE)
: System.currentTimeMillis() + 2 * Constants.ONE_DAY_IN_MILLIS;
JSONObject titleObject = jsonObject.has(Constants.KEY_TITLE) ? jsonObject
.getJSONObject(Constants.KEY_TITLE) : null;
this.backgroundColor = jsonObject.has(Constants.KEY_BG) ? jsonObject.getString(Constants.KEY_BG) : Constants.WHITE;
this.isPortrait = !jsonObject.has(Constants.KEY_PORTRAIT) || jsonObject.getBoolean(Constants.KEY_PORTRAIT);
this.isLandscape = jsonObject.has(Constants.KEY_LANDSCAPE) && jsonObject.getBoolean(Constants.KEY_LANDSCAPE);
this.timeToLive = jsonObject.has(Constants.WZRK_TIME_TO_LIVE) ? jsonObject.getLong(Constants.WZRK_TIME_TO_LIVE) : System.currentTimeMillis() + 2 * Constants.ONE_DAY_IN_MILLIS;
JSONObject titleObject = jsonObject.has(Constants.KEY_TITLE) ? jsonObject.getJSONObject(Constants.KEY_TITLE) : null;
if (titleObject != null) {
this.title = titleObject.has(Constants.KEY_TEXT) ? titleObject.getString(Constants.KEY_TEXT) : "";
this.titleColor = titleObject.has(Constants.KEY_COLOR) ? titleObject.getString(Constants.KEY_COLOR)
: Constants.BLACK;
this.titleColor = titleObject.has(Constants.KEY_COLOR) ? titleObject.getString(Constants.KEY_COLOR) : Constants.BLACK;
}
JSONObject msgObject = jsonObject.has(Constants.KEY_MESSAGE) ? jsonObject
.getJSONObject(Constants.KEY_MESSAGE) : null;
JSONObject msgObject = jsonObject.has(Constants.KEY_MESSAGE) ? jsonObject.getJSONObject(Constants.KEY_MESSAGE) : null;
if (msgObject != null) {
this.message = msgObject.has(Constants.KEY_TEXT) ? msgObject.getString(Constants.KEY_TEXT) : "";
this.messageColor = msgObject.has(Constants.KEY_COLOR) ? msgObject.getString(Constants.KEY_COLOR)
: Constants.BLACK;
this.messageColor = msgObject.has(Constants.KEY_COLOR) ? msgObject.getString(Constants.KEY_COLOR) : Constants.BLACK;
}
this.hideCloseButton = jsonObject.has(Constants.KEY_HIDE_CLOSE) && jsonObject
.getBoolean(Constants.KEY_HIDE_CLOSE);
JSONObject media = jsonObject.has(Constants.KEY_MEDIA) ? jsonObject.getJSONObject(Constants.KEY_MEDIA)
: null;
this.hideCloseButton = jsonObject.has(Constants.KEY_HIDE_CLOSE) && jsonObject.getBoolean(Constants.KEY_HIDE_CLOSE);
JSONObject media = jsonObject.has(Constants.KEY_MEDIA) ? jsonObject.getJSONObject(Constants.KEY_MEDIA) : null;
if (media != null) {
CTInAppNotificationMedia portraitMedia = new CTInAppNotificationMedia()
.initWithJSON(media, Configuration.ORIENTATION_PORTRAIT);
Expand All @@ -530,21 +518,17 @@ private void configureWithJson(JSONObject jsonObject) {
}
}

JSONObject media_landscape = jsonObject.has(Constants.KEY_MEDIA_LANDSCAPE) ? jsonObject
.getJSONObject(Constants.KEY_MEDIA_LANDSCAPE) : null;
JSONObject media_landscape = jsonObject.has(Constants.KEY_MEDIA_LANDSCAPE) ? jsonObject.getJSONObject(Constants.KEY_MEDIA_LANDSCAPE) : null;
if (media_landscape != null) {
CTInAppNotificationMedia landscapeMedia = new CTInAppNotificationMedia()
.initWithJSON(media_landscape, Configuration.ORIENTATION_LANDSCAPE);
CTInAppNotificationMedia landscapeMedia = new CTInAppNotificationMedia().initWithJSON(media_landscape, Configuration.ORIENTATION_LANDSCAPE);
if (landscapeMedia != null) {
mediaList.add(landscapeMedia);
}
}
JSONArray buttonArray = jsonObject.has(Constants.KEY_BUTTONS) ? jsonObject
.getJSONArray(Constants.KEY_BUTTONS) : null;
JSONArray buttonArray = jsonObject.has(Constants.KEY_BUTTONS) ? jsonObject.getJSONArray(Constants.KEY_BUTTONS) : null;
if (buttonArray != null) {
for (int i = 0; i < buttonArray.length(); i++) {
CTInAppNotificationButton inAppNotificationButton = new CTInAppNotificationButton()
.initWithJSON(buttonArray.getJSONObject(i));
CTInAppNotificationButton inAppNotificationButton = new CTInAppNotificationButton().initWithJSON(buttonArray.getJSONObject(i));
if (inAppNotificationButton != null && inAppNotificationButton.getError() == null) {
this.buttons.add(inAppNotificationButton);
this.buttonCount++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -949,16 +949,18 @@ private boolean isNonRegisteredCustomTemplate(JSONObject inApp) {
return isNonRegistered;
}

private void triggerCustomTemplateAction(CTInAppNotification notification,
CustomTemplateInAppData templateInAppData) {
private void triggerCustomTemplateAction(
CTInAppNotification notification,
CustomTemplateInAppData templateInAppData
) {
if (templateInAppData != null && templateInAppData.getTemplateName() != null) {
CustomTemplate template = templatesManager.getTemplate(templateInAppData.getTemplateName());
if (template != null) {
// When a custom in-app template is triggered as an action we need to present it.
// Since all related methods operate with either CTInAppNotification or its json representation, here
// we create a copy of the notification that initiated the triggering and add the action as its
// template data.
CTInAppNotification notificationFromAction = notification.copy();
CTInAppNotification notificationFromAction = notification.copyAsCustomCode();
CustomTemplateInAppData actionTemplateData = templateInAppData.copy();
actionTemplateData.setAction(true);
notificationFromAction.setCustomTemplateData(actionTemplateData);
Expand Down

0 comments on commit f0a89ff

Please sign in to comment.