FIRInAppMessaging+Bootstrap.m 5.3 KB

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