Skip to content

Commit 30a9957

Browse files
committed
🔧 Track first foreground accordingly
1 parent 04f261c commit 30a9957

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

swift-sdk/Internal/InternalIterableAPI.swift

+18-11
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
484484

485485
private var config: IterableConfig
486486
private var apiEndPoint: String
487+
private var hasHandledFirstForeground = false
487488

488489
/// Following are needed for handling pending notification and deep link.
489490
static var pendingNotificationResponse: NotificationResponseProtocol?
@@ -718,26 +719,32 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
718719
}
719720

720721
@objc private func onAppDidBecomeActiveNotification(notification: Notification) {
721-
// Always update the stored notification state
722-
notificationStateProvider.isNotificationsEnabled { [weak self] isEnabled in
722+
// Track first foreground handling
723+
let isFirstForeground = !hasHandledFirstForeground
724+
hasHandledFirstForeground = true
725+
726+
// Always check notification state
727+
notificationStateProvider.isNotificationsEnabled { [weak self] systemEnabled in
723728
guard let self else { return }
724729

725-
let previousState = self.localStorage.isNotificationsEnabled
726-
727-
// Update stored state
728-
self.localStorage.isNotificationsEnabled = isEnabled
730+
let storedEnabled = self.localStorage.isNotificationsEnabled
729731

730-
// Only attempt registration/deregistration if state changed and auto-registration is enabled
731-
if previousState != isEnabled && self.config.autoPushRegistration {
732-
if isEnabled {
732+
// Handle push registration/deregistration if needed
733+
if self.config.autoPushRegistration && storedEnabled != systemEnabled {
734+
if systemEnabled {
733735
self.notificationStateProvider.registerForRemoteNotifications()
734736
} else {
735737
self.disableDeviceForCurrentUser()
736738
}
737739
}
738740

739-
// Check remote configuration after push registration check
740-
self.checkRemoteConfiguration()
741+
// Only fetch remote configuration on first foreground
742+
if isFirstForeground {
743+
self.checkRemoteConfiguration()
744+
}
745+
746+
// Always update stored state (equivalent to SharedPreferences.Editor.putBoolean)
747+
self.localStorage.isNotificationsEnabled = systemEnabled
741748
}
742749
}
743750

0 commit comments

Comments
 (0)