FIRInAppMessaging+Bootstrap.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/Private/Runtime/FIRInAppMessaging+Bootstrap.h"
  19. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  20. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  21. #import "Interop/Analytics/Public/FIRAnalyticsInterop.h"
  22. #import "FirebaseInAppMessaging/Sources/FIRCore+InAppMessaging.h"
  23. #import "FirebaseInAppMessaging/Sources/Private/Analytics/FIRIAMClearcutUploader.h"
  24. #import "FirebaseInAppMessaging/Sources/Private/Runtime/FIRIAMRuntimeManager.h"
  25. #import "FirebaseInAppMessaging/Sources/Private/Runtime/FIRIAMSDKSettings.h"
  26. #import "FirebaseInAppMessaging/Sources/Private/Util/NSString+FIRInterlaceStrings.h"
  27. #import "FirebaseInAppMessaging/Sources/Public/FirebaseInAppMessaging/FIRInAppMessagingErrors.h"
  28. @implementation FIRInAppMessaging (Bootstrap)
  29. static FIRIAMSDKSettings *_sdkSetting = nil;
  30. static NSString *_fiamServerHostName = @"firebaseinappmessaging.googleapis.com";
  31. + (NSString *)getFiamServerHost {
  32. return _fiamServerHostName;
  33. }
  34. + (void)setFiamServerHostWithName:(NSString *)serverHost {
  35. _fiamServerHostName = serverHost;
  36. }
  37. + (NSString *)getServer {
  38. // Override to change to test server.
  39. NSString *serverHostNameFirstComponent = @"pa.ogepscm";
  40. NSString *serverHostNameSecondComponent = @"lygolai.o";
  41. return [NSString fir_interlaceString:serverHostNameFirstComponent
  42. withString:serverHostNameSecondComponent];
  43. }
  44. + (void)bootstrapIAMFromFIRApp:(FIRApp *)app {
  45. FIROptions *options = app.options;
  46. NSError *error;
  47. if (!options.GCMSenderID.length) {
  48. error =
  49. [NSError errorWithDomain:FIRInAppMessagingErrorDomain
  50. code:0
  51. userInfo:@{
  52. NSLocalizedDescriptionKey : @"Google Sender ID must not be nil or empty."
  53. }];
  54. [self exitAppWithFatalError:error];
  55. }
  56. if (!options.APIKey.length) {
  57. error = [NSError
  58. errorWithDomain:FIRInAppMessagingErrorDomain
  59. code:0
  60. userInfo:@{NSLocalizedDescriptionKey : @"API key must not be nil or empty."}];
  61. [self exitAppWithFatalError:error];
  62. }
  63. if (!options.googleAppID.length) {
  64. error =
  65. [NSError errorWithDomain:FIRInAppMessagingErrorDomain
  66. code:0
  67. userInfo:@{NSLocalizedDescriptionKey : @"Google App ID must not be nil."}];
  68. [self exitAppWithFatalError:error];
  69. }
  70. // following are the default sdk settings to be used by hosting app
  71. _sdkSetting = [[FIRIAMSDKSettings alloc] init];
  72. _sdkSetting.apiServerHost = [FIRInAppMessaging getFiamServerHost];
  73. _sdkSetting.clearcutServerHost = [FIRInAppMessaging getServer];
  74. _sdkSetting.apiHttpProtocol = @"https";
  75. _sdkSetting.firebaseAppId = options.googleAppID;
  76. _sdkSetting.firebaseProjectNumber = options.GCMSenderID;
  77. _sdkSetting.apiKey = options.APIKey;
  78. _sdkSetting.fetchMinIntervalInMinutes = 24 * 60; // fetch at most once every 24 hours
  79. _sdkSetting.loggerMaxCountBeforeReduce = 100;
  80. _sdkSetting.loggerSizeAfterReduce = 50;
  81. _sdkSetting.appFGRenderMinIntervalInMinutes = 24 * 60; // render at most one message from
  82. // app-foreground trigger every 24 hours
  83. _sdkSetting.loggerInVerboseMode = NO;
  84. // TODO: once Firebase Core supports sending notifications at global Firebase level setting
  85. // change, FIAM SDK would listen to it and respond to it. Until then, FIAM SDK only checks
  86. // the setting once upon App/SDK startup.
  87. _sdkSetting.firebaseAutoDataCollectionEnabled = app.isDataCollectionDefaultEnabled;
  88. if ([GULAppEnvironmentUtil isSimulator]) {
  89. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170004",
  90. @"Running in simulator. Do realtime clearcut uploading.");
  91. _sdkSetting.clearcutStrategy =
  92. [[FIRIAMClearcutStrategy alloc] initWithMinWaitTimeInMills:0
  93. maxWaitTimeInMills:0
  94. failureBackoffTimeInMills:60 * 60 * 1000 // 60 mins
  95. batchSendSize:50];
  96. } else {
  97. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM170005",
  98. @"Not running in simulator. Use regular clearcut uploading strategy.");
  99. _sdkSetting.clearcutStrategy =
  100. [[FIRIAMClearcutStrategy alloc] initWithMinWaitTimeInMills:5 * 60 * 1000 // 5 mins
  101. maxWaitTimeInMills:12 * 60 * 60 * 1000 // 12 hours
  102. failureBackoffTimeInMills:60 * 60 * 1000 // 60 mins
  103. batchSendSize:50];
  104. }
  105. [[FIRIAMRuntimeManager getSDKRuntimeInstance] startRuntimeWithSDKSettings:_sdkSetting];
  106. }
  107. + (void)bootstrapIAMWithSettings:(FIRIAMSDKSettings *)settings {
  108. _sdkSetting = settings;
  109. [[FIRIAMRuntimeManager getSDKRuntimeInstance] startRuntimeWithSDKSettings:_sdkSetting];
  110. }
  111. + (void)exitAppWithFatalError:(NSError *)error {
  112. [NSException raise:FIRInAppMessagingErrorDomain
  113. format:@"Error happened %@", error.localizedDescription];
  114. }
  115. @end
  116. #endif // TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION