|
|
@@ -12,21 +12,21 @@
|
|
|
// See the License for the specific language governing permissions and
|
|
|
// limitations under the License.
|
|
|
|
|
|
-#import "SEGContentManager.h"
|
|
|
+#import "FirebaseSegmentation/Sources/SEGContentManager.h"
|
|
|
|
|
|
-#import <FirebaseCore/FIRAppInternal.h>
|
|
|
-#import <FirebaseInstanceID/FIRInstanceID.h>
|
|
|
-#import "FIRSegmentation.h"
|
|
|
-#import "SEGDatabaseManager.h"
|
|
|
-#import "SEGNetworkManager.h"
|
|
|
-#import "SEGSegmentationConstants.h"
|
|
|
+#import "FirebaseCore/Sources/Private/FIRAppInternal.h"
|
|
|
+#import "FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FirebaseInstallations.h"
|
|
|
+#import "FirebaseSegmentation/Sources/Public/FIRSegmentation.h"
|
|
|
+#import "FirebaseSegmentation/Sources/SEGDatabaseManager.h"
|
|
|
+#import "FirebaseSegmentation/Sources/SEGNetworkManager.h"
|
|
|
+#import "FirebaseSegmentation/Sources/SEGSegmentationConstants.h"
|
|
|
|
|
|
NSString *const kErrorDescription = @"ErrorDescription";
|
|
|
|
|
|
@interface SEGContentManager () {
|
|
|
NSMutableDictionary<NSString *, id> *_associationData;
|
|
|
- NSString *_instanceIdentifier;
|
|
|
- NSString *_instanceIdentifierToken;
|
|
|
+ NSString *_installationIdentifier;
|
|
|
+ NSString *_installationIdentifierToken;
|
|
|
SEGDatabaseManager *_databaseManager;
|
|
|
SEGNetworkManager *_networkManager;
|
|
|
}
|
|
|
@@ -63,69 +63,111 @@ NSString *const kErrorDescription = @"ErrorDescription";
|
|
|
return self;
|
|
|
}
|
|
|
|
|
|
-// TODO(dmandar) IID only supports default instance. Modify for FIS.
|
|
|
-- (FIRInstanceID *)instanceIDForApp:(NSString *)firebaseApp {
|
|
|
- return [FIRInstanceID instanceID];
|
|
|
+- (FIRInstallations *)installationForApp:(NSString *)firebaseApp {
|
|
|
+ return [FIRInstallations installationsWithApp:[FIRApp appNamed:firebaseApp]];
|
|
|
}
|
|
|
|
|
|
- (void)associateCustomInstallationIdentiferNamed:(NSString *)customInstallationID
|
|
|
firebaseApp:(NSString *)firebaseApp
|
|
|
completion:(SEGRequestCompletion)completionHandler {
|
|
|
- // Get the latest instance identifier
|
|
|
- if (![self instanceIDForApp:firebaseApp]) {
|
|
|
- completionHandler(NO, @{kErrorDescription : @"InstanceID SDK not available"});
|
|
|
+ // Get the latest installation identifier
|
|
|
+ FIRInstallations *installation = [self installationForApp:firebaseApp];
|
|
|
+ if (installation == nil) {
|
|
|
+ completionHandler(NO, @{kErrorDescription : @"Firebase Installations SDK not available"});
|
|
|
}
|
|
|
__weak SEGContentManager *weakSelf = self;
|
|
|
- [[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result,
|
|
|
- NSError *_Nullable error) {
|
|
|
+ [installation
|
|
|
+ installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
|
|
|
+ SEGContentManager *strongSelf = weakSelf;
|
|
|
+ if (!strongSelf) {
|
|
|
+ completionHandler(NO, @{kErrorDescription : @"Internal Error getting installation ID."});
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ [strongSelf associateInstallationWithLatestIdentifier:identifier
|
|
|
+ installation:installation
|
|
|
+ customizedIdentifier:customInstallationID
|
|
|
+ firebaseApp:firebaseApp
|
|
|
+ error:error
|
|
|
+ completion:completionHandler];
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)associateInstallationWithLatestIdentifier:(NSString *_Nullable)identifier
|
|
|
+ installation:(FIRInstallations *)installation
|
|
|
+ customizedIdentifier:(NSString *)customInstallationID
|
|
|
+ firebaseApp:(NSString *)firebaseApp
|
|
|
+ error:(NSError *_Nullable)error
|
|
|
+ completion:(SEGRequestCompletion)completionHandler {
|
|
|
+ if (!identifier || error) {
|
|
|
+ NSString *errorMessage = @"Error getting installation ID.";
|
|
|
+ if (error) {
|
|
|
+ errorMessage = [errorMessage stringByAppendingString:error.description];
|
|
|
+ }
|
|
|
+ NSDictionary *errorDictionary = @{kErrorDescription : errorMessage};
|
|
|
+ completionHandler(NO, errorDictionary);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ _installationIdentifier = identifier;
|
|
|
+
|
|
|
+ __weak SEGContentManager *weakSelf = self;
|
|
|
+ [installation authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
|
|
|
+ NSError *_Nullable error) {
|
|
|
SEGContentManager *strongSelf = weakSelf;
|
|
|
if (!strongSelf) {
|
|
|
- completionHandler(NO, @{kErrorDescription : @"Internal Error getting instance ID."});
|
|
|
+ completionHandler(NO, @{kErrorDescription : @"Internal Error getting installation token."});
|
|
|
return;
|
|
|
}
|
|
|
+ [strongSelf associateInstallationWithToken:tokenResult
|
|
|
+ customizedIdentifier:customInstallationID
|
|
|
+ firebaseApp:firebaseApp
|
|
|
+ error:error
|
|
|
+ completion:completionHandler];
|
|
|
+ }];
|
|
|
+}
|
|
|
|
|
|
- if (!result || error) {
|
|
|
- NSString *errorMessage = @"Error getting instance ID.";
|
|
|
- if (error) {
|
|
|
- errorMessage = [errorMessage stringByAppendingString:error.description];
|
|
|
- }
|
|
|
- NSDictionary *errorDictionary = @{kErrorDescription : errorMessage};
|
|
|
- completionHandler(NO, errorDictionary);
|
|
|
- return;
|
|
|
+- (void)associateInstallationWithToken:(FIRInstallationsAuthTokenResult *_Nullable)tokenResult
|
|
|
+ customizedIdentifier:(NSString *)customInstallationID
|
|
|
+ firebaseApp:(NSString *)firebaseApp
|
|
|
+ error:(NSError *_Nullable)error
|
|
|
+ completion:(SEGRequestCompletion)completionHandler {
|
|
|
+ if (!tokenResult || error) {
|
|
|
+ NSString *errorMessage = @"Error getting AuthToken.";
|
|
|
+ if (error) {
|
|
|
+ errorMessage = [errorMessage stringByAppendingString:error.description];
|
|
|
}
|
|
|
-
|
|
|
- strongSelf->_instanceIdentifier = result.instanceID;
|
|
|
- strongSelf->_instanceIdentifierToken = result.token;
|
|
|
-
|
|
|
- NSMutableDictionary<NSString *, NSString *> *appAssociationData =
|
|
|
- [[NSMutableDictionary alloc] init];
|
|
|
- [appAssociationData setObject:customInstallationID forKey:kSEGCustomInstallationIdentifierKey];
|
|
|
- [appAssociationData setObject:self->_instanceIdentifier
|
|
|
- forKey:kSEGFirebaseInstallationIdentifierKey];
|
|
|
- [appAssociationData setObject:kSEGAssociationStatusPending forKey:kSEGAssociationStatusKey];
|
|
|
- [strongSelf->_associationData setObject:appAssociationData forKey:firebaseApp];
|
|
|
-
|
|
|
- // Update the database async.
|
|
|
- // TODO(mandard) The database write and corresponding completion handler needs to be wired up
|
|
|
- // once we support listening to FID changes.
|
|
|
- [strongSelf->_databaseManager insertMainTableApplicationNamed:firebaseApp
|
|
|
- customInstanceIdentifier:customInstallationID
|
|
|
- firebaseInstanceIdentifier:strongSelf->_instanceIdentifier
|
|
|
- associationStatus:kSEGAssociationStatusPending
|
|
|
- completionHandler:nil];
|
|
|
-
|
|
|
- // Send the change up to the backend. Also add the token.
|
|
|
-
|
|
|
- [strongSelf->_networkManager
|
|
|
- makeAssociationRequestToBackendWithData:appAssociationData
|
|
|
- token:strongSelf->_instanceIdentifierToken
|
|
|
- completion:^(BOOL status,
|
|
|
- NSDictionary<NSString *, id> *result) {
|
|
|
- // TODO...log, update database.
|
|
|
-
|
|
|
- completionHandler(status, result);
|
|
|
- }];
|
|
|
- }];
|
|
|
+ NSDictionary *errorDictionary = @{kErrorDescription : errorMessage};
|
|
|
+ completionHandler(NO, errorDictionary);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ _installationIdentifierToken = tokenResult.authToken;
|
|
|
+
|
|
|
+ NSMutableDictionary<NSString *, NSString *> *appAssociationData =
|
|
|
+ [[NSMutableDictionary alloc] init];
|
|
|
+ appAssociationData[kSEGCustomInstallationIdentifierKey] = customInstallationID;
|
|
|
+ appAssociationData[kSEGFirebaseInstallationIdentifierKey] = _installationIdentifier;
|
|
|
+ appAssociationData[kSEGAssociationStatusKey] = kSEGAssociationStatusPending;
|
|
|
+ _associationData[firebaseApp] = appAssociationData;
|
|
|
+
|
|
|
+ // Update the database async.
|
|
|
+ // TODO(mandard) The database write and corresponding completion handler needs to be wired up
|
|
|
+ // once we support listening to FID changes.
|
|
|
+ [_databaseManager insertMainTableApplicationNamed:firebaseApp
|
|
|
+ customInstanceIdentifier:customInstallationID
|
|
|
+ firebaseInstanceIdentifier:_installationIdentifier
|
|
|
+ associationStatus:kSEGAssociationStatusPending
|
|
|
+ completionHandler:nil];
|
|
|
+
|
|
|
+ // Send the change up to the backend. Also add the token.
|
|
|
+ [_networkManager
|
|
|
+ makeAssociationRequestToBackendWithData:appAssociationData
|
|
|
+ token:_installationIdentifierToken
|
|
|
+ completion:^(BOOL status, NSDictionary<NSString *, id> *result) {
|
|
|
+ // TODO...log, update database.
|
|
|
+
|
|
|
+ completionHandler(status, result);
|
|
|
+ }];
|
|
|
}
|
|
|
|
|
|
@end
|