FIRInAppMessaging.m 5.4 KB

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