Skip to content

Commit

Permalink
Refactor test push inapp handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nzagorchev committed Nov 26, 2024
1 parent b44db16 commit a9ad294
Showing 1 changed file with 43 additions and 35 deletions.
78 changes: 43 additions & 35 deletions CleverTapSDK/InApps/CTInAppDisplayManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -738,46 +738,29 @@ - (BOOL)didHandleInAppTestFromPushNotificaton:(NSDictionary * _Nullable)notifica
NSMutableDictionary *inapp = [[NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:nil] mutableCopy];
if (!inapp) {
CleverTapLogDebug(self.config.logLevel, @"%@: Failed to parse the inapp notification as JSON", self);
return YES;
}

// Handle Image Interstitial InApp Test
// Handle Image Interstitial and Advanced Builder InApp Test (Preview)
NSString *inAppPreviewType = notification[CLTAP_INAPP_PREVIEW_TYPE];
if (inapp && ([inAppPreviewType isEqualToString:CLTAP_INAPP_IMAGE_INTERSTITIAL_TYPE] || [inAppPreviewType isEqualToString:CLTAP_INAPP_ADVANCED_BUILDER_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 ([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;
}

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 {
CleverTapLogDebug(self.config.logLevel, @"%@: Failed to parse the inapp notification as JSON", self);
return YES;
}

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 Down Expand Up @@ -807,5 +790,30 @@ - (NSString *)wrapImageInterstitialContent:(NSString *)content {
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

0 comments on commit a9ad294

Please sign in to comment.