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