FIRIAMDisplayExecutor.m 35 KB

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