Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MC-2302] Update HTML template for inapp preview. #383

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CleverTapSDK/CTConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ extern NSString *CLTAP_PROFILE_IDENTITY_KEY;

#define CLTAP_INAPP_PREVIEW_TYPE @"wzrk_inapp_type"
#define CLTAP_INAPP_IMAGE_INTERSTITIAL_TYPE @"image-interstitial"
#define CLTAP_INAPP_ADVANCED_BUILDER_TYPE @"advanced-builder"
#define CLTAP_INAPP_IMAGE_INTERSTITIAL_CONFIG @"imageInterstitialConfig"
#define CLTAP_INAPP_HTML_SPLIT @"\"##Vars##\""
#define CLTAP_INAPP_IMAGE_INTERSTITIAL_HTML_NAME @"image_interstitial"
Expand Down
81 changes: 45 additions & 36 deletions CleverTapSDK/InApps/CTInAppDisplayManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -738,45 +738,29 @@ - (BOOL)didHandleInAppTestFromPushNotificaton:(NSDictionary * _Nullable)notifica
NSMutableDictionary *inapp = [[NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:nil] mutableCopy];

// Handle Image Interstitial InApp Test
if (inapp && [notification[CLTAP_INAPP_PREVIEW_TYPE] isEqualToString:CLTAP_INAPP_IMAGE_INTERSTITIAL_TYPE]) {
NSString *config = [inapp valueForKeyPath:CLTAP_INAPP_IMAGE_INTERSTITIAL_CONFIG];
NSString *htmlContent = [self wrapImageInterstitialContent:[CTUtils jsonObjectToString:config]];
if (config && htmlContent) {
inapp[@"type"] = CLTAP_INAPP_HTML_TYPE;
id data = inapp[CLTAP_INAPP_DATA_TAG];
if (data && [data isKindOfClass:[NSDictionary class]]) {
data = [data mutableCopy];
// Update the html
data[CLTAP_INAPP_HTML] = htmlContent;
} else {
// If data key is not present or it is not a dictionary,
// set it and overwrite it
inapp[CLTAP_INAPP_DATA_TAG] = @{
CLTAP_INAPP_HTML: htmlContent
};
}
} else {
CleverTapLogDebug(self.config.logLevel, @"%@: Failed to parse the image-interstitial notification", self);
return YES;
}
}

if (inapp) {
float delay = self.isAppActiveForeground ? 0.5 : 2.0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
@try {
[self prepareNotificationForDisplay:inapp];
} @catch (NSException *e) {
CleverTapLogDebug(self.config.logLevel, @"%@: Failed to display the inapp notifcation from payload: %@", self, e.debugDescription);
}
});
} else {
if (!inapp) {
CleverTapLogDebug(self.config.logLevel, @"%@: Failed to parse the inapp notification as JSON", self);
return YES;
}

// Handle Image Interstitial and Advanced Builder InApp Test (Preview)
NSString *inAppPreviewType = notification[CLTAP_INAPP_PREVIEW_TYPE];
if ([inAppPreviewType isEqualToString:CLTAP_INAPP_IMAGE_INTERSTITIAL_TYPE] || [inAppPreviewType isEqualToString:CLTAP_INAPP_ADVANCED_BUILDER_TYPE]) {
NSMutableDictionary *htmlInapp = [self handleHTMLInAppPreview:inapp];
if (!htmlInapp) {
return YES; // Failed to handle HTML inapp
}
inapp = htmlInapp;
}

float delay = self.isAppActiveForeground ? 0.5 : 2.0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
@try {
[self prepareNotificationForDisplay:inapp];
} @catch (NSException *e) {
CleverTapLogDebug(self.config.logLevel, @"%@: Failed to display the inapp notifcation from payload: %@", self, e.debugDescription);
}
});
} @catch (NSException *e) {
CleverTapLogDebug(self.config.logLevel, @"%@: Failed to display the inapp notifcation from payload: %@", self, e.debugDescription);
return YES;
Expand All @@ -800,11 +784,36 @@ - (NSString *)wrapImageInterstitialContent:(NSString *)content {
if (html && content) {
NSArray *parts = [html componentsSeparatedByString:CLTAP_INAPP_HTML_SPLIT];
if ([parts count] == 2) {
return [NSString stringWithFormat:@"%@'%@'%@", parts[0], content, parts[1]];
return [NSString stringWithFormat:@"%@%@%@", parts[0], content, parts[1]];
}
}
return nil;
}

- (NSMutableDictionary *)handleHTMLInAppPreview:(NSMutableDictionary *)inapp {
NSMutableDictionary *htmlInapp = [inapp mutableCopy];
NSString *config = [htmlInapp valueForKeyPath:CLTAP_INAPP_IMAGE_INTERSTITIAL_CONFIG];
NSString *htmlContent = [self wrapImageInterstitialContent:[CTUtils jsonObjectToString:config]];
if (config && htmlContent) {
htmlInapp[@"type"] = CLTAP_INAPP_HTML_TYPE;
id data = htmlInapp[CLTAP_INAPP_DATA_TAG];
if (data && [data isKindOfClass:[NSDictionary class]]) {
data = [data mutableCopy];
// Update the html
data[CLTAP_INAPP_HTML] = htmlContent;
} else {
// If data key is not present or it is not a dictionary,
// set it and overwrite it
htmlInapp[CLTAP_INAPP_DATA_TAG] = @{
CLTAP_INAPP_HTML: htmlContent
};
}
return htmlInapp;
} else {
CleverTapLogDebug(self.config.logLevel, @"%@: Failed to parse the image-interstitial notification", self);
return nil;
}
}

@end

29 changes: 28 additions & 1 deletion CleverTapSDK/InApps/resources/image_interstitial.html
nzagorchev marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

Loading