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

Enhance push notification state tracking in sd ks #879

Open
wants to merge 4 commits into
base: feature/DEV-719-Support-Firebase-on-the-Swift-SDK
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
0C8EE4682CEEF678006F17D6 /* FirebaseStorage in Frameworks */ = {isa = PBXBuildFile; productRef = 0C8EE4672CEEF678006F17D6 /* FirebaseStorage */; };
0C8EE46A2CEEF678006F17D6 /* FirebaseStorageCombine-Community in Frameworks */ = {isa = PBXBuildFile; productRef = 0C8EE4692CEEF678006F17D6 /* FirebaseStorageCombine-Community */; };
0C8EE46C2CEEF678006F17D6 /* FirebaseVertexAI in Frameworks */ = {isa = PBXBuildFile; productRef = 0C8EE46B2CEEF678006F17D6 /* FirebaseVertexAI */; };
0CD4B0562CEF00BD000DD590 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 0CD4B0552CEF00BD000DD590 /* GoogleService-Info.plist */; };
0CEAAB3E2CF8BB4F00172B3E /* guitar.wav in Resources */ = {isa = PBXBuildFile; fileRef = 0CEAAB3D2CF8BB4F00172B3E /* guitar.wav */; };
37088F332B3C38250000B218 /* IterableAppExtensions in Frameworks */ = {isa = PBXBuildFile; productRef = 37088F322B3C38250000B218 /* IterableAppExtensions */; };
37088F352B3C38250000B218 /* IterableSDK in Frameworks */ = {isa = PBXBuildFile; productRef = 37088F342B3C38250000B218 /* IterableSDK */; };
Expand Down Expand Up @@ -79,7 +78,6 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0CD4B0552CEF00BD000DD590 /* GoogleService-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
0CEAAB3D2CF8BB4F00172B3E /* guitar.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = guitar.wav; sourceTree = "<group>"; };
551A5FEC251AB1510004C9A0 /* swift-sdk */ = {isa = PBXFileReference; lastKnownFileType = folder; name = "swift-sdk"; path = ../..; sourceTree = "<group>"; };
AC1BDF5720E30436000010CA /* DeepLinkHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeepLinkHandler.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -191,7 +189,6 @@
isa = PBXGroup;
children = (
0CEAAB3D2CF8BB4F00172B3E /* guitar.wav */,
0CD4B0552CEF00BD000DD590 /* GoogleService-Info.plist */,
E9C60B782B3C2061005C4462 /* EmbeddedMessages */,
AC5ECD9D20E303F50081E1DA /* ViewControllers */,
AC5ECD9B20E3038B0081E1DA /* Deep Link Handling */,
Expand Down Expand Up @@ -386,7 +383,6 @@
0CEAAB3E2CF8BB4F00172B3E /* guitar.wav in Resources */,
ACA3A14320E2F6B100FEF74F /* LaunchScreen.storyboard in Resources */,
ACA3A14020E2F6B100FEF74F /* Assets.xcassets in Resources */,
0CD4B0562CEF00BD000DD590 /* GoogleService-Info.plist in Resources */,
ACA3A13E20E2F6AF00FEF74F /* Main.storyboard in Resources */,
E9C60B7B2B3C2061005C4462 /* embeddedmessages.json in Resources */,
);
Expand Down
1 change: 1 addition & 0 deletions swift-sdk/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ enum Const {
static let matchedCriteria = "itbl_matched_criteria"
static let eventList = "itbl_event_list"
static let anonymousUsageTrack = "itbl_anonymous_usage_track"
static let isNotificationsEnabled = "isNotificationsEnabled"
static let attributionInfoExpiration = 24
}

Expand Down
31 changes: 31 additions & 0 deletions swift-sdk/Internal/InternalIterableAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
}

hexToken = token
isFromFCM = isFromFCM
let registerTokenInfo = RegisterTokenInfo(hexToken: token,
appName: appName,
pushServicePlatform: config.pushPlatform,
Expand All @@ -297,6 +298,9 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
onFailure?(reason, data)
}
)
notificationStateProvider.isNotificationsEnabled { isEnabled in
self.localStorage.isNotificationsEnabled = isEnabled
}
}

@discardableResult
Expand Down Expand Up @@ -657,10 +661,13 @@ 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
private var hexToken: String?
private var isFromFCM: Bool?

private var launchOptions: [UIApplication.LaunchOptionsKey: Any]?

Expand Down Expand Up @@ -841,6 +848,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
//localStorage.email = nil // remove this before pushing the code (only for testing)
inAppDisplayer = dependencyContainer.inAppDisplayer
urlOpener = dependencyContainer.urlOpener
notificationCenter = dependencyContainer.notificationCenter
deepLinkManager = DeepLinkManager(redirectNetworkSessionProvider: dependencyContainer)
}

Expand Down Expand Up @@ -873,10 +881,33 @@ 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 {
if let token = self.hexToken, let isFromFCM = self.isFromFCM, isFromFCM {
IterableAPI.registerFCM(token: token)
} else {
self.notificationStateProvider.registerForRemoteNotifications()
}
}
}
}
}

private func handle(launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
guard let launchOptions = launchOptions else {
return
Expand Down
9 changes: 9 additions & 0 deletions swift-sdk/Internal/IterableUserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ class IterableUserDefaults {
}
}

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

var body = [AnyHashable: Any]()

private func anonSessionsData(withKey key: UserDefaultsKey) -> IterableAnonSessionsWrapper? {
Expand Down Expand Up @@ -310,6 +318,7 @@ class IterableUserDefaults {
static let criteriaData = UserDefaultsKey(value: Const.UserDefault.criteriaData)
static let anonymousSessions = UserDefaultsKey(value: Const.UserDefault.anonymousSessions)
static let anonymousUsageTrack = UserDefaultsKey(value: Const.UserDefault.anonymousUsageTrack)
static let isNotificationsEnabled = UserDefaultsKey(value: Const.UserDefault.isNotificationsEnabled)

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

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/LocalStorageProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ protocol LocalStorageProtocol {

var anonymousSessions: IterableAnonSessionsWrapper? { get set }

var isNotificationsEnabled: Bool { get set }

func getAttributionInfo(currentDate: Date) -> IterableAttributionInfo?

func save(attributionInfo: IterableAttributionInfo?, withExpiration expiration: Date?)
Expand Down
Loading