FIRInAppMessaging.m 5.8 KB

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