FIRIAMDisplayExecutor.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. cardMessage.body = body;
  399. cardMessage.landscapeImageData = landscapeImageData;
  400. cardMessage.secondaryActionButton = secondaryActionButton;
  401. cardMessage.secondaryActionURL = definition.renderData.contentData.secondaryActionURL;
  402. return cardMessage;
  403. }
  404. - (FIRInAppMessagingBannerDisplay *)
  405. bannerDisplayMessageWithMessageDefinition:(FIRIAMMessageDefinition *)definition
  406. imageData:(FIRInAppMessagingImageData *)imageData
  407. triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
  408. NSString *title = definition.renderData.contentData.titleText;
  409. NSString *body = definition.renderData.contentData.bodyText;
  410. #pragma clang diagnostic push
  411. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  412. FIRInAppMessagingBannerDisplay *bannerMessage = [[FIRInAppMessagingBannerDisplay alloc]
  413. initWithMessageID:definition.renderData.messageID
  414. campaignName:definition.renderData.name
  415. renderAsTestMessage:definition.isTestMessage
  416. triggerType:triggerType
  417. titleText:title
  418. bodyText:body
  419. textColor:definition.renderData.renderingEffectSettings.textColor
  420. backgroundColor:definition.renderData.renderingEffectSettings.displayBGColor
  421. imageData:imageData
  422. actionURL:definition.renderData.contentData.actionURL];
  423. #pragma clang diagnostic pop
  424. return bannerMessage;
  425. }
  426. - (FIRInAppMessagingImageOnlyDisplay *)
  427. imageOnlyDisplayMessageWithMessageDefinition:(FIRIAMMessageDefinition *)definition
  428. imageData:(FIRInAppMessagingImageData *)imageData
  429. triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
  430. #pragma clang diagnostic push
  431. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  432. FIRInAppMessagingImageOnlyDisplay *imageOnlyMessage = [[FIRInAppMessagingImageOnlyDisplay alloc]
  433. initWithMessageID:definition.renderData.messageID
  434. campaignName:definition.renderData.name
  435. renderAsTestMessage:definition.isTestMessage
  436. triggerType:triggerType
  437. imageData:imageData
  438. actionURL:definition.renderData.contentData.actionURL];
  439. #pragma clang diagnostic pop
  440. return imageOnlyMessage;
  441. }
  442. - (FIRInAppMessagingModalDisplay *)
  443. modalDisplayMessageWithMessageDefinition:(FIRIAMMessageDefinition *)definition
  444. imageData:(FIRInAppMessagingImageData *)imageData
  445. triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
  446. // For easier reference in this method.
  447. FIRIAMMessageRenderData *renderData = definition.renderData;
  448. NSString *title = renderData.contentData.titleText;
  449. NSString *body = renderData.contentData.bodyText;
  450. FIRInAppMessagingActionButton *actionButton = nil;
  451. if (definition.renderData.contentData.actionButtonText) {
  452. #pragma clang diagnostic push
  453. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  454. actionButton = [[FIRInAppMessagingActionButton alloc]
  455. initWithButtonText:renderData.contentData.actionButtonText
  456. buttonTextColor:renderData.renderingEffectSettings.btnTextColor
  457. backgroundColor:renderData.renderingEffectSettings.btnBGColor];
  458. #pragma clang diagnostic pop
  459. }
  460. #pragma clang diagnostic push
  461. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  462. FIRInAppMessagingModalDisplay *modalViewMessage = [[FIRInAppMessagingModalDisplay alloc]
  463. initWithMessageID:definition.renderData.messageID
  464. campaignName:definition.renderData.name
  465. renderAsTestMessage:definition.isTestMessage
  466. triggerType:triggerType
  467. titleText:title
  468. bodyText:body
  469. textColor:renderData.renderingEffectSettings.textColor
  470. backgroundColor:renderData.renderingEffectSettings.displayBGColor
  471. imageData:imageData
  472. actionButton:actionButton
  473. actionURL:definition.renderData.contentData.actionURL];
  474. #pragma clang diagnostic pop
  475. return modalViewMessage;
  476. }
  477. - (FIRInAppMessagingDisplayMessage *)
  478. displayMessageWithMessageDefinition:(FIRIAMMessageDefinition *)definition
  479. imageData:(FIRInAppMessagingImageData *)imageData
  480. landscapeImageData:(nullable FIRInAppMessagingImageData *)landscapeImageData
  481. triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
  482. switch (definition.renderData.renderingEffectSettings.viewMode) {
  483. case FIRIAMRenderAsCardView:
  484. // Image data should never nil for a valid card message.
  485. if (imageData == nil) {
  486. return nil;
  487. }
  488. return [self cardDisplayMessageWithMessageDefinition:definition
  489. portraitImageData:imageData
  490. landscapeImageData:landscapeImageData
  491. triggerType:triggerType];
  492. case FIRIAMRenderAsBannerView:
  493. return [self bannerDisplayMessageWithMessageDefinition:definition
  494. imageData:imageData
  495. triggerType:triggerType];
  496. case FIRIAMRenderAsModalView:
  497. return [self modalDisplayMessageWithMessageDefinition:definition
  498. imageData:imageData
  499. triggerType:triggerType];
  500. case FIRIAMRenderAsImageOnlyView:
  501. return [self imageOnlyDisplayMessageWithMessageDefinition:definition
  502. imageData:imageData
  503. triggerType:triggerType];
  504. default:
  505. return nil;
  506. }
  507. }
  508. - (void)displayForMessage:(FIRIAMMessageDefinition *)message
  509. triggerType:(FIRInAppMessagingDisplayTriggerType)triggerType {
  510. _currentMsgBeingDisplayed = message;
  511. [message.renderData.contentData
  512. loadImageDataWithBlock:^(NSData *_Nullable standardImageRawData,
  513. NSData *_Nullable landscapeImageRawData, NSError *_Nullable error) {
  514. FIRInAppMessagingImageData *imageData = nil;
  515. FIRInAppMessagingImageData *landscapeImageData = nil;
  516. if (error) {
  517. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400019",
  518. @"Error in loading image data for the message.");
  519. FIRInAppMessagingDisplayMessage *erroredMessage =
  520. [self displayMessageWithMessageDefinition:message
  521. imageData:imageData
  522. landscapeImageData:landscapeImageData
  523. triggerType:triggerType];
  524. // short-circuit to display error handling
  525. [self displayErrorForMessage:erroredMessage error:error];
  526. return;
  527. } else {
  528. if (standardImageRawData) {
  529. #pragma clang diagnostic push
  530. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  531. imageData = [[FIRInAppMessagingImageData alloc]
  532. initWithImageURL:message.renderData.contentData.imageURL.absoluteString
  533. imageData:standardImageRawData];
  534. #pragma clang diagnostic pop
  535. }
  536. if (landscapeImageRawData) {
  537. #pragma clang diagnostic push
  538. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  539. landscapeImageData = [[FIRInAppMessagingImageData alloc]
  540. initWithImageURL:message.renderData.contentData.landscapeImageURL.absoluteString
  541. imageData:landscapeImageRawData];
  542. #pragma clang diagnostic pop
  543. }
  544. }
  545. self.impressionRecorded = NO;
  546. self.isMsgBeingDisplayed = YES;
  547. FIRInAppMessagingDisplayMessage *displayMessage =
  548. [self displayMessageWithMessageDefinition:message
  549. imageData:imageData
  550. landscapeImageData:landscapeImageData
  551. triggerType:triggerType];
  552. [self.messageDisplayComponent displayMessage:displayMessage displayDelegate:self];
  553. }];
  554. }
  555. - (BOOL)enoughIntervalFromLastDisplay {
  556. NSTimeInterval intervalFromLastDisplayInSeconds =
  557. [self.timeFetcher currentTimestampInSeconds] - self.lastDisplayTime;
  558. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400005",
  559. @"Interval time from last display is %lf seconds", intervalFromLastDisplayInSeconds);
  560. return intervalFromLastDisplayInSeconds >= self.setting.displayMinIntervalInMinutes * 60.0;
  561. }
  562. - (void)checkAndDisplayNextAppLaunchMessage {
  563. // synchronizing on self so that we won't potentially enter the render flow from two
  564. // threads.
  565. @synchronized(self) {
  566. if (!self.messageDisplayComponent) {
  567. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400028",
  568. @"Message display component is not present yet. No display should happen.");
  569. return;
  570. }
  571. if (self.suppressMessageDisplay) {
  572. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400029",
  573. @"Message display is being suppressed. No regular message rendering.");
  574. return;
  575. }
  576. if (self.isMsgBeingDisplayed) {
  577. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400030",
  578. @"An in-app message display is in progress, do not over-display on top of it.");
  579. return;
  580. }
  581. if ([self.messageCache hasTestMessage] || [self enoughIntervalFromLastDisplay]) {
  582. // We can display test messages anytime or display regular messages when
  583. // the display time interval has been reached
  584. FIRIAMMessageDefinition *nextAppLaunchMessage = [self.messageCache nextOnAppLaunchDisplayMsg];
  585. if (nextAppLaunchMessage) {
  586. [self displayForMessage:nextAppLaunchMessage
  587. triggerType:FIRInAppMessagingDisplayTriggerTypeOnAnalyticsEvent];
  588. self.lastDisplayTime = [self.timeFetcher currentTimestampInSeconds];
  589. } else {
  590. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400040",
  591. @"No appropriate in-app message detected for display.");
  592. }
  593. } else {
  594. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400041",
  595. @"Minimal display interval of %lf seconds has not been reached yet.",
  596. self.setting.displayMinIntervalInMinutes * 60.0);
  597. }
  598. }
  599. }
  600. - (void)checkAndDisplayNextAppForegroundMessage {
  601. // synchronizing on self so that we won't potentially enter the render flow from two
  602. // threads: example like showing analytics triggered message and a regular app open
  603. // triggered message concurrently
  604. @synchronized(self) {
  605. if (!self.messageDisplayComponent) {
  606. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400027",
  607. @"Message display component is not present yet. No display should happen.");
  608. return;
  609. }
  610. if (self.suppressMessageDisplay) {
  611. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400016",
  612. @"Message display is being suppressed. No regular message rendering.");
  613. return;
  614. }
  615. if (self.isMsgBeingDisplayed) {
  616. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400002",
  617. @"An in-app message display is in progress, do not over-display on top of it.");
  618. return;
  619. }
  620. if ([self.messageCache hasTestMessage] || [self enoughIntervalFromLastDisplay]) {
  621. // We can display test messages anytime or display regular messages when
  622. // the display time interval has been reached
  623. FIRIAMMessageDefinition *nextForegroundMessage = [self.messageCache nextOnAppOpenDisplayMsg];
  624. if (nextForegroundMessage) {
  625. [self displayForMessage:nextForegroundMessage
  626. triggerType:FIRInAppMessagingDisplayTriggerTypeOnAppForeground];
  627. self.lastDisplayTime = [self.timeFetcher currentTimestampInSeconds];
  628. } else {
  629. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400001",
  630. @"No appropriate in-app message detected for display.");
  631. }
  632. } else {
  633. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM400003",
  634. @"Minimal display interval of %lf seconds has not been reached yet.",
  635. self.setting.displayMinIntervalInMinutes * 60.0);
  636. }
  637. }
  638. }
  639. @end