@@ -105,9 +105,6 @@ + (DelayedConsentInitializationParameters *)delayedInitParameters {
105105// Called after successfully calling setAppId and setLaunchOptions
106106static BOOL initDone = false ;
107107
108- // Used to track last time SDK was initialized, for whether or not to start a new session
109- static NSTimeInterval initializationTime;
110-
111108// // Set when the app is launched
112109// static NSDate *sessionLaunchTime;
113110
@@ -366,18 +363,7 @@ + (BOOL)shouldStartNewSession {
366363 return false ;
367364 }
368365
369- NSTimeInterval now = [[NSDate date ] timeIntervalSince1970 ];
370- NSTimeInterval lastTimeClosed = [OneSignalUserDefaults.initStandard getSavedDoubleForKey: OSUD_APP_LAST_CLOSED_TIME defaultValue: 0 ];
371-
372- // Make sure last time we closed app was more than 30 secs ago
373- const int minTimeThreshold = 30 ;
374- NSTimeInterval timeSinceLastClosed = now - lastTimeClosed;
375- NSTimeInterval timeSinceInitialization = now - initializationTime;
376-
377- [OneSignalLog onesignalLog: ONE_S_LL_DEBUG message: [NSString stringWithFormat: @" shouldStartNewSession:timeSinceLastClosed: %f " , timeSinceLastClosed]];
378- [OneSignalLog onesignalLog: ONE_S_LL_DEBUG message: [NSString stringWithFormat: @" shouldStartNewSession:timeSinceInitialization: %f " , timeSinceInitialization]];
379-
380- return MIN (timeSinceLastClosed, timeSinceInitialization) >= minTimeThreshold;
366+ return [OSSessionStartGate.shared shouldStartNewSession ];
381367}
382368
383369+ (void )startNewSession : (BOOL )fromInit {
@@ -391,17 +377,28 @@ + (void)startNewSession:(BOOL)fromInit {
391377 // Run on the main queue as it is possible for this to be called from multiple queues.
392378 // Also some of the code in the method is not thread safe such as _outcomeEventsController.
393379 [OneSignalHelper dispatch_async_on_main_queue: ^{
394- [self startNewSessionInternal ];
380+ [self startNewSessionInternal: !fromInit];
381+ }];
382+ }
383+
384+ // / Starts the session init deferred during a prewarm, without the 30 second gate: the init start
385+ // / it replaces never ran.
386+ + (void )startDeferredSession {
387+ [OneSignalHelper dispatch_async_on_main_queue: ^{
388+ [self startNewSessionInternal: YES ];
395389 }];
396390}
397391
398- + (void )startNewSessionInternal {
392+ + (void )startNewSessionInternal : ( BOOL ) deduplicate {
399393 [OneSignalLog onesignalLog: ONE_S_LL_VERBOSE message: @" OneSignal.startNewSessionInternal" ];
400394
401395 // return if the user has not granted consent, or device-protected storage isn't readable yet
402396 if ([OneSignalConfig shouldAwaitAppIdAndLogMissingPrivacyConsentForMethod: nil ])
403397 return ;
404398
399+ if (![OSSessionStartGate.shared beginSessionStartWithDeduplicate: deduplicate])
400+ return ;
401+
405402 // A new session is a fresh chance at params after the launch attempt exhausted its retries
406403 if (!_downloadedParameters && !_didCallDownloadParameters && OneSignalIdentifiers.currentAppId ) {
407404 _downloadParametersAttempts = 0 ;
@@ -621,7 +618,7 @@ + (void)setupProtectedDataObserverOnce {
621618 [OneSignal startInAppMessages ];
622619 StartFeatureFlagsRefresh ();
623620 [OSDeviceGestureDetector start ];
624- [OneSignal startNewSession: YES ];
621+ [OneSignal startDeferredSession ];
625622 };
626623
627624 // Authoritative signal: the OS says protected data just became available.
@@ -726,7 +723,7 @@ + (void)init {
726723 }
727724 [self startNewSession: YES ];
728725
729- initializationTime = [[ NSDate date ] timeIntervalSince1970 ];
726+ [OSSessionStartGate.shared recordInitialization ];
730727 initDone = true ;
731728}
732729
0 commit comments