FIRIAMDisplayExecutor.m 32 KB

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