FIRIAMDisplayExecutor.m 33 KB

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