FIRInAppMessaging.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "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. + (void)load {
  39. [FIRApp registerInternalLibrary:(Class<FIRLibrary>)self withName:@"fire-fiam"];
  40. }
  41. + (nonnull NSArray<FIRComponent *> *)componentsToRegister {
  42. FIRDependency *analyticsDep = [FIRDependency dependencyWithProtocol:@protocol(FIRAnalyticsInterop)
  43. isRequired:YES];
  44. FIRComponentCreationBlock creationBlock =
  45. ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  46. // Ensure it's cached so it returns the same instance every time fiam is called.
  47. *isCacheable = YES;
  48. id<FIRAnalyticsInterop> analytics = FIR_COMPONENT(FIRAnalyticsInterop, container);
  49. FIRInstallations *installations = [FIRInstallations installationsWithApp:container.app];
  50. return [[FIRInAppMessaging alloc] initWithAnalytics:analytics installations:installations];
  51. };
  52. FIRComponent *fiamProvider =
  53. [FIRComponent componentWithProtocol:@protocol(FIRInAppMessagingInstanceProvider)
  54. instantiationTiming:FIRInstantiationTimingLazy
  55. dependencies:@[ analyticsDep ]
  56. creationBlock:creationBlock];
  57. return @[ fiamProvider ];
  58. }
  59. + (void)configureWithApp:(FIRApp *)app {
  60. if (!app.isDefaultApp) {
  61. // Only configure for the default FIRApp.
  62. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170000",
  63. @"Firebase InAppMessaging only works with the default app.");
  64. return;
  65. }
  66. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170001",
  67. @"Got notification for kFIRAppReadyToConfigureSDKNotification");
  68. if (_autoBootstrapOnFIRAppInit) {
  69. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170002",
  70. @"Auto bootstrap Firebase in-app messaging SDK");
  71. [self bootstrapIAMFromFIRApp:app];
  72. } else {
  73. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170003",
  74. @"No auto bootstrap Firebase in-app messaging SDK");
  75. }
  76. }
  77. - (instancetype)initWithAnalytics:(id<FIRAnalyticsInterop>)analytics
  78. installations:(FIRInstallations *)installations {
  79. if (self = [super init]) {
  80. _messageDisplaySuppressed = NO;
  81. _analytics = analytics;
  82. _installations = installations;
  83. }
  84. return self;
  85. }
  86. + (FIRInAppMessaging *)inAppMessaging {
  87. FIRApp *defaultApp = [FIRApp defaultApp]; // Missing configure will be logged here.
  88. id<FIRInAppMessagingInstanceProvider> inAppMessaging =
  89. FIR_COMPONENT(FIRInAppMessagingInstanceProvider, defaultApp.container);
  90. return (FIRInAppMessaging *)inAppMessaging;
  91. }
  92. - (BOOL)messageDisplaySuppressed {
  93. return _messageDisplaySuppressed;
  94. }
  95. - (void)setMessageDisplaySuppressed:(BOOL)suppressed {
  96. _messageDisplaySuppressed = suppressed;
  97. [[FIRIAMRuntimeManager getSDKRuntimeInstance] setShouldSuppressMessageDisplay:suppressed];
  98. }
  99. - (BOOL)automaticDataCollectionEnabled {
  100. return [FIRIAMRuntimeManager getSDKRuntimeInstance].automaticDataCollectionEnabled;
  101. }
  102. - (void)setAutomaticDataCollectionEnabled:(BOOL)automaticDataCollectionEnabled {
  103. [FIRIAMRuntimeManager getSDKRuntimeInstance].automaticDataCollectionEnabled =
  104. automaticDataCollectionEnabled;
  105. }
  106. - (void)setMessageDisplayComponent:(id<FIRInAppMessagingDisplay>)messageDisplayComponent {
  107. _messageDisplayComponent = messageDisplayComponent;
  108. if (messageDisplayComponent == nil) {
  109. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM290002", @"messageDisplayComponent set to nil.");
  110. } else {
  111. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM290001",
  112. @"Setting a non-nil message display component");
  113. }
  114. // Forward the setting to the display executor.
  115. [FIRIAMRuntimeManager getSDKRuntimeInstance].displayExecutor.messageDisplayComponent =
  116. messageDisplayComponent;
  117. }
  118. - (void)triggerEvent:(NSString *)eventName {
  119. [[FIRIAMRuntimeManager getSDKRuntimeInstance].displayExecutor
  120. checkAndDisplayNextContextualMessageForAnalyticsEvent:eventName];
  121. }
  122. @end
  123. #endif // TARGET_OS_IOS || TARGET_OS_TV