Skip to content

Firebase In-app messaging callbacks #2354

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

Merged
merged 13 commits into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from 7 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
99 changes: 78 additions & 21 deletions Firebase/InAppMessaging/Flows/FIRIAMDisplayExecutor.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import "FIRIAMMessageContentData.h"
#import "FIRIAMMessageDefinition.h"
#import "FIRIAMSDKRuntimeErrorCodes.h"
#import "FIRInAppMessaging.h"

@implementation FIRIAMDisplaySetting
@end
Expand All @@ -45,7 +46,14 @@ @implementation FIRIAMDisplayExecutor {
}

#pragma mark - FIRInAppMessagingDisplayDelegate methods
- (void)messageClicked {
- (void)messageClicked:(FIRInAppMessagingDisplayMessage *)inAppMessage {
// Call through to app-side delegate.
__weak id<FIRInAppMessagingDisplayDelegate> appSideDelegate =
[FIRInAppMessaging inAppMessaging].delegate;
if ([appSideDelegate respondsToSelector:@selector(messageClicked:)]) {
[appSideDelegate messageClicked:inAppMessage];
}

self.isMsgBeingDisplayed = NO;
if (!_currentMsgBeingDisplayed.renderData.messageID) {
FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM400030",
Expand Down Expand Up @@ -124,7 +132,15 @@ - (void)messageClicked {
}
}

- (void)messageDismissedWithType:(FIRInAppMessagingDismissType)dismissType {
- (void)messageDismissed:(FIRInAppMessagingDisplayMessage *)inAppMessage
dismissType:(FIRInAppMessagingDismissType)dismissType {
// Call through to app-side delegate.
__weak id<FIRInAppMessagingDisplayDelegate> appSideDelegate =
[FIRInAppMessaging inAppMessaging].delegate;
if ([appSideDelegate respondsToSelector:@selector(messageDismissed:dismissType:)]) {
[appSideDelegate messageDismissed:inAppMessage dismissType:dismissType];
}

self.isMsgBeingDisplayed = NO;
if (!_currentMsgBeingDisplayed.renderData.messageID) {
FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM400014",
Expand Down Expand Up @@ -170,7 +186,13 @@ - (void)messageDismissedWithType:(FIRInAppMessagingDismissType)dismissType {
}];
}

- (void)impressionDetected {
- (void)impressionDetectedForMessage:(FIRInAppMessagingDisplayMessage *)inAppMessage {
__weak id<FIRInAppMessagingDisplayDelegate> appSideDelegate =
[FIRInAppMessaging inAppMessaging].delegate;
if ([appSideDelegate respondsToSelector:@selector(impressionDetectedForMessage:)]) {
[appSideDelegate impressionDetectedForMessage:inAppMessage];
}

if (!_currentMsgBeingDisplayed.renderData.messageID) {
FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM400022",
@"impressionDetected called but "
Expand All @@ -189,7 +211,14 @@ - (void)impressionDetected {
}
}

- (void)displayErrorEncountered:(NSError *)error {
- (void)displayErrorForMessage:(FIRInAppMessagingDisplayMessage *)inAppMessage
error:(NSError *)error {
__weak id<FIRInAppMessagingDisplayDelegate> appSideDelegate =
[FIRInAppMessaging inAppMessaging].delegate;
if ([appSideDelegate respondsToSelector:@selector(displayErrorForMessage:error:)]) {
[appSideDelegate displayErrorForMessage:inAppMessage error:error];
}

self.isMsgBeingDisplayed = NO;

if (!_currentMsgBeingDisplayed.renderData.messageID) {
Expand Down Expand Up @@ -334,43 +363,53 @@ - (void)checkAndDisplayNextContextualMessageForAnalyticsEvent:(NSString *)eventN
[self.messageCache nextOnFirebaseAnalyticEventDisplayMsg:eventName];

if (nextAnalyticsBasedMessage) {
[self displayForMessage:nextAnalyticsBasedMessage];
[self displayForMessage:nextAnalyticsBasedMessage
triggerType:FIRInAppMessagingDisplayTriggerTypeOnAnalyticsEvent];
}
}
}

- (FIRInAppMessagingBannerDisplay *)
bannerMessageWithMessageDefinition:(FIRIAMMessageDefinition *)definition
imageData:(FIRInAppMessagingImageData *)imageData {
imageData:(FIRInAppMessagingImageData *)imageData
triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
NSString *title = definition.renderData.contentData.titleText;
NSString *body = definition.renderData.contentData.bodyText;

FIRInAppMessagingBannerDisplay *bannerMessage = [[FIRInAppMessagingBannerDisplay alloc]
initWithMessageID:definition.renderData.messageID
campaignName:definition.renderData.name
renderAsTestMessage:definition.isTestMessage
triggerType:triggerType
titleText:title
bodyText:body
textColor:definition.renderData.renderingEffectSettings.textColor
backgroundColor:definition.renderData.renderingEffectSettings.displayBGColor
imageData:imageData];
imageData:imageData
actionURL:definition.renderData.contentData.actionURL];

return bannerMessage;
}

- (FIRInAppMessagingImageOnlyDisplay *)
imageOnlyMessageWithMessageDefinition:(FIRIAMMessageDefinition *)definition
imageData:(FIRInAppMessagingImageData *)imageData {
imageData:(FIRInAppMessagingImageData *)imageData
triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
FIRInAppMessagingImageOnlyDisplay *imageOnlyMessage =
[[FIRInAppMessagingImageOnlyDisplay alloc] initWithMessageID:definition.renderData.messageID
campaignName:definition.renderData.name
renderAsTestMessage:definition.isTestMessage
imageData:imageData];
triggerType:triggerType
imageData:imageData
actionURL:definition.renderData.contentData.actionURL];

return imageOnlyMessage;
}

- (FIRInAppMessagingModalDisplay *)
modalViewMessageWithMessageDefinition:(FIRIAMMessageDefinition *)definition
imageData:(FIRInAppMessagingImageData *)imageData {
imageData:(FIRInAppMessagingImageData *)imageData
triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
// For easier reference in this method.
FIRIAMMessageRenderData *renderData = definition.renderData;

Expand All @@ -388,33 +427,44 @@ - (void)checkAndDisplayNextContextualMessageForAnalyticsEvent:(NSString *)eventN

FIRInAppMessagingModalDisplay *modalViewMessage = [[FIRInAppMessagingModalDisplay alloc]
initWithMessageID:definition.renderData.messageID
campaignName:definition.renderData.name
renderAsTestMessage:definition.isTestMessage
triggerType:triggerType
titleText:title
bodyText:body
textColor:renderData.renderingEffectSettings.textColor
backgroundColor:renderData.renderingEffectSettings.displayBGColor
imageData:imageData
actionButton:actionButton];
actionButton:actionButton
actionURL:definition.renderData.contentData.actionURL];

return modalViewMessage;
}

- (FIRInAppMessagingDisplayMessageBase *)
- (FIRInAppMessagingDisplayMessage *)
displayMessageWithMessageDefinition:(FIRIAMMessageDefinition *)definition
imageData:(FIRInAppMessagingImageData *)imageData {
imageData:(FIRInAppMessagingImageData *)imageData
triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
switch (definition.renderData.renderingEffectSettings.viewMode) {
case FIRIAMRenderAsBannerView:
return [self bannerMessageWithMessageDefinition:definition imageData:imageData];
return [self bannerMessageWithMessageDefinition:definition
imageData:imageData
triggerType:triggerType];
case FIRIAMRenderAsModalView:
return [self modalViewMessageWithMessageDefinition:definition imageData:imageData];
return [self modalViewMessageWithMessageDefinition:definition
imageData:imageData
triggerType:triggerType];
case FIRIAMRenderAsImageOnlyView:
return [self imageOnlyMessageWithMessageDefinition:definition imageData:imageData];
return [self imageOnlyMessageWithMessageDefinition:definition
imageData:imageData
triggerType:triggerType];
default:
return nil;
}
}

- (void)displayForMessage:(FIRIAMMessageDefinition *)message {
- (void)displayForMessage:(FIRIAMMessageDefinition *)message
triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
_currentMsgBeingDisplayed = message;
[message.renderData.contentData
loadImageDataWithBlock:^(NSData *_Nullable imageNSData, NSError *error) {
Expand All @@ -424,8 +474,12 @@ - (void)displayForMessage:(FIRIAMMessageDefinition *)message {
FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400019",
@"Error in loading image data for the message.");

FIRInAppMessagingDisplayMessage *erroredMessage =
[self displayMessageWithMessageDefinition:message
imageData:imageData
triggerType:triggerType];
// short-circuit to display error handling
[self displayErrorEncountered:error];
[self displayErrorForMessage:erroredMessage error:error];
return;
} else if (imageNSData != nil) {
imageData = [[FIRInAppMessagingImageData alloc]
Expand All @@ -436,8 +490,10 @@ - (void)displayForMessage:(FIRIAMMessageDefinition *)message {
self.impressionRecorded = NO;
self.isMsgBeingDisplayed = YES;

FIRInAppMessagingDisplayMessageBase *displayMessage =
[self displayMessageWithMessageDefinition:message imageData:imageData];
FIRInAppMessagingDisplayMessage *displayMessage =
[self displayMessageWithMessageDefinition:message
imageData:imageData
triggerType:triggerType];
[self.messageDisplayComponent displayMessage:displayMessage displayDelegate:self];
}];
}
Expand Down Expand Up @@ -481,7 +537,8 @@ - (void)checkAndDisplayNextAppForegroundMessage {
FIRIAMMessageDefinition *nextForegroundMessage = [self.messageCache nextOnAppOpenDisplayMsg];

if (nextForegroundMessage) {
[self displayForMessage:nextForegroundMessage];
[self displayForMessage:nextForegroundMessage
triggerType:FIRInAppMessagingDisplayTriggerTypeOnAppForeground];
self.lastDisplayTime = [self.timeFetcher currentTimestampInSeconds];
} else {
FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400001",
Expand Down
6 changes: 6 additions & 0 deletions Firebase/InAppMessaging/Public/FIRInAppMessaging.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,11 @@ NS_SWIFT_NAME(InAppMessaging)
* property so that it can be used for rendering fiam message UIs.
*/
@property(nonatomic) id<FIRInAppMessagingDisplay> messageDisplayComponent;

/**
* This delegate should be set on the app side to receive message lifecycle events in app runtime.
*/
@property(nonatomic, weak) id<FIRInAppMessagingDisplayDelegate> delegate;

@end
NS_ASSUME_NONNULL_END
Loading