diff --git a/Example/Core/Tests/FIRAppTest.m b/Example/Core/Tests/FIRAppTest.m index 13bc9210817..7b8b29dfb9f 100644 --- a/Example/Core/Tests/FIRAppTest.m +++ b/Example/Core/Tests/FIRAppTest.m @@ -690,8 +690,6 @@ - (void)testGlobalDataCollectionClearedAfterDelete { } - (void)testGlobalDataCollectionNoDiagnosticsSent { - [FIRApp configure]; - // Add an observer for the diagnostics notification - both with and without an object to ensure it // catches it either way. Currently no object is sent, but in the future that could change. [self.notificationCenter addMockObserver:self.observerMock @@ -707,6 +705,9 @@ - (void)testGlobalDataCollectionNoDiagnosticsSent { OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY]) .andReturn(@NO); + // Ensure configure doesn't fire a notification. + [FIRApp configure]; + NSError *error = [NSError errorWithDomain:@"com.firebase" code:42 userInfo:nil]; [[FIRApp defaultApp] sendLogsWithServiceName:@"Service" version:@"Version" error:error]; diff --git a/Firebase/Core/FIRApp.m b/Firebase/Core/FIRApp.m index 2edcb078963..2920d4c737d 100644 --- a/Firebase/Core/FIRApp.m +++ b/Firebase/Core/FIRApp.m @@ -107,13 +107,19 @@ @implementation FIRApp + (void)configure { FIROptions *options = [FIROptions defaultOptions]; if (!options) { - [[NSNotificationCenter defaultCenter] - postNotificationName:kFIRAppDiagnosticsNotification - object:nil - userInfo:@{ - kFIRAppDiagnosticsConfigurationTypeKey : @(FIRConfigTypeCore), - kFIRAppDiagnosticsErrorKey : [FIRApp errorForMissingOptions] - }]; + // Read the Info.plist to see if the flag is set. At this point we can't check any user defaults + // since the app isn't configured at all, so only rely on the Info.plist value. + NSNumber *collectionEnabledPlistValue = [[self class] readDataCollectionSwitchFromPlist]; + if (!collectionEnabledPlistValue || [collectionEnabledPlistValue boolValue]) { + [[NSNotificationCenter defaultCenter] + postNotificationName:kFIRAppDiagnosticsNotification + object:nil + userInfo:@{ + kFIRAppDiagnosticsConfigurationTypeKey : @(FIRConfigTypeCore), + kFIRAppDiagnosticsErrorKey : [FIRApp errorForMissingOptions] + }]; + } + [NSException raise:kFirebaseCoreErrorDomain format: @"`[FIRApp configure];` (`FirebaseApp.configure()` in Swift) could not find " @@ -274,13 +280,6 @@ + (void)addAppToAppDictionary:(FIRApp *)app { } if ([app configureCore]) { sAllApps[app.name] = app; - [[NSNotificationCenter defaultCenter] - postNotificationName:kFIRAppDiagnosticsNotification - object:nil - userInfo:@{ - kFIRAppDiagnosticsConfigurationTypeKey : @(FIRConfigTypeCore), - kFIRAppDiagnosticsFIRAppKey : app - }]; } else { [NSException raise:kFirebaseCoreErrorDomain format: @@ -317,7 +316,7 @@ - (void)getTokenForcingRefresh:(BOOL)forceRefresh withCallback:(FIRTokenCallback - (BOOL)configureCore { [self checkExpectedBundleID]; if (![self isAppIDValid]) { - if (_options.usingOptionsFromDefaultPlist) { + if (_options.usingOptionsFromDefaultPlist && [self isDataCollectionDefaultEnabled]) { [[NSNotificationCenter defaultCenter] postNotificationName:kFIRAppDiagnosticsNotification object:nil @@ -329,6 +328,16 @@ - (BOOL)configureCore { return NO; } + if ([self isDataCollectionDefaultEnabled]) { + [[NSNotificationCenter defaultCenter] + postNotificationName:kFIRAppDiagnosticsNotification + object:nil + userInfo:@{ + kFIRAppDiagnosticsConfigurationTypeKey : @(FIRConfigTypeCore), + kFIRAppDiagnosticsFIRAppKey : self + }]; + } + #if TARGET_OS_IOS // Initialize the Analytics once there is a valid options under default app. Analytics should // always initialize first by itself before the other SDKs.