FIRInAppMessaging.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "FIRInAppMessaging.h"
  17. #import <Foundation/Foundation.h>
  18. #import <FirebaseAnalyticsInterop/FIRAnalyticsInterop.h>
  19. #import <FirebaseCore/FIRAppInternal.h>
  20. #import <FirebaseCore/FIRComponent.h>
  21. #import <FirebaseCore/FIRComponentContainer.h>
  22. #import <FirebaseCore/FIRDependency.h>
  23. #import <FirebaseInstallations/FIRInstallations.h>
  24. #import "FIRCore+InAppMessaging.h"
  25. #import "FIRIAMDisplayExecutor.h"
  26. #import "FIRIAMRuntimeManager.h"
  27. #import "FIRInAppMessaging+Bootstrap.h"
  28. #import "FIRInAppMessagingPrivate.h"
  29. static BOOL _autoBootstrapOnFIRAppInit = YES;
  30. @implementation FIRInAppMessaging {
  31. BOOL _messageDisplaySuppressed;
  32. }
  33. // Call this to present the SDK being auto bootstrapped with other Firebase SDKs. It needs
  34. // to be triggered before [FIRApp configure] is executed. This should only be needed for
  35. // testing app that wants to use custom fiam SDK settings.
  36. + (void)disableAutoBootstrapWithFIRApp {
  37. _autoBootstrapOnFIRAppInit = NO;
  38. }
  39. // extract macro value into a C string
  40. #define STR_FROM_MACRO(x) #x
  41. #define STR(x) STR_FROM_MACRO(x)
  42. + (void)load {
  43. [FIRApp
  44. registerInternalLibrary:(Class<FIRLibrary>)self
  45. withName:@"fire-fiam"
  46. withVersion:[NSString stringWithUTF8String:STR(FIRInAppMessaging_LIB_VERSION)]];
  47. }
  48. + (nonnull NSArray<FIRComponent *> *)componentsToRegister {
  49. FIRDependency *analyticsDep = [FIRDependency dependencyWithProtocol:@protocol(FIRAnalyticsInterop)
  50. isRequired:YES];
  51. FIRComponentCreationBlock creationBlock =
  52. ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  53. // Ensure it's cached so it returns the same instance every time fiam is called.
  54. *isCacheable = YES;
  55. id<FIRAnalyticsInterop> analytics = FIR_COMPONENT(FIRAnalyticsInterop, container);
  56. FIRInstallations *installations = [FIRInstallations installationsWithApp:container.app];
  57. return [[FIRInAppMessaging alloc] initWithAnalytics:analytics installations:installations];
  58. };
  59. FIRComponent *fiamProvider =
  60. [FIRComponent componentWithProtocol:@protocol(FIRInAppMessagingInstanceProvider)
  61. instantiationTiming:FIRInstantiationTimingLazy
  62. dependencies:@[ analyticsDep ]
  63. creationBlock:creationBlock];
  64. return @[ fiamProvider ];
  65. }
  66. + (void)configureWithApp:(FIRApp *)app {
  67. if (!app.isDefaultApp) {
  68. // Only configure for the default FIRApp.
  69. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170000",
  70. @"Firebase InAppMessaging only works with the default app.");
  71. return;
  72. }
  73. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170001",
  74. @"Got notification for kFIRAppReadyToConfigureSDKNotification");
  75. if (_autoBootstrapOnFIRAppInit) {
  76. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170002",
  77. @"Auto bootstrap Firebase in-app messaging SDK");
  78. [self bootstrapIAMFromFIRApp:app];
  79. } else {
  80. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170003",
  81. @"No auto bootstrap Firebase in-app messaging SDK");
  82. }
  83. }
  84. - (instancetype)initWithAnalytics:(id<FIRAnalyticsInterop>)analytics
  85. installations:(FIRInstallations *)installations {
  86. if (self = [super init]) {
  87. _messageDisplaySuppressed = NO;
  88. _analytics = analytics;
  89. _installations = installations;
  90. }
  91. return self;
  92. }
  93. + (FIRInAppMessaging *)inAppMessaging {
  94. FIRApp *defaultApp = [FIRApp defaultApp]; // Missing configure will be logged here.
  95. id<FIRInAppMessagingInstanceProvider> inAppMessaging =
  96. FIR_COMPONENT(FIRInAppMessagingInstanceProvider, defaultApp.container);
  97. return (FIRInAppMessaging *)inAppMessaging;
  98. }
  99. - (BOOL)messageDisplaySuppressed {
  100. return _messageDisplaySuppressed;
  101. }
  102. - (void)setMessageDisplaySuppressed:(BOOL)suppressed {
  103. _messageDisplaySuppressed = suppressed;
  104. [[FIRIAMRuntimeManager getSDKRuntimeInstance] setShouldSuppressMessageDisplay:suppressed];
  105. }
  106. - (BOOL)automaticDataCollectionEnabled {
  107. return [FIRIAMRuntimeManager getSDKRuntimeInstance].automaticDataCollectionEnabled;
  108. }
  109. - (void)setAutomaticDataCollectionEnabled:(BOOL)automaticDataCollectionEnabled {
  110. [FIRIAMRuntimeManager getSDKRuntimeInstance].automaticDataCollectionEnabled =
  111. automaticDataCollectionEnabled;
  112. }
  113. - (void)setMessageDisplayComponent:(id<FIRInAppMessagingDisplay>)messageDisplayComponent {
  114. _messageDisplayComponent = messageDisplayComponent;
  115. if (messageDisplayComponent == nil) {
  116. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM290002", @"messageDisplayComponent set to nil.");
  117. } else {
  118. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM290001",
  119. @"Setting a non-nil message display component");
  120. }
  121. // Forward the setting to the display executor.
  122. [FIRIAMRuntimeManager getSDKRuntimeInstance].displayExecutor.messageDisplayComponent =
  123. messageDisplayComponent;
  124. }
  125. - (void)triggerEvent:(NSString *)eventName {
  126. [[FIRIAMRuntimeManager getSDKRuntimeInstance].displayExecutor
  127. checkAndDisplayNextContextualMessageForAnalyticsEvent:eventName];
  128. }
  129. @end