FIRInAppMessaging.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // Only configure for the default FIRApp.
  47. if (!container.app.isDefaultApp) {
  48. FIRLogError(kFIRLoggerInAppMessaging, @"I-IAM170000",
  49. @"In-App Messaging must be used with the default Firebase app.");
  50. return nil;
  51. }
  52. id<FIRAnalyticsInterop> analytics = FIR_COMPONENT(FIRAnalyticsInterop, container);
  53. FIRInstallations *installations = [FIRInstallations installationsWithApp:container.app];
  54. if (_autoBootstrapOnFIRAppInit) {
  55. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170002",
  56. @"Auto bootstrap Firebase in-app messaging SDK");
  57. [FIRInAppMessaging bootstrapIAMFromFIRApp:container.app];
  58. } else {
  59. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170003",
  60. @"No auto bootstrap Firebase in-app messaging SDK");
  61. }
  62. return [[FIRInAppMessaging alloc] initWithAnalytics:analytics installations:installations];
  63. };
  64. FIRComponent *fiamProvider =
  65. [FIRComponent componentWithProtocol:@protocol(FIRInAppMessagingInstanceProvider)
  66. instantiationTiming:FIRInstantiationTimingEagerInDefaultApp
  67. creationBlock:creationBlock];
  68. return @[ fiamProvider ];
  69. }
  70. - (instancetype)initWithAnalytics:(id<FIRAnalyticsInterop>)analytics
  71. installations:(FIRInstallations *)installations {
  72. if (self = [super init]) {
  73. _messageDisplaySuppressed = NO;
  74. _analytics = analytics;
  75. _installations = installations;
  76. }
  77. return self;
  78. }
  79. + (FIRInAppMessaging *)inAppMessaging {
  80. FIRApp *defaultApp = [FIRApp defaultApp]; // Missing configure will be logged here.
  81. id<FIRInAppMessagingInstanceProvider> inAppMessaging =
  82. FIR_COMPONENT(FIRInAppMessagingInstanceProvider, defaultApp.container);
  83. return (FIRInAppMessaging *)inAppMessaging;
  84. }
  85. - (BOOL)messageDisplaySuppressed {
  86. return _messageDisplaySuppressed;
  87. }
  88. - (void)setMessageDisplaySuppressed:(BOOL)suppressed {
  89. _messageDisplaySuppressed = suppressed;
  90. [[FIRIAMRuntimeManager getSDKRuntimeInstance] setShouldSuppressMessageDisplay:suppressed];
  91. }
  92. - (BOOL)automaticDataCollectionEnabled {
  93. return [FIRIAMRuntimeManager getSDKRuntimeInstance].automaticDataCollectionEnabled;
  94. }
  95. - (void)setAutomaticDataCollectionEnabled:(BOOL)automaticDataCollectionEnabled {
  96. [FIRIAMRuntimeManager getSDKRuntimeInstance].automaticDataCollectionEnabled =
  97. automaticDataCollectionEnabled;
  98. }
  99. - (void)setMessageDisplayComponent:(id<FIRInAppMessagingDisplay>)messageDisplayComponent {
  100. _messageDisplayComponent = messageDisplayComponent;
  101. if (messageDisplayComponent == nil) {
  102. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM290002", @"messageDisplayComponent set to nil.");
  103. } else {
  104. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM290001",
  105. @"Setting a non-nil message display component");
  106. }
  107. // Forward the setting to the display executor.
  108. [FIRIAMRuntimeManager getSDKRuntimeInstance].displayExecutor.messageDisplayComponent =
  109. messageDisplayComponent;
  110. }
  111. - (void)triggerEvent:(NSString *)eventName {
  112. [[FIRIAMRuntimeManager getSDKRuntimeInstance].displayExecutor
  113. checkAndDisplayNextContextualMessageForAnalyticsEvent:eventName];
  114. }
  115. @end
  116. #endif // TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION