FIRIAMDisplayExecutor.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <FirebaseCore/FIRLogger.h>
  17. #import <UIKit/UIKit.h>
  18. #import "FIRCore+InAppMessaging.h"
  19. #import "FIRIAMActivityLogger.h"
  20. #import "FIRIAMDisplayExecutor.h"
  21. #import "FIRIAMMessageContentData.h"
  22. #import "FIRIAMMessageDefinition.h"
  23. #import "FIRIAMSDKRuntimeErrorCodes.h"
  24. #import "FIRInAppMessaging.h"
  25. #import "FIRInAppMessagingRenderingPrivate.h"
  26. #import <FirebaseABTesting/FIRExperimentController.h>
  27. @implementation FIRIAMDisplaySetting
  28. @end
  29. @interface FIRIAMDisplayExecutor () <FIRInAppMessagingDisplayDelegate>
  30. @property(nonatomic) id<FIRIAMTimeFetcher> timeFetcher;
  31. // YES if a message is being rendered at this time
  32. @property(nonatomic) BOOL isMsgBeingDisplayed;
  33. @property(nonatomic) NSTimeInterval lastDisplayTime;
  34. @property(nonatomic, nonnull, readonly) FIRInAppMessaging *inAppMessaging;
  35. @property(nonatomic, nonnull, readonly) FIRIAMDisplaySetting *setting;
  36. @property(nonatomic, nonnull, readonly) FIRIAMMessageClientCache *messageCache;
  37. @property(nonatomic, nonnull, readonly) id<FIRIAMBookKeeper> displayBookKeeper;
  38. @property(nonatomic) BOOL impressionRecorded;
  39. @property(nonatomic, nonnull, readonly) id<FIRIAMAnalyticsEventLogger> analyticsEventLogger;
  40. @property(nonatomic, nonnull, readonly) FIRIAMActionURLFollower *actionURLFollower;
  41. // Used for displaying the test on device message error alert.
  42. @property(nonatomic, strong) UIWindow *alertWindow;
  43. @end
  44. @implementation FIRIAMDisplayExecutor {
  45. FIRIAMMessageDefinition *_currentMsgBeingDisplayed;
  46. }
  47. #pragma mark - FIRInAppMessagingDisplayDelegate methods
  48. - (void)messageClicked:(FIRInAppMessagingDisplayMessage *)inAppMessage
  49. withAction:(FIRInAppMessagingAction *)action {
  50. // Call through to app-side delegate.
  51. __weak id<FIRInAppMessagingDisplayDelegate> appSideDelegate = self.inAppMessaging.delegate;
  52. if ([appSideDelegate respondsToSelector:@selector(messageClicked:withAction:)]) {
  53. [appSideDelegate messageClicked:inAppMessage withAction:action];
  54. } else if ([appSideDelegate respondsToSelector:@selector(messageClicked:)]) {
  55. // Deprecated method is called only as a fall-back.
  56. #pragma clang diagnostic push
  57. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  58. [appSideDelegate messageClicked:inAppMessage];
  59. #pragma clang diagnostic pop
  60. }
  61. self.isMsgBeingDisplayed = NO;
  62. if (!_currentMsgBeingDisplayed.renderData.messageID) {
  63. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM400030",
  64. @"messageClicked called but "
  65. "there is no current message ID.");
  66. return;
  67. }
  68. if (_currentMsgBeingDisplayed.isTestMessage) {
  69. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400031",
  70. @"A test message clicked. Do test event impression/click analytics logging");
  71. [self.analyticsEventLogger
  72. logAnalyticsEventForType:FIRIAMAnalyticsEventTestMessageImpression
  73. forCampaignID:_currentMsgBeingDisplayed.renderData.messageID
  74. withCampaignName:_currentMsgBeingDisplayed.renderData.name
  75. eventTimeInMs:nil
  76. completion:^(BOOL success) {
  77. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400036",
  78. @"Logging analytics event for url following %@",
  79. success ? @"succeeded" : @"failed");
  80. }];
  81. [self.analyticsEventLogger
  82. logAnalyticsEventForType:FIRIAMAnalyticsEventTestMessageClick
  83. forCampaignID:_currentMsgBeingDisplayed.renderData.messageID
  84. withCampaignName:_currentMsgBeingDisplayed.renderData.name
  85. eventTimeInMs:nil
  86. completion:^(BOOL success) {
  87. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400039",
  88. @"Logging analytics event for url following %@",
  89. success ? @"succeeded" : @"failed");
  90. }];
  91. } else {
  92. // Logging the impression
  93. [self recordValidImpression:_currentMsgBeingDisplayed.renderData.messageID
  94. withMessageName:_currentMsgBeingDisplayed.renderData.name];
  95. [self.analyticsEventLogger
  96. logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
  97. forCampaignID:_currentMsgBeingDisplayed.renderData.messageID
  98. withCampaignName:_currentMsgBeingDisplayed.renderData.name
  99. eventTimeInMs:nil
  100. completion:^(BOOL success) {
  101. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400032",
  102. @"Logging analytics event for url following %@",
  103. success ? @"succeeded" : @"failed");
  104. }];
  105. }
  106. NSURL *actionURL = action.actionURL;
  107. if (!actionURL) {
  108. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400033",
  109. @"messageClicked called but "
  110. "there is no action url specified in the message data.");
  111. // it's equivalent to closing the message with no further action
  112. return;
  113. } else {
  114. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400037", @"Following action url %@",
  115. actionURL.absoluteString);
  116. @try {
  117. [self.actionURLFollower
  118. followActionURL:actionURL
  119. withCompletionBlock:^(BOOL success) {
  120. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400034",
  121. @"Seeing %@ from following action URL", success ? @"success" : @"error");
  122. }];
  123. } @catch (NSException *e) {
  124. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM400035",
  125. @"Exception encountered in following "
  126. "action url (%@): %@ ",
  127. actionURL, e.description);
  128. @throw;
  129. }
  130. }
  131. }
  132. - (void)messageDismissed:(FIRInAppMessagingDisplayMessage *)inAppMessage
  133. dismissType:(FIRInAppMessagingDismissType)dismissType {
  134. // Call through to app-side delegate.
  135. __weak id<FIRInAppMessagingDisplayDelegate> appSideDelegate = self.inAppMessaging.delegate;
  136. if ([appSideDelegate respondsToSelector:@selector(messageDismissed:dismissType:)]) {
  137. [appSideDelegate messageDismissed:inAppMessage dismissType:dismissType];
  138. }
  139. self.isMsgBeingDisplayed = NO;
  140. if (!_currentMsgBeingDisplayed.renderData.messageID) {
  141. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM400014",
  142. @"messageDismissedWithType called but "
  143. "there is no current message ID.");
  144. return;
  145. }
  146. if (_currentMsgBeingDisplayed.isTestMessage) {
  147. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400020",
  148. @"A test message dismissed. Record the impression event.");
  149. [self.analyticsEventLogger
  150. logAnalyticsEventForType:FIRIAMAnalyticsEventTestMessageImpression
  151. forCampaignID:_currentMsgBeingDisplayed.renderData.messageID
  152. withCampaignName:_currentMsgBeingDisplayed.renderData.name
  153. eventTimeInMs:nil
  154. completion:^(BOOL success) {
  155. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400038",
  156. @"Logging analytics event for url following %@",
  157. success ? @"succeeded" : @"failed");
  158. }];
  159. return;
  160. }
  161. // Logging the impression
  162. [self recordValidImpression:_currentMsgBeingDisplayed.renderData.messageID
  163. withMessageName:_currentMsgBeingDisplayed.renderData.name];
  164. FIRIAMAnalyticsLogEventType logEventType = dismissType == FIRInAppMessagingDismissTypeAuto
  165. ? FIRIAMAnalyticsEventMessageDismissAuto
  166. : FIRIAMAnalyticsEventMessageDismissClick;
  167. [self.analyticsEventLogger
  168. logAnalyticsEventForType:logEventType
  169. forCampaignID:_currentMsgBeingDisplayed.renderData.messageID
  170. withCampaignName:_currentMsgBeingDisplayed.renderData.name
  171. eventTimeInMs:nil
  172. completion:^(BOOL success) {
  173. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400004",
  174. @"Logging analytics event for message dismiss %@",
  175. success ? @"succeeded" : @"failed");
  176. }];
  177. }
  178. - (void)impressionDetectedForMessage:(FIRInAppMessagingDisplayMessage *)inAppMessage {
  179. __weak id<FIRInAppMessagingDisplayDelegate> appSideDelegate = self.inAppMessaging.delegate;
  180. if ([appSideDelegate respondsToSelector:@selector(impressionDetectedForMessage:)]) {
  181. [appSideDelegate impressionDetectedForMessage:inAppMessage];
  182. }
  183. if (!_currentMsgBeingDisplayed.renderData.messageID) {
  184. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM400022",
  185. @"impressionDetected called but "
  186. "there is no current message ID.");
  187. return;
  188. }
  189. // If this is an experimental FIAM, activate the experiment.
  190. if (inAppMessage.campaignInfo.experimentPayload) {
  191. [[FIRExperimentController sharedInstance]
  192. activateExperiment:inAppMessage.campaignInfo.experimentPayload
  193. forServiceOrigin:@"fiam"];
  194. }
  195. if (!_currentMsgBeingDisplayed.isTestMessage) {
  196. // Displayed long enough to be a valid impression.
  197. [self recordValidImpression:_currentMsgBeingDisplayed.renderData.messageID
  198. withMessageName:_currentMsgBeingDisplayed.renderData.name];
  199. } else {
  200. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400011",
  201. @"A test message. Record the test message impression event.");
  202. return;
  203. }
  204. }
  205. - (void)displayErrorForMessage:(FIRInAppMessagingDisplayMessage *)inAppMessage
  206. error:(NSError *)error {
  207. __weak id<FIRInAppMessagingDisplayDelegate> appSideDelegate = self.inAppMessaging.delegate;
  208. if ([appSideDelegate respondsToSelector:@selector(displayErrorForMessage:error:)]) {
  209. [appSideDelegate displayErrorForMessage:inAppMessage error:error];
  210. }
  211. self.isMsgBeingDisplayed = NO;
  212. if (!_currentMsgBeingDisplayed.renderData.messageID) {
  213. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM400017",
  214. @"displayErrorEncountered called but "
  215. "there is no current message ID.");
  216. return;
  217. }
  218. NSString *messageID = _currentMsgBeingDisplayed.renderData.messageID;
  219. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400009",
  220. @"Display ran into error for message %@: %@", messageID, error);
  221. if (_currentMsgBeingDisplayed.isTestMessage) {
  222. [self displayMessageLoadError:error];
  223. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400012",
  224. @"A test message. No analytics tracking "
  225. "from image data loading failure");
  226. return;
  227. }
  228. // we remove the message from the client side cache so that it won't be retried until next time
  229. // it's fetched again from server.
  230. [self.messageCache removeMessageWithId:messageID];
  231. NSString *messageName = _currentMsgBeingDisplayed.renderData.name;
  232. if ([error.domain isEqualToString:NSURLErrorDomain]) {
  233. [self.analyticsEventLogger
  234. logAnalyticsEventForType:FIRIAMAnalyticsEventImageFetchError
  235. forCampaignID:messageID
  236. withCampaignName:messageName
  237. eventTimeInMs:nil
  238. completion:^(BOOL success) {
  239. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400010",
  240. @"Logging analytics event for image fetch error %@",
  241. success ? @"succeeded" : @"failed");
  242. }];
  243. } else if (error.code == FIRIAMSDKRuntimeErrorNonImageMimetypeFromImageURL) {
  244. [self.analyticsEventLogger
  245. logAnalyticsEventForType:FIRIAMAnalyticsEventImageFormatUnsupported
  246. forCampaignID:messageID
  247. withCampaignName:messageName
  248. eventTimeInMs:nil
  249. completion:^(BOOL success) {
  250. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400013",
  251. @"Logging analytics event for image format error %@",
  252. success ? @"succeeded" : @"failed");
  253. }];
  254. }
  255. }
  256. - (void)recordValidImpression:(NSString *)messageID withMessageName:(NSString *)messageName {
  257. if (!self.impressionRecorded) {
  258. [self.displayBookKeeper recordNewImpressionForMessage:messageID
  259. withStartTimestampInSeconds:self.lastDisplayTime];
  260. self.impressionRecorded = YES;
  261. [self.messageCache removeMessageWithId:messageID];
  262. // Log an impression analytics event as well.
  263. [self.analyticsEventLogger
  264. logAnalyticsEventForType:FIRIAMAnalyticsEventMessageImpression
  265. forCampaignID:messageID
  266. withCampaignName:messageName
  267. eventTimeInMs:nil
  268. completion:^(BOOL success) {
  269. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400007",
  270. @"Logging analytics event for impression %@",
  271. success ? @"succeeded" : @"failed");
  272. }];
  273. }
  274. }
  275. - (void)displayMessageLoadError:(NSError *)error {
  276. NSString *errorMsg = error.userInfo[NSLocalizedDescriptionKey]
  277. ? error.userInfo[NSLocalizedDescriptionKey]
  278. : NSLocalizedString(@"Message loading failed", nil);
  279. UIAlertController *alert = [UIAlertController
  280. alertControllerWithTitle:@"Firebase InAppMessaging fail to load a test message"
  281. message:errorMsg
  282. preferredStyle:UIAlertControllerStyleAlert];
  283. UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
  284. style:UIAlertActionStyleDefault
  285. handler:^(UIAlertAction *action) {
  286. self.alertWindow.hidden = NO;
  287. self.alertWindow = nil;
  288. }];
  289. [alert addAction:defaultAction];
  290. dispatch_async(dispatch_get_main_queue(), ^{
  291. #if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  292. if (@available(iOS 13.0, *)) {
  293. UIWindowScene *foregroundedScene = nil;
  294. for (UIWindowScene *connectedScene in [UIApplication sharedApplication].connectedScenes) {
  295. if (connectedScene.activationState == UISceneActivationStateForegroundActive) {
  296. foregroundedScene = connectedScene;
  297. break;
  298. }
  299. }
  300. if (foregroundedScene == nil) {
  301. return;
  302. }
  303. self.alertWindow = [[UIWindow alloc] initWithWindowScene:foregroundedScene];
  304. }
  305. #else // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  306. self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  307. #endif
  308. UIViewController *alertViewController = [[UIViewController alloc] init];
  309. self.alertWindow.rootViewController = alertViewController;
  310. self.alertWindow.hidden = NO;
  311. [alertViewController presentViewController:alert animated:YES completion:nil];
  312. });
  313. }
  314. - (instancetype)initWithInAppMessaging:(FIRInAppMessaging *)inAppMessaging
  315. setting:(FIRIAMDisplaySetting *)setting
  316. messageCache:(FIRIAMMessageClientCache *)cache
  317. timeFetcher:(id<FIRIAMTimeFetcher>)timeFetcher
  318. bookKeeper:(id<FIRIAMBookKeeper>)displayBookKeeper
  319. actionURLFollower:(FIRIAMActionURLFollower *)actionURLFollower
  320. activityLogger:(FIRIAMActivityLogger *)activityLogger
  321. analyticsEventLogger:(id<FIRIAMAnalyticsEventLogger>)analyticsEventLogger {
  322. if (self = [super init]) {
  323. _inAppMessaging = inAppMessaging;
  324. _timeFetcher = timeFetcher;
  325. _lastDisplayTime = displayBookKeeper.lastDisplayTime;
  326. _setting = setting;
  327. _messageCache = cache;
  328. _displayBookKeeper = displayBookKeeper;
  329. _isMsgBeingDisplayed = NO;
  330. _analyticsEventLogger = analyticsEventLogger;
  331. _actionURLFollower = actionURLFollower;
  332. _suppressMessageDisplay = NO; // always allow message display on startup
  333. }
  334. return self;
  335. }
  336. - (void)checkAndDisplayNextContextualMessageForAnalyticsEvent:(NSString *)eventName {
  337. // synchronizing on self so that we won't potentially enter the render flow from two
  338. // threads: example like showing analytics triggered message and a regular app open
  339. // triggered message
  340. @synchronized(self) {
  341. if (self.suppressMessageDisplay) {
  342. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400015",
  343. @"Message display is being suppressed. No contextual message rendering.");
  344. return;
  345. }
  346. if (!self.messageDisplayComponent) {
  347. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400026",
  348. @"Message display component is not present yet. No display should happen.");
  349. return;
  350. }
  351. if (self.isMsgBeingDisplayed) {
  352. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400008",
  353. @"An in-app message display is in progress, do not check analytics event "
  354. "based message for now.");
  355. return;
  356. }
  357. // Pop up next analytics event based message to be displayed
  358. FIRIAMMessageDefinition *nextAnalyticsBasedMessage =
  359. [self.messageCache nextOnFirebaseAnalyticEventDisplayMsg:eventName];
  360. if (nextAnalyticsBasedMessage) {
  361. [self displayForMessage:nextAnalyticsBasedMessage
  362. triggerType:FIRInAppMessagingDisplayTriggerTypeOnAnalyticsEvent];
  363. }
  364. }
  365. }
  366. - (FIRInAppMessagingCardDisplay *)
  367. cardDisplayMessageWithMessageDefinition:(FIRIAMMessageDefinition *)definition
  368. portraitImageData:(nonnull FIRInAppMessagingImageData *)portraitImageData
  369. landscapeImageData:
  370. (nullable FIRInAppMessagingImageData *)landscapeImageData
  371. triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
  372. // For easier reference in this method.
  373. FIRIAMMessageRenderData *renderData = definition.renderData;
  374. NSString *title = renderData.contentData.titleText;
  375. NSString *body = renderData.contentData.bodyText;
  376. // Action button data is never nil for a card message.
  377. #pragma clang diagnostic push
  378. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  379. FIRInAppMessagingActionButton *primaryActionButton = [[FIRInAppMessagingActionButton alloc]
  380. initWithButtonText:renderData.contentData.actionButtonText
  381. buttonTextColor:renderData.renderingEffectSettings.btnTextColor
  382. backgroundColor:renderData.renderingEffectSettings.btnBGColor];
  383. #pragma clang diagnostic pop
  384. FIRInAppMessagingActionButton *secondaryActionButton = nil;
  385. if (definition.renderData.contentData.secondaryActionButtonText) {
  386. #pragma clang diagnostic push
  387. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  388. secondaryActionButton = [[FIRInAppMessagingActionButton alloc]
  389. initWithButtonText:renderData.contentData.secondaryActionButtonText
  390. buttonTextColor:renderData.renderingEffectSettings.secondaryActionBtnTextColor
  391. backgroundColor:renderData.renderingEffectSettings.btnBGColor];
  392. #pragma clang diagnostic pop
  393. }
  394. FIRInAppMessagingCardDisplay *cardMessage = [[FIRInAppMessagingCardDisplay alloc]
  395. initWithMessageID:renderData.messageID
  396. campaignName:renderData.name
  397. experimentPayload:definition.experimentPayload
  398. renderAsTestMessage:definition.isTestMessage
  399. triggerType:triggerType
  400. titleText:title
  401. textColor:renderData.renderingEffectSettings.textColor
  402. portraitImageData:portraitImageData
  403. backgroundColor:renderData.renderingEffectSettings.displayBGColor
  404. primaryActionButton:primaryActionButton
  405. primaryActionURL:definition.renderData.contentData.actionURL
  406. appData:definition.appData];
  407. cardMessage.body = body;
  408. cardMessage.landscapeImageData = landscapeImageData;
  409. cardMessage.secondaryActionButton = secondaryActionButton;
  410. cardMessage.secondaryActionURL = definition.renderData.contentData.secondaryActionURL;
  411. return cardMessage;
  412. }
  413. - (FIRInAppMessagingBannerDisplay *)
  414. bannerDisplayMessageWithMessageDefinition:(FIRIAMMessageDefinition *)definition
  415. imageData:(FIRInAppMessagingImageData *)imageData
  416. triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
  417. NSString *title = definition.renderData.contentData.titleText;
  418. NSString *body = definition.renderData.contentData.bodyText;
  419. #pragma clang diagnostic push
  420. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  421. FIRInAppMessagingBannerDisplay *bannerMessage = [[FIRInAppMessagingBannerDisplay alloc]
  422. initWithMessageID:definition.renderData.messageID
  423. campaignName:definition.renderData.name
  424. experimentPayload:definition.experimentPayload
  425. renderAsTestMessage:definition.isTestMessage
  426. triggerType:triggerType
  427. titleText:title
  428. bodyText:body
  429. textColor:definition.renderData.renderingEffectSettings.textColor
  430. backgroundColor:definition.renderData.renderingEffectSettings.displayBGColor
  431. imageData:imageData
  432. actionURL:definition.renderData.contentData.actionURL
  433. appData:definition.appData];
  434. #pragma clang diagnostic pop
  435. return bannerMessage;
  436. }
  437. - (FIRInAppMessagingImageOnlyDisplay *)
  438. imageOnlyDisplayMessageWithMessageDefinition:(FIRIAMMessageDefinition *)definition
  439. imageData:(FIRInAppMessagingImageData *)imageData
  440. triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
  441. #pragma clang diagnostic push
  442. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  443. FIRInAppMessagingImageOnlyDisplay *imageOnlyMessage = [[FIRInAppMessagingImageOnlyDisplay alloc]
  444. initWithMessageID:definition.renderData.messageID
  445. campaignName:definition.renderData.name
  446. experimentPayload:definition.experimentPayload
  447. renderAsTestMessage:definition.isTestMessage
  448. triggerType:triggerType
  449. imageData:imageData
  450. actionURL:definition.renderData.contentData.actionURL
  451. appData:definition.appData];
  452. #pragma clang diagnostic pop
  453. return imageOnlyMessage;
  454. }
  455. - (FIRInAppMessagingModalDisplay *)
  456. modalDisplayMessageWithMessageDefinition:(FIRIAMMessageDefinition *)definition
  457. imageData:(FIRInAppMessagingImageData *)imageData
  458. triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
  459. // For easier reference in this method.
  460. FIRIAMMessageRenderData *renderData = definition.renderData;
  461. NSString *title = renderData.contentData.titleText;
  462. NSString *body = renderData.contentData.bodyText;
  463. FIRInAppMessagingActionButton *actionButton = nil;
  464. if (definition.renderData.contentData.actionButtonText) {
  465. #pragma clang diagnostic push
  466. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  467. actionButton = [[FIRInAppMessagingActionButton alloc]
  468. initWithButtonText:renderData.contentData.actionButtonText
  469. buttonTextColor:renderData.renderingEffectSettings.btnTextColor
  470. backgroundColor:renderData.renderingEffectSettings.btnBGColor];
  471. #pragma clang diagnostic pop
  472. }
  473. #pragma clang diagnostic push
  474. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  475. FIRInAppMessagingModalDisplay *modalViewMessage = [[FIRInAppMessagingModalDisplay alloc]
  476. initWithMessageID:definition.renderData.messageID
  477. campaignName:definition.renderData.name
  478. experimentPayload:definition.experimentPayload
  479. renderAsTestMessage:definition.isTestMessage
  480. triggerType:triggerType
  481. titleText:title
  482. bodyText:body
  483. textColor:renderData.renderingEffectSettings.textColor
  484. backgroundColor:renderData.renderingEffectSettings.displayBGColor
  485. imageData:imageData
  486. actionButton:actionButton
  487. actionURL:definition.renderData.contentData.actionURL
  488. appData:definition.appData];
  489. #pragma clang diagnostic pop
  490. return modalViewMessage;
  491. }
  492. - (FIRInAppMessagingDisplayMessage *)
  493. displayMessageWithMessageDefinition:(FIRIAMMessageDefinition *)definition
  494. imageData:(FIRInAppMessagingImageData *)imageData
  495. landscapeImageData:(nullable FIRInAppMessagingImageData *)landscapeImageData
  496. triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
  497. switch (definition.renderData.renderingEffectSettings.viewMode) {
  498. case FIRIAMRenderAsCardView:
  499. // Image data should never nil for a valid card message.
  500. if (imageData == nil) {
  501. return nil;
  502. }
  503. return [self cardDisplayMessageWithMessageDefinition:definition
  504. portraitImageData:imageData
  505. landscapeImageData:landscapeImageData
  506. triggerType:triggerType];
  507. case FIRIAMRenderAsBannerView:
  508. return [self bannerDisplayMessageWithMessageDefinition:definition
  509. imageData:imageData
  510. triggerType:triggerType];
  511. case FIRIAMRenderAsModalView:
  512. return [self modalDisplayMessageWithMessageDefinition:definition
  513. imageData:imageData
  514. triggerType:triggerType];
  515. case FIRIAMRenderAsImageOnlyView:
  516. return [self imageOnlyDisplayMessageWithMessageDefinition:definition
  517. imageData:imageData
  518. triggerType:triggerType];
  519. default:
  520. return nil;
  521. }
  522. }
  523. - (void)displayForMessage:(FIRIAMMessageDefinition *)message
  524. triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
  525. _currentMsgBeingDisplayed = message;
  526. [message.renderData.contentData
  527. loadImageDataWithBlock:^(NSData *_Nullable standardImageRawData,
  528. NSData *_Nullable landscapeImageRawData, NSError *_Nullable error) {
  529. FIRInAppMessagingImageData *imageData = nil;
  530. FIRInAppMessagingImageData *landscapeImageData = nil;
  531. if (error) {
  532. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400019",
  533. @"Error in loading image data for the message.");
  534. FIRInAppMessagingDisplayMessage *erroredMessage =
  535. [self displayMessageWithMessageDefinition:message
  536. imageData:imageData
  537. landscapeImageData:landscapeImageData
  538. triggerType:triggerType];
  539. // short-circuit to display error handling
  540. [self displayErrorForMessage:erroredMessage error:error];
  541. return;
  542. } else {
  543. if (standardImageRawData) {
  544. #pragma clang diagnostic push
  545. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  546. imageData = [[FIRInAppMessagingImageData alloc]
  547. initWithImageURL:message.renderData.contentData.imageURL.absoluteString
  548. imageData:standardImageRawData];
  549. #pragma clang diagnostic pop
  550. }
  551. if (landscapeImageRawData) {
  552. #pragma clang diagnostic push
  553. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  554. landscapeImageData = [[FIRInAppMessagingImageData alloc]
  555. initWithImageURL:message.renderData.contentData.landscapeImageURL.absoluteString
  556. imageData:landscapeImageRawData];
  557. #pragma clang diagnostic pop
  558. }
  559. }
  560. self.impressionRecorded = NO;
  561. self.isMsgBeingDisplayed = YES;
  562. FIRInAppMessagingDisplayMessage *displayMessage =
  563. [self displayMessageWithMessageDefinition:message
  564. imageData:imageData
  565. landscapeImageData:landscapeImageData
  566. triggerType:triggerType];
  567. [self.messageDisplayComponent displayMessage:displayMessage displayDelegate:self];
  568. }];
  569. }
  570. - (BOOL)enoughIntervalFromLastDisplay {
  571. NSTimeInterval intervalFromLastDisplayInSeconds =
  572. [self.timeFetcher currentTimestampInSeconds] - self.lastDisplayTime;
  573. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400005",
  574. @"Interval time from last display is %lf seconds", intervalFromLastDisplayInSeconds);
  575. return intervalFromLastDisplayInSeconds >= self.setting.displayMinIntervalInMinutes * 60.0;
  576. }
  577. - (void)checkAndDisplayNextAppLaunchMessage {
  578. // synchronizing on self so that we won't potentially enter the render flow from two
  579. // threads.
  580. @synchronized(self) {
  581. if (!self.messageDisplayComponent) {
  582. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400028",
  583. @"Message display component is not present yet. No display should happen.");
  584. return;
  585. }
  586. if (self.suppressMessageDisplay) {
  587. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400029",
  588. @"Message display is being suppressed. No regular message rendering.");
  589. return;
  590. }
  591. if (self.isMsgBeingDisplayed) {
  592. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400030",
  593. @"An in-app message display is in progress, do not over-display on top of it.");
  594. return;
  595. }
  596. if ([self.messageCache hasTestMessage] || [self enoughIntervalFromLastDisplay]) {
  597. // We can display test messages anytime or display regular messages when
  598. // the display time interval has been reached
  599. FIRIAMMessageDefinition *nextAppLaunchMessage = [self.messageCache nextOnAppLaunchDisplayMsg];
  600. if (nextAppLaunchMessage) {
  601. [self displayForMessage:nextAppLaunchMessage
  602. triggerType:FIRInAppMessagingDisplayTriggerTypeOnAnalyticsEvent];
  603. self.lastDisplayTime = [self.timeFetcher currentTimestampInSeconds];
  604. } else {
  605. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400040",
  606. @"No appropriate in-app message detected for display.");
  607. }
  608. } else {
  609. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400041",
  610. @"Minimal display interval of %lf seconds has not been reached yet.",
  611. self.setting.displayMinIntervalInMinutes * 60.0);
  612. }
  613. }
  614. }
  615. - (void)checkAndDisplayNextAppForegroundMessage {
  616. // synchronizing on self so that we won't potentially enter the render flow from two
  617. // threads: example like showing analytics triggered message and a regular app open
  618. // triggered message concurrently
  619. @synchronized(self) {
  620. if (!self.messageDisplayComponent) {
  621. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400027",
  622. @"Message display component is not present yet. No display should happen.");
  623. return;
  624. }
  625. if (self.suppressMessageDisplay) {
  626. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400016",
  627. @"Message display is being suppressed. No regular message rendering.");
  628. return;
  629. }
  630. if (self.isMsgBeingDisplayed) {
  631. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400002",
  632. @"An in-app message display is in progress, do not over-display on top of it.");
  633. return;
  634. }
  635. if ([self.messageCache hasTestMessage] || [self enoughIntervalFromLastDisplay]) {
  636. // We can display test messages anytime or display regular messages when
  637. // the display time interval has been reached
  638. FIRIAMMessageDefinition *nextForegroundMessage = [self.messageCache nextOnAppOpenDisplayMsg];
  639. if (nextForegroundMessage) {
  640. [self displayForMessage:nextForegroundMessage
  641. triggerType:FIRInAppMessagingDisplayTriggerTypeOnAppForeground];
  642. self.lastDisplayTime = [self.timeFetcher currentTimestampInSeconds];
  643. } else {
  644. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400001",
  645. @"No appropriate in-app message detected for display.");
  646. }
  647. } else {
  648. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400003",
  649. @"Minimal display interval of %lf seconds has not been reached yet.",
  650. self.setting.displayMinIntervalInMinutes * 60.0);
  651. }
  652. }
  653. }
  654. @end