FIRInAppMessaging.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "FIRCore+InAppMessaging.h"
  24. #import "FIRIAMDisplayExecutor.h"
  25. #import "FIRIAMRuntimeManager.h"
  26. #import "FIRInAppMessaging+Bootstrap.h"
  27. #import "FIRInAppMessagingPrivate.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. return [[FIRInAppMessaging alloc] initWithAnalytics:analytics];
  56. };
  57. FIRComponent *fiamProvider =
  58. [FIRComponent componentWithProtocol:@protocol(FIRInAppMessagingInstanceProvider)
  59. instantiationTiming:FIRInstantiationTimingLazy
  60. dependencies:@[ analyticsDep ]
  61. creationBlock:creationBlock];
  62. return @[ fiamProvider ];
  63. }
  64. + (void)configureWithApp:(FIRApp *)app {
  65. if (!app.isDefaultApp) {
  66. // Only configure for the default FIRApp.
  67. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170000",
  68. @"Firebase InAppMessaging only works with the default app.");
  69. return;
  70. }
  71. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170001",
  72. @"Got notification for kFIRAppReadyToConfigureSDKNotification");
  73. if (_autoBootstrapOnFIRAppInit) {
  74. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170002",
  75. @"Auto bootstrap Firebase in-app messaging SDK");
  76. [self bootstrapIAMFromFIRApp:app];
  77. } else {
  78. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170003",
  79. @"No auto bootstrap Firebase in-app messaging SDK");
  80. }
  81. }
  82. - (instancetype)initWithAnalytics:(id<FIRAnalyticsInterop>)analytics {
  83. if (self = [super init]) {
  84. _messageDisplaySuppressed = NO;
  85. _analytics = analytics;
  86. }
  87. return self;
  88. }
  89. + (FIRInAppMessaging *)inAppMessaging {
  90. FIRApp *defaultApp = [FIRApp defaultApp]; // Missing configure will be logged here.
  91. id<FIRInAppMessagingInstanceProvider> inAppMessaging =
  92. FIR_COMPONENT(FIRInAppMessagingInstanceProvider, defaultApp.container);
  93. return (FIRInAppMessaging *)inAppMessaging;
  94. }
  95. - (BOOL)messageDisplaySuppressed {
  96. return _messageDisplaySuppressed;
  97. }
  98. - (void)setMessageDisplaySuppressed:(BOOL)suppressed {
  99. _messageDisplaySuppressed = suppressed;
  100. [[FIRIAMRuntimeManager getSDKRuntimeInstance] setShouldSuppressMessageDisplay:suppressed];
  101. }
  102. - (BOOL)automaticDataCollectionEnabled {
  103. return [FIRIAMRuntimeManager getSDKRuntimeInstance].automaticDataCollectionEnabled;
  104. }
  105. - (void)setAutomaticDataCollectionEnabled:(BOOL)automaticDataCollectionEnabled {
  106. [FIRIAMRuntimeManager getSDKRuntimeInstance].automaticDataCollectionEnabled =
  107. automaticDataCollectionEnabled;
  108. }
  109. - (void)setMessageDisplayComponent:(id<FIRInAppMessagingDisplay>)messageDisplayComponent {
  110. _messageDisplayComponent = messageDisplayComponent;
  111. if (messageDisplayComponent == nil) {
  112. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM290002", @"messageDisplayComponent set to nil.");
  113. } else {
  114. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM290001",
  115. @"Setting a non-nil message display component");
  116. }
  117. // Forward the setting to the display executor.
  118. [FIRIAMRuntimeManager getSDKRuntimeInstance].displayExecutor.messageDisplayComponent =
  119. messageDisplayComponent;
  120. }
  121. - (void)triggerEvent:(NSString *)eventName {
  122. [[FIRIAMRuntimeManager getSDKRuntimeInstance].displayExecutor
  123. checkAndDisplayNextContextualMessageForAnalyticsEvent:eventName];
  124. }
  125. @end