@@ -36,7 +36,7 @@ public class IterableApi {
3636 private String _apiKey ;
3737 private String _email ;
3838 private String _userId ;
39- private String _userIdAnon ;
39+ String _userIdAnon ;
4040 private String _authToken ;
4141 private boolean _debugMode ;
4242 private Bundle _payloadData ;
@@ -47,15 +47,15 @@ public class IterableApi {
4747 private IterableHelper .FailureHandler _setUserFailureCallbackHandler ;
4848
4949 IterableApiClient apiClient = new IterableApiClient (new IterableApiAuthProvider ());
50- private static final AnonymousUserManager anonymousUserManager = new AnonymousUserManager ();
5150 private static final AnonymousUserMerge anonymousUserMerge = new AnonymousUserMerge ();
51+ private @ Nullable AnonymousUserManager anonymousUserManager ;
5252 private @ Nullable IterableInAppManager inAppManager ;
5353 private @ Nullable IterableEmbeddedManager embeddedManager ;
5454 private String inboxSessionId ;
5555 private IterableAuthManager authManager ;
5656 private HashMap <String , String > deviceAttributes = new HashMap <>();
5757 private IterableKeychain keychain ;
58- private long lastCriteriaFetch = 0 ;
58+ long lastCriteriaFetch = 0 ;
5959
6060 void fetchRemoteConfiguration () {
6161 apiClient .getRemoteConfiguration (new IterableHelper .IterableActionHandler () {
@@ -436,28 +436,13 @@ private void onForeground() {
436436 editor .putBoolean (IterableConstants .SHARED_PREFS_DEVICE_NOTIFICATIONS_ENABLED , systemNotificationEnabled );
437437 editor .apply ();
438438 }
439-
440- long currentTime = System .currentTimeMillis ();
441-
442- // fetching anonymous user criteria on foregrounding
443- if (!sharedInstance .checkSDKInitialization ()
444- && sharedInstance ._userIdAnon == null
445- && sharedInstance .config .enableAnonActivation
446- && sharedInstance .getVisitorUsageTracked ()
447- && sharedInstance .config .foregroundCriteriaFetch
448- && currentTime - lastCriteriaFetch >= IterableConstants .CRITERIA_FETCHING_COOLDOWN ) {
449-
450- lastCriteriaFetch = currentTime ;
451- anonymousUserManager .getCriteria ();
452- IterableLogger .d (TAG , "Fetching anonymous user criteria - Foreground" );
453- }
454439 }
455440
456441 private boolean isInitialized () {
457442 return _apiKey != null && (_email != null || _userId != null );
458443 }
459444
460- private boolean checkSDKInitialization () {
445+ boolean checkSDKInitialization () {
461446 if (!isInitialized ()) {
462447 IterableLogger .w (TAG , "Iterable SDK must be initialized with an API key and user email/userId before calling SDK methods" );
463448 return false ;
@@ -682,12 +667,21 @@ public static void initialize(@NonNull Context context, @NonNull String apiKey,
682667 );
683668 }
684669
670+ if (sharedInstance .anonymousUserManager == null ) {
671+ sharedInstance .anonymousUserManager = new AnonymousUserManager (
672+ sharedInstance
673+ );
674+ }
675+
685676 loadLastSavedConfiguration (context );
686677 IterablePushNotificationUtil .processPendingAction (context );
687678
688- if (!sharedInstance .checkSDKInitialization () && sharedInstance ._userIdAnon == null && sharedInstance .config .enableAnonActivation && sharedInstance .getVisitorUsageTracked ()) {
689- anonymousUserManager .updateAnonSession ();
690- anonymousUserManager .getCriteria ();
679+ if (!sharedInstance .checkSDKInitialization ()
680+ && sharedInstance ._userIdAnon == null
681+ && sharedInstance .config .enableAnonActivation
682+ && sharedInstance .getVisitorUsageTracked ()) {
683+ sharedInstance .anonymousUserManager .updateAnonSession ();
684+ sharedInstance .anonymousUserManager .getCriteria ();
691685 }
692686
693687 if (DeviceInfoUtils .isFireTV (context .getPackageManager ())) {
@@ -1599,7 +1593,10 @@ public void setVisitorUsageTracked(@NonNull Boolean isSetVisitorUsageTracked) {
15991593 editor .putBoolean (IterableConstants .SHARED_PREFS_VISITOR_USAGE_TRACKED , isSetVisitorUsageTracked );
16001594 editor .apply ();
16011595
1596+ long currentTime = System .currentTimeMillis ();
1597+
16021598 if (isSetVisitorUsageTracked && config .enableAnonActivation ) {
1599+ lastCriteriaFetch = currentTime ;
16031600 anonymousUserManager .updateAnonSession ();
16041601 anonymousUserManager .getCriteria ();
16051602 }
0 commit comments