Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update backgrounded logic to determine displaying cached IAMs #1233

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions iOS_SDK/OneSignalSDK/Source/OneSignalLifecycleObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ - (void)didBecomeActive {

- (void)willResignActive {
[OneSignal onesignalLog:ONE_S_LL_VERBOSE message:@"application/scene willResignActive"];

[OneSignalTracker willResignActiveTriggered];
if ([OneSignal appId]) {
[OneSignalTracker onFocus:YES];
[OneSignal sendTagsOnBackground];
Expand All @@ -99,7 +99,7 @@ - (void)willResignActive {

- (void)didEnterBackground {
[OneSignal onesignalLog:ONE_S_LL_VERBOSE message:@"application/scene didEnterBackground"];

[OneSignalTracker didEnterBackgroundTriggered];
if ([OneSignal appId])
[OneSignalLocation onFocus:NO];
}
Expand Down
2 changes: 2 additions & 0 deletions iOS_SDK/OneSignalSDK/Source/OneSignalTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@

+ (void)onFocus:(BOOL)toBackground;
+ (void)onSessionEnded:(NSArray<OSInfluence *> *) lastInfluences;
+ (void)willResignActiveTriggered;
+ (void)didEnterBackgroundTriggered;

@end
40 changes: 37 additions & 3 deletions iOS_SDK/OneSignalSDK/Source/OneSignalTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,29 @@ @implementation OneSignalTracker
static UIBackgroundTaskIdentifier focusBackgroundTask;
static NSTimeInterval lastOpenedTime;
static BOOL lastOnFocusWasToBackground = YES;
static BOOL willResignActiveTriggered = NO;
static BOOL didEnterBackgroundTriggered = NO;

+ (void)resetLocals {
[OSFocusTimeProcessorFactory resetUnsentActiveTime];
focusBackgroundTask = 0;
focusBackgroundTask = 0;
lastOpenedTime = 0;
lastOnFocusWasToBackground = YES;
[self resetBackgroundDetection];
}

+ (void)setLastOpenedTime:(NSTimeInterval)lastOpened {
lastOpenedTime = lastOpened;
}

+ (void)willResignActiveTriggered {
willResignActiveTriggered = YES;
}

+ (void)didEnterBackgroundTriggered {
didEnterBackgroundTriggered = YES;
}

+ (void)beginBackgroundFocusTask {
focusBackgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[OneSignalTracker endBackgroundFocusTask];
Expand All @@ -81,10 +92,26 @@ + (void)endBackgroundFocusTask {
focusBackgroundTask = UIBackgroundTaskInvalid;
}

/**
Returns true if application truly did come from a backgrounded state.
Returns false if the application bypassed `didEnterBackground` after entering `willResignActive`.
This can happen if the app resumes after a native dialog displays over the app or after the app is in a suspended state and not backgrounded.
**/
+ (BOOL)applicationForegroundedFromBackgroundedState {
return !(willResignActiveTriggered && !didEnterBackgroundTriggered);
}

+ (void)resetBackgroundDetection {
willResignActiveTriggered = NO;
didEnterBackgroundTriggered = NO;
}

+ (void)onFocus:(BOOL)toBackground {
// return if the user has not granted privacy permissions
if ([OneSignal requiresUserPrivacyConsent])
if ([OneSignal requiresUserPrivacyConsent]) {
[self resetBackgroundDetection];
return;
}

// Prevent the onFocus to be called twice when app being terminated
// - Both WillResignActive and willTerminate
Expand All @@ -101,6 +128,10 @@ + (void)onFocus:(BOOL)toBackground {

+ (void)applicationForegrounded {
[OneSignal onesignalLog:ONE_S_LL_DEBUG message:@"Application Foregrounded started"];

BOOL fromBackgroundedState = [self applicationForegroundedFromBackgroundedState];
[self resetBackgroundDetection];

[OSFocusTimeProcessorFactory cancelFocusCall];

if (OneSignal.appEntryState != NOTIFICATION_CLICK)
Expand All @@ -115,7 +146,10 @@ + (void)applicationForegrounded {
// This checks if notification permissions changed when app was backgrounded
[OneSignal sendNotificationTypesUpdate];
[[OSSessionManager sharedSessionManager] attemptSessionUpgrade:OneSignal.appEntryState];
[OneSignal receivedInAppMessageJson:nil];
if (fromBackgroundedState) {
// Use cached IAMs if app truly went into the background
[OneSignal receivedInAppMessageJson:nil];
}
}

let wasBadgeSet = [OneSignal clearBadgeCount:false];
Expand Down