Skip to content

Commit 0572813

Browse files
committed
Clear badges when entering foreground NOT when didBecomeActive
The application active state is different than the application foreground state. The app resigns and becomes active if the app is interrupted by the notification center even though it remains in the foreground. We previously were dismissing notifications in didBecomeActive so when the notification center was opened while the app was foregrounded we would dismiss notifications. There also seems to be an apple bug where the resign and become active triggers would fire twice as you are swiping down the notification center. This means that we were dismissing the notifications as you open the notification center which made the problem even worse.
1 parent f0552dd commit 0572813

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

iOS_SDK/OneSignalSDK/Source/OneSignalLifecycleObserver.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ + (void)registerLifecycleObserverAsUIScene {
6868
+ (void)registerLifecycleObserverAsUIApplication {
6969
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"registering for Application Lifecycle notifications"];
7070
[[NSNotificationCenter defaultCenter] addObserver:[OneSignalLifecycleObserver sharedInstance] selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
71+
[[NSNotificationCenter defaultCenter] addObserver:[OneSignalLifecycleObserver sharedInstance] selector:@selector(willEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
7172
[[NSNotificationCenter defaultCenter] addObserver:[OneSignalLifecycleObserver sharedInstance] selector:@selector(didBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
7273
[[NSNotificationCenter defaultCenter] addObserver:[OneSignalLifecycleObserver sharedInstance] selector:@selector(willResignActive) name:UIApplicationWillResignActiveNotification object:nil];
7374
}
@@ -112,6 +113,11 @@ - (void)didEnterBackground {
112113
}
113114
}
114115

116+
- (void)willEnterForeground {
117+
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"application/scene willEnterForeground"];
118+
[OneSignalTracker applicationWillEnterForeground];
119+
}
120+
115121
- (void)dealloc {
116122
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"lifecycle observer deallocated"];
117123
[[NSNotificationCenter defaultCenter] removeObserver:self];

iOS_SDK/OneSignalSDK/Source/OneSignalTracker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131

3232
+ (void)onFocus:(BOOL)toBackground;
3333
+ (void)onSessionEnded:(NSArray<OSInfluence *> *) lastInfluences;
34+
+ (void)applicationWillEnterForeground;
3435

3536
@end

iOS_SDK/OneSignalSDK/Source/OneSignalTracker.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,20 @@ + (void)onFocus:(BOOL)toBackground {
7777
if (toBackground) {
7878
[self applicationBackgrounded];
7979
} else {
80-
[self applicationForegrounded];
80+
[self applicationBecameActive];
8181
}
8282
}
8383

84-
+ (void)applicationForegrounded {
84+
// This is a separate lifecycle event than application became active
85+
// Notably this is NOT called when the app resumes after resigning active
86+
// From things like entering and exiting the notification center
87+
+ (void)applicationWillEnterForeground {
8588
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"Application Foregrounded started"];
89+
[OSNotificationsManager clearBadgeCount:false fromClearAll:false];
90+
}
91+
92+
+ (void)applicationBecameActive {
93+
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"Application Active started"];
8694
[OSFocusTimeProcessorFactory cancelFocusCall];
8795

8896
if (OSSessionManager.sharedSessionManager.appEntryState != NOTIFICATION_CLICK)
@@ -100,8 +108,6 @@ + (void)applicationForegrounded {
100108
// TODO: Here it used to call receivedInAppMessageJson with nil, this method no longer exists
101109
// [OneSignal receivedInAppMessageJson:nil];
102110
}
103-
104-
[OSNotificationsManager clearBadgeCount:false fromClearAll:false];
105111
}
106112

107113
+ (void)applicationBackgrounded {

0 commit comments

Comments
 (0)