Skip to content

Commit 398a871

Browse files
authored
[Core] Support watchOS lifecycle notifications (#10112)
* [Core] Support watchOS lifecycle notifications * Rework for so version is checked in runtime
1 parent fe093a7 commit 398a871

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

FirebaseCore/Sources/FIRApp.m

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#import <AppKit/AppKit.h>
2323
#endif
2424

25+
#if __has_include(<WatchKit/WatchKit.h>)
26+
#import <WatchKit/WatchKit.h>
27+
#endif
28+
2529
#import "FirebaseCore/Sources/Public/FirebaseCore/FIRApp.h"
2630

2731
#import "FirebaseCore/Sources/FIRAnalyticsConfiguration.h"
@@ -871,14 +875,23 @@ - (void)subscribeForAppDidBecomeActiveNotifications {
871875
NSNotificationName notificationName = UIApplicationDidBecomeActiveNotification;
872876
#elif TARGET_OS_OSX
873877
NSNotificationName notificationName = NSApplicationDidBecomeActiveNotification;
878+
#elif TARGET_OS_WATCH
879+
// TODO(ncooke3): Remove when minimum supported watchOS version is watchOS 7.0.
880+
// On watchOS 7.0+, heartbeats are logged when the watch app becomes active.
881+
// On watchOS 6.0, heartbeats are logged when the Firebase app is configuring.
882+
// While it does not cover all use cases, logging when the Firebase app is
883+
// configuring is done because watchOS lifecycle notifications are a
884+
// watchOS 7.0+ feature.
885+
NSNotificationName notificationName = kFIRAppReadyToConfigureSDKNotification;
886+
if (@available(watchOS 7.0, *)) {
887+
notificationName = WKApplicationDidBecomeActiveNotification;
888+
}
874889
#endif
875890

876-
#if !TARGET_OS_WATCH
877891
[[NSNotificationCenter defaultCenter] addObserver:self
878892
selector:@selector(appDidBecomeActive:)
879893
name:notificationName
880894
object:nil];
881-
#endif
882895
}
883896

884897
- (void)appDidBecomeActive:(NSNotification *)notification {

FirebaseCore/Tests/Unit/FIRAppTest.m

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#if __has_include(<UIKit/UIKit.h>)
16+
#import <UIKit/UIKit.h>
17+
#endif
18+
19+
#if __has_include(<AppKit/AppKit.h>)
20+
#import <AppKit/AppKit.h>
21+
#endif
22+
23+
#if __has_include(<WatchKit/WatchKit.h>)
24+
#import <WatchKit/WatchKit.h>
25+
#endif
26+
1527
#import "FirebaseCore/Tests/Unit/FIRTestCase.h"
1628
#import "FirebaseCore/Tests/Unit/FIRTestComponents.h"
1729

@@ -827,7 +839,6 @@ - (void)testIsDefaultAppConfigured {
827839

828840
#pragma mark - Core Telemetry
829841

830-
#if !TARGET_OS_WATCH
831842
- (void)testCoreDiagnosticsLoggedWhenAppDidBecomeActive {
832843
FIRApp *app = [self createConfiguredAppWithName:NSStringFromSelector(_cmd)];
833844
[self expectCoreDiagnosticsDataLogWithOptions:app.options];
@@ -845,7 +856,6 @@ - (void)testHeartbeatLogIsAttemptedWhenAppDidBecomeActive {
845856
object:nil];
846857
OCMVerifyAll(self.mockHeartbeatLogger);
847858
}
848-
#endif // TARGET_OS_WATCH
849859

850860
#pragma mark - private
851861

@@ -890,10 +900,15 @@ - (void)expectCoreDiagnosticsDataLogWithOptions:(nullable FIROptions *)expectedO
890900
- (NSNotificationName)appDidBecomeActiveNotificationName {
891901
#if TARGET_OS_IOS || TARGET_OS_TV
892902
return UIApplicationDidBecomeActiveNotification;
893-
#endif
894-
895-
#if TARGET_OS_OSX
903+
#elif TARGET_OS_OSX
896904
return NSApplicationDidBecomeActiveNotification;
905+
#elif TARGET_OS_WATCH
906+
// See comment in `- [FIRApp subscribeForAppDidBecomeActiveNotifications]`.
907+
if (@available(watchOS 7.0, *)) {
908+
return WKApplicationDidBecomeActiveNotification;
909+
} else {
910+
return kFIRAppReadyToConfigureSDKNotification;
911+
}
897912
#endif
898913
}
899914

0 commit comments

Comments
 (0)