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

[MOB-9446] Enhance push notification state tracking in SDKs #881

Merged
merged 18 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions swift-sdk/Core/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ enum Const {
static let deviceId = "itbl_device_id"
static let sdkVersion = "itbl_sdk_version"
static let offlineMode = "itbl_offline_mode"
static let isNotificationsEnabled = "itbl_is_Notifications_Enabled"

static let attributionInfoExpiration = 24
}
Expand Down
25 changes: 25 additions & 0 deletions swift-sdk/Internal/InternalIterableAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
onFailure?(reason, data)
}
)
notificationStateProvider.isNotificationsEnabled { isEnabled in
self.localStorage.isNotificationsEnabled = isEnabled
}
}

@discardableResult
Expand Down Expand Up @@ -500,6 +503,8 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
private var _userId: String?
private var _successCallback: OnSuccessHandler? = nil
private var _failureCallback: OnFailureHandler? = nil

private let notificationCenter: NotificationCenterProtocol


/// the hex representation of this device token
Expand Down Expand Up @@ -666,6 +671,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
localStorage = dependencyContainer.localStorage
inAppDisplayer = dependencyContainer.inAppDisplayer
urlOpener = dependencyContainer.urlOpener
notificationCenter = dependencyContainer.notificationCenter
deepLinkManager = DeepLinkManager(redirectNetworkSessionProvider: dependencyContainer)
}

Expand Down Expand Up @@ -698,10 +704,29 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
requestHandler.start()

checkRemoteConfiguration()

addForegroundObservers()

return inAppManager.start()
}

private func addForegroundObservers() {
NotificationCenter.default.addObserver(self,
selector: #selector(onAppDidBecomeActiveNotification(notification:)),
name: UIApplication.didBecomeActiveNotification,
object: nil)
}

@objc private func onAppDidBecomeActiveNotification(notification: Notification) {
self.notificationStateProvider.isNotificationsEnabled { isEnabled in
if self.localStorage.isNotificationsEnabled != isEnabled {
if self.config.autoPushRegistration {
self.notificationStateProvider.registerForRemoteNotifications()
joaodordio marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}

private func handle(launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
guard let launchOptions = launchOptions else {
return
Expand Down
11 changes: 10 additions & 1 deletion swift-sdk/Internal/IterableUserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,20 @@ class IterableUserDefaults {

var offlineMode: Bool {
get {
return bool(withKey: .offlineMode)
bool(withKey: .offlineMode)
} set {
save(bool: newValue, withKey: .offlineMode)
}
}

var isNotificationsEnabled: Bool {
get {
bool(withKey: .isNotificationsEnabled)
} set {
save(bool: newValue, withKey: .isNotificationsEnabled)
}
}

func getAttributionInfo(currentDate: Date) -> IterableAttributionInfo? {
(try? codable(withKey: .attributionInfo, currentDate: currentDate)) ?? nil
}
Expand Down Expand Up @@ -196,6 +204,7 @@ class IterableUserDefaults {
static let deviceId = UserDefaultsKey(value: Const.UserDefault.deviceId)
static let sdkVersion = UserDefaultsKey(value: Const.UserDefault.sdkVersion)
static let offlineMode = UserDefaultsKey(value: Const.UserDefault.offlineMode)
static let isNotificationsEnabled = UserDefaultsKey(value: Const.UserDefault.isNotificationsEnabled)
}

private struct Envelope: Codable {
Expand Down
8 changes: 8 additions & 0 deletions swift-sdk/Internal/Utilities/LocalStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ struct LocalStorage: LocalStorageProtocol {
}
}

var isNotificationsEnabled: Bool {
get {
iterableUserDefaults.isNotificationsEnabled
} set {
iterableUserDefaults.isNotificationsEnabled = newValue
}
}

func getAttributionInfo(currentDate: Date) -> IterableAttributionInfo? {
iterableUserDefaults.getAttributionInfo(currentDate: currentDate)
}
Expand Down
2 changes: 2 additions & 0 deletions swift-sdk/Internal/Utilities/LocalStorageProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ protocol LocalStorageProtocol {

var offlineMode: Bool { get set }

var isNotificationsEnabled: Bool { get set }

func getAttributionInfo(currentDate: Date) -> IterableAttributionInfo?

func save(attributionInfo: IterableAttributionInfo?, withExpiration expiration: Date?)
Expand Down
2 changes: 2 additions & 0 deletions tests/common/MockLocalStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class MockLocalStorage: LocalStorageProtocol {

var offlineMode: Bool = false

var isNotificationsEnabled: Bool = false

func getAttributionInfo(currentDate: Date) -> IterableAttributionInfo? {
guard !MockLocalStorage.isExpired(expiration: attributionInfoExpiration, currentDate: currentDate) else {
return nil
Expand Down
Loading