Skip to content

Commit 887ff82

Browse files
dmandarmaksymmalyhinpaulb777
authored
Migrate from using IID SDK for Remote Config to the new FIS SDK (#5096)
* Update podspec to replace IID with installations SDK. * Update sample app podfile to include installations sdk. * Source code changes to support migration to FIS. The FIS SDK now supports multi-app FIS ids and tokens. * Update RC test app to use FIS SDK. * WIP: Unit tests for incorporating FIS with Remote Config. * Remote Config: fix Installations tests (#5109) * Fix Installations tests. * Fix Firebase/Auth version (#5106) * Remote Config: enable ARC for tests. * run ./scripts/style.sh * Fix warnings * Remote Config: enable ARC for tests. (#5108) * Remote Config: enable ARC for tests. * run ./scripts/style.sh * Fix warnings Co-authored-by: Paul Beusterien <[email protected]> * Add the installations token as an HTTP header. * Add changelog for FIS changes. Co-authored-by: Maksym Malyhin <[email protected]> Co-authored-by: Paul Beusterien <[email protected]>
1 parent 0dd53d0 commit 887ff82

File tree

8 files changed

+148
-145
lines changed

8 files changed

+148
-145
lines changed

FirebaseRemoteConfig.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ app update.
4141
s.dependency 'FirebaseAnalyticsInterop', '~> 1.4'
4242
s.dependency 'FirebaseABTesting', '~> 3.1'
4343
s.dependency 'FirebaseCore', '~> 6.2'
44-
s.dependency 'FirebaseInstanceID', '~> 4.2'
44+
s.dependency 'FirebaseInstallations', '~> 1.1'
4545
s.dependency 'GoogleUtilities/Environment', '~> 6.2'
4646
s.dependency 'GoogleUtilities/NSData+zlib', '~> 6.2'
4747
s.dependency 'Protobuf', '~> 3.9', '>= 3.9.2'

FirebaseRemoteConfig/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# v4.4.10
2+
- [changed] Internal code changes - migrate to using the FIS SDK. (#5096)
13
# v4.4.9
24
- [changed] Internal code changes. (#4934)
35
# v4.4.8

FirebaseRemoteConfig/Sources/Private/RCNConfigSettings.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
@property(nonatomic, copy) NSString *secretToken;
4040
/// Device data version of checkin information.
4141
@property(nonatomic, copy) NSString *deviceDataVersion;
42-
/// InstanceID.
43-
@property(nonatomic, copy) NSString *configInstanceID;
44-
/// InstanceID token.
45-
@property(nonatomic, copy) NSString *configInstanceIDToken;
42+
/// InstallationsID.
43+
@property(nonatomic, copy) NSString *configInstallationsIdentifier;
44+
/// Installations token.
45+
@property(nonatomic, copy) NSString *configInstallationsToken;
4646

4747
/// A list of successful fetch timestamps in milliseconds.
4848
/// TODO Not used anymore. Safe to remove.

FirebaseRemoteConfig/Sources/RCNConfigSettings.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,9 @@ - (NSString *)nextRequestWithUserProperties:(NSDictionary *)userProperties {
334334
// Note: We only set user properties as mentioned in the new REST API Design doc
335335
NSString *ret = [NSString stringWithFormat:@"{"];
336336
ret = [ret stringByAppendingString:[NSString stringWithFormat:@"app_instance_id:'%@'",
337-
_configInstanceID]];
337+
_configInstallationsIdentifier]];
338338
ret = [ret stringByAppendingString:[NSString stringWithFormat:@", app_instance_id_token:'%@'",
339-
_configInstanceIDToken]];
339+
_configInstallationsToken]];
340340
ret = [ret stringByAppendingString:[NSString stringWithFormat:@", app_id:'%@'", _googleAppID]];
341341

342342
ret = [ret stringByAppendingString:[NSString stringWithFormat:@", country_code:'%@'",

FirebaseRemoteConfig/Sources/RCNFetch.m

Lines changed: 35 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
#import "FirebaseRemoteConfig/Sources/RCNConfigFetch.h"
1818

19+
#import <FirebaseCore/FIRApp.h>
1920
#import <FirebaseCore/FIRLogger.h>
2021
#import <FirebaseCore/FIROptions.h>
21-
#import <FirebaseInstanceID/FIRInstanceID+Private.h>
22-
#import <FirebaseInstanceID/FIRInstanceIDCheckinPreferences.h>
22+
#import <FirebaseInstallations/FirebaseInstallations.h>
2323
#import <GoogleUtilities/GULNSData+zlib.h>
2424
#import "FirebaseRemoteConfig/Sources/Private/RCNConfigSettings.h"
2525
#import "FirebaseRemoteConfig/Sources/RCNConfigConstants.h"
@@ -40,7 +40,6 @@
4040
static NSString *const kServerURLQuery = @":fetch?";
4141
static NSString *const kServerURLKey = @"key=";
4242
static NSString *const kRequestJSONKeyAppID = @"app_id";
43-
static NSString *const kRequestJSONKeyAppInstanceID = @"app_instance_id";
4443

4544
static NSString *const kHTTPMethodPost = @"POST"; ///< HTTP request method config fetch using
4645
static NSString *const kContentTypeHeaderName = @"Content-Type"; ///< HTTP Header Field Name
@@ -49,13 +48,13 @@
4948
static NSString *const kAcceptEncodingHeaderName = @"Accept-Encoding"; ///< HTTP Header Field Name
5049
static NSString *const kETagHeaderName = @"etag"; ///< HTTP Header Field Name
5150
static NSString *const kIfNoneMatchETagHeaderName = @"if-none-match"; ///< HTTP Header Field Name
51+
static NSString *const kInstallationsAuthTokenHeaderName = @"x-goog-firebase-installations-auth";
5252
// Sends the bundle ID. Refer to b/130301479 for details.
5353
static NSString *const kiOSBundleIdentifierHeaderName =
5454
@"X-Ios-Bundle-Identifier"; ///< HTTP Header Field Name
5555

5656
/// Config HTTP request content type proto buffer
5757
static NSString *const kContentTypeValueJSON = @"application/json";
58-
static NSString *const kInstanceIDScopeConfig = @"*"; /// InstanceID scope
5958

6059
/// HTTP status codes. Ref: https://cloud.google.com/apis/design/errors#error_retries
6160
static NSInteger const kRCNFetchResponseHTTPStatusCodeOK = 200;
@@ -192,18 +191,22 @@ - (void)fetchConfigWithExpirationDuration:(NSTimeInterval)expirationDuration
192191
withError:error];
193192
}
194193
strongSelf->_settings.isFetchInProgress = YES;
195-
[strongSelf refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler:completionHandler];
194+
[strongSelf refreshInstallationsTokenWithCompletionHandler:completionHandler];
196195
});
197196
}
198197

199198
#pragma mark - Fetch helpers
200199

201-
/// Refresh instance ID token before fetching config. Instance ID is now mandatory for fetch
200+
- (NSString *)FIRAppNameFromFullyQualifiedNamespace {
201+
return [[_FIRNamespace componentsSeparatedByString:@":"] lastObject];
202+
}
203+
/// Refresh installation ID token before fetching config. installation ID is now mandatory for fetch
202204
/// requests to work.(b/14751422).
203-
- (void)refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler:
205+
- (void)refreshInstallationsTokenWithCompletionHandler:
204206
(FIRRemoteConfigFetchCompletion)completionHandler {
205-
FIRInstanceID *instanceID = [FIRInstanceID instanceID];
206-
if (!_options.GCMSenderID) {
207+
FIRInstallations *installations = [FIRInstallations
208+
installationsWithApp:[FIRApp appNamed:[self FIRAppNameFromFullyQualifiedNamespace]]];
209+
if (!installations || !_options.GCMSenderID) {
207210
NSString *errorDescription = @"Failed to get GCMSenderID";
208211
FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000074", @"%@",
209212
[NSString stringWithFormat:@"%@", errorDescription]);
@@ -217,10 +220,11 @@ - (void)refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler:
217220
NSLocalizedDescriptionKey : errorDescription
218221
}]];
219222
}
220-
FIRInstanceIDTokenHandler instanceIDHandler = ^(NSString *token, NSError *error) {
221-
if (!token || error) {
223+
FIRInstallationsTokenHandler installationsTokenHandler = ^(
224+
FIRInstallationsAuthTokenResult *tokenResult, NSError *error) {
225+
if (!tokenResult || !tokenResult.authToken || error) {
222226
NSString *errorDescription =
223-
[NSString stringWithFormat:@"Failed to get InstanceID token. Error : %@.", error];
227+
[NSString stringWithFormat:@"Failed to get installations token. Error : %@.", error];
224228
FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000073", @"%@",
225229
[NSString stringWithFormat:@"%@", errorDescription]);
226230
self->_settings.isFetchInProgress = NO;
@@ -234,20 +238,21 @@ - (void)refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler:
234238
}]];
235239
}
236240

237-
// If the token is available, try to get the instanceID.
241+
// We have a valid token. Get the backing installationID.
238242
__weak RCNConfigFetch *weakSelf = self;
239-
[instanceID getIDWithHandler:^(NSString *_Nullable identity, NSError *_Nullable error) {
243+
[installations installationIDWithCompletion:^(NSString *_Nullable identifier,
244+
NSError *_Nullable error) {
240245
RCNConfigFetch *strongSelf = weakSelf;
241246

242247
// Dispatch to the RC serial queue to update settings on the queue.
243248
dispatch_async(strongSelf->_lockQueue, ^{
244249
RCNConfigFetch *strongSelfQueue = weakSelf;
245250

246251
// Update config settings with the IID and token.
247-
strongSelfQueue->_settings.configInstanceIDToken = [token copy];
248-
strongSelfQueue->_settings.configInstanceID = identity;
252+
strongSelfQueue->_settings.configInstallationsToken = tokenResult.authToken;
253+
strongSelfQueue->_settings.configInstallationsIdentifier = identifier;
249254

250-
if (!identity || error) {
255+
if (!identifier || error) {
251256
NSString *errorDescription =
252257
[NSString stringWithFormat:@"Error getting iid : %@.", error];
253258
FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000055", @"%@",
@@ -265,56 +270,23 @@ - (void)refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler:
265270
}
266271

267272
FIRLogInfo(kFIRLoggerRemoteConfig, @"I-RCN000022", @"Success to get iid : %@.",
268-
strongSelfQueue->_settings.configInstanceID);
269-
270-
// Continue the fetch regardless of whether fetch of instance ID succeeded.
271-
[strongSelfQueue fetchCheckinInfoWithCompletionHandler:completionHandler];
273+
strongSelfQueue->_settings.configInstallationsIdentifier);
274+
[strongSelf
275+
getAnalyticsUserPropertiesWithCompletionHandler:^(NSDictionary *userProperties) {
276+
dispatch_async(strongSelf->_lockQueue, ^{
277+
[strongSelf fetchWithUserProperties:userProperties
278+
completionHandler:completionHandler];
279+
});
280+
}];
272281
});
273282
}];
274283
};
275-
FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000039", @"Starting requesting token.");
276-
// Note: We expect the GCMSenderID to always be available by the time this request is made.
277-
[instanceID tokenWithAuthorizedEntity:_options.GCMSenderID
278-
scope:kInstanceIDScopeConfig
279-
options:nil
280-
handler:instanceIDHandler];
281-
}
282284

283-
/// Fetch checkin info before fetching config. Checkin info including device authentication ID,
284-
/// secret token and device data version are optional fields in config request.
285-
- (void)fetchCheckinInfoWithCompletionHandler:(FIRRemoteConfigFetchCompletion)completionHandler {
286-
FIRInstanceID *instanceID = [FIRInstanceID instanceID];
287-
__weak RCNConfigFetch *weakSelf = self;
288-
[instanceID fetchCheckinInfoWithHandler:^(FIRInstanceIDCheckinPreferences *preferences,
289-
NSError *error) {
290-
RCNConfigFetch *fetchCheckinInfoWithHandlerSelf = weakSelf;
291-
dispatch_async(fetchCheckinInfoWithHandlerSelf->_lockQueue, ^{
292-
RCNConfigFetch *strongSelf = fetchCheckinInfoWithHandlerSelf;
293-
if (error) {
294-
FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000023", @"Failed to fetch checkin info: %@.",
295-
error);
296-
} else {
297-
strongSelf->_settings.deviceAuthID = preferences.deviceID;
298-
strongSelf->_settings.secretToken = preferences.secretToken;
299-
strongSelf->_settings.deviceDataVersion = preferences.deviceDataVersion;
300-
if (strongSelf->_settings.deviceAuthID.length && strongSelf->_settings.secretToken.length) {
301-
FIRLogInfo(kFIRLoggerRemoteConfig, @"I-RCN000024",
302-
@"Success to get device authentication ID: %@, security token: %@.",
303-
self->_settings.deviceAuthID, self->_settings.secretToken);
304-
}
305-
}
306-
// Checkin info is optional, continue fetch config regardless fetch of checkin info
307-
// succeeded.
308-
[strongSelf fetchWithUserPropertiesCompletionHandler:^(NSDictionary *userProperties) {
309-
dispatch_async(strongSelf->_lockQueue, ^{
310-
[strongSelf fetchWithUserProperties:userProperties completionHandler:completionHandler];
311-
});
312-
}];
313-
});
314-
}];
285+
FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000039", @"Starting requesting token.");
286+
[installations authTokenWithCompletion:installationsTokenHandler];
315287
}
316288

317-
- (void)fetchWithUserPropertiesCompletionHandler:
289+
- (void)getAnalyticsUserPropertiesWithCompletionHandler:
318290
(FIRAInteropUserPropertiesCallback)completionHandler {
319291
FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000060", @"Fetch with user properties completed.");
320292
id<FIRAnalyticsInterop> analytics = self->_analytics;
@@ -590,6 +562,8 @@ - (NSURLSessionDataTask *)URLSessionDataTaskWithContent:(NSData *)content
590562
timeoutInterval:timeoutInterval];
591563
URLRequest.HTTPMethod = kHTTPMethodPost;
592564
[URLRequest setValue:kContentTypeValueJSON forHTTPHeaderField:kContentTypeHeaderName];
565+
[URLRequest setValue:_settings.configInstallationsToken
566+
forHTTPHeaderField:kInstallationsAuthTokenHeaderName];
593567
[URLRequest setValue:[[NSBundle mainBundle] bundleIdentifier]
594568
forHTTPHeaderField:kiOSBundleIdentifierHeaderName];
595569
[URLRequest setValue:@"gzip" forHTTPHeaderField:kContentEncodingHeaderName];

FirebaseRemoteConfig/Tests/Sample/Podfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ target 'RemoteConfigSampleApp' do
77
pod 'FirebaseAnalytics'
88
pod 'FirebaseCore', :path => '../../../'
99
pod 'FirebaseInstallations', :path => '../../../'
10-
pod 'FirebaseInstanceID', :path => '../../../'
1110
pod 'FirebaseRemoteConfig', :path => '../../../'
1211

1312
# Pods for RemoteConfigSampleApp

FirebaseRemoteConfig/Tests/Sample/RemoteConfigSampleApp/ViewController.m

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#import <FirebaseAnalytics/FirebaseAnalytics.h>
1818
#import <FirebaseCore/FIROptions.h>
1919
#import <FirebaseCore/FirebaseCore.h>
20-
#import <FirebaseInstanceID/FIRInstanceID+Private.h>
20+
#import <FirebaseInstallations/FirebaseInstallations.h>
2121
#import <FirebaseRemoteConfig/FIRRemoteConfig_Private.h>
2222
#import <FirebaseRemoteConfig/FirebaseRemoteConfig.h>
2323
#import "FRCLog.h"
@@ -355,21 +355,19 @@ - (void)printResult:(NSMutableString *)output {
355355
stringWithFormat:@"%@\n",
356356
[self statusString:currentRCInstance.lastFetchStatus]]];
357357

358-
FIRInstanceID *instanceID = [FIRInstanceID instanceID];
359-
[instanceID getIDWithHandler:^(NSString *_Nullable identity, NSError *_Nullable error) {
360-
[output appendString:@"\n-----------Instance ID------------------\n"];
361-
[output appendString:[NSString stringWithFormat:@"%@\n", identity]];
358+
FIRInstallations *installations = [FIRInstallations installations];
359+
[installations installationIDWithCompletion:^(NSString *_Nullable identifier,
360+
NSError *_Nullable error) {
361+
[output appendString:@"\n-----------Installation ID------------------\n"];
362+
[output appendString:[NSString stringWithFormat:@"%@\n", identifier]];
362363

363-
[output appendString:@"\n-----------Instance ID token------------\n"];
364-
[output
365-
appendString:[NSString stringWithFormat:@"%@\n",
366-
currentRCInstance.settings.configInstanceIDToken]];
364+
[output appendString:@"\n-----------Installation ID token------------\n"];
367365

368-
[output appendString:@"\n-----------Android ID------------\n"];
369-
[output
370-
appendString:[NSString stringWithFormat:@"%@\n", currentRCInstance.settings.deviceAuthID]];
371-
372-
[[FRCLog sharedInstance] logToConsole:output];
366+
[installations authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
367+
NSError *_Nullable error) {
368+
[output appendString:[NSString stringWithFormat:@"%@\n", tokenResult.authToken]];
369+
[[FRCLog sharedInstance] logToConsole:output];
370+
}];
373371
}];
374372
}
375373

@@ -409,27 +407,28 @@ - (NSString *)errorString:(FIRRemoteConfigError)error {
409407
}
410408

411409
- (IBAction)fetchIIDButtonClicked:(id)sender {
412-
FIRInstanceID *instanceID = [FIRInstanceID instanceID];
413-
FIRInstanceIDTokenHandler instanceIDHandler = ^(NSString *token, NSError *error) {
410+
FIRInstallations *installations =
411+
[FIRInstallations installationsWithApp:[FIRApp appNamed:self.FIRAppName]];
412+
[installations installationIDWithCompletion:^(NSString *_Nullable identifier,
413+
NSError *_Nullable error) {
414414
if (error) {
415415
[[FRCLog sharedInstance] logToConsole:[NSString stringWithFormat:@"%@", error]];
416-
}
417-
if (token) {
418-
((FIRRemoteConfig *)self.RCInstances[self.currentNamespace][self.FIRAppName])
419-
.settings.configInstanceIDToken = token;
420-
[instanceID getIDWithHandler:^(NSString *_Nullable identity, NSError *_Nullable error) {
421-
[[FRCLog sharedInstance]
422-
logToConsole:[NSString
423-
stringWithFormat:
424-
@"Successfully getting InstanceID : \n\n%@\n\nToken : \n\n%@\n",
425-
identity, token]];
416+
} else {
417+
[installations authTokenWithCompletion:^(
418+
FIRInstallationsAuthTokenResult *_Nullable tokenResult,
419+
NSError *_Nullable error) {
420+
if (tokenResult.authToken) {
421+
((FIRRemoteConfig *)self.RCInstances[self.currentNamespace][self.FIRAppName])
422+
.settings.configInstallationsToken = tokenResult.authToken;
423+
[[FRCLog sharedInstance]
424+
logToConsole:[NSString
425+
stringWithFormat:
426+
@"Successfully got installation ID : \n\n%@\n\nToken : \n\n%@\n",
427+
identifier, tokenResult.authToken]];
428+
}
426429
}];
427430
}
428-
};
429-
[instanceID tokenWithAuthorizedEntity:[FIRApp appNamed:self.FIRAppName].options.GCMSenderID
430-
scope:@"*"
431-
options:nil
432-
handler:instanceIDHandler];
431+
}];
433432
}
434433

435434
- (IBAction)searchButtonClicked:(id)sender {

0 commit comments

Comments
 (0)