FIRInAppMessaging.m 5.5 KB

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