Skip to content

[Mob-9747] support firebase on the swift sdk #873

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

Open
wants to merge 3 commits into
base: feature/itbl_track_anon_user
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

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
{
"object": {
"pins": [
{
"package": "abseil",
"repositoryURL": "https://github.com/google/abseil-cpp-binary.git",
"state": {
"branch": null,
"revision": "194a6706acbd25e4ef639bcaddea16e8758a3e27",
"version": "1.2024011602.0"
}
},
{
"package": "AppCheck",
"repositoryURL": "https://github.com/google/app-check.git",
"state": {
"branch": null,
"revision": "61b85103a1aeed8218f17c794687781505fbbef5",
"version": "11.2.0"
}
},
{
"package": "Firebase",
"repositoryURL": "https://github.com/firebase/firebase-ios-sdk.git",
"state": {
"branch": null,
"revision": "dbdfdc44bee8b8e4eaa5ec27eb12b9338f3f2bc1",
"version": "11.5.0"
}
},
{
"package": "GoogleAppMeasurement",
"repositoryURL": "https://github.com/google/GoogleAppMeasurement.git",
"state": {
"branch": null,
"revision": "4f234bcbdae841d7015258fbbf8e7743a39b8200",
"version": "11.4.0"
}
},
{
"package": "GoogleDataTransport",
"repositoryURL": "https://github.com/google/GoogleDataTransport.git",
"state": {
"branch": null,
"revision": "617af071af9aa1d6a091d59a202910ac482128f9",
"version": "10.1.0"
}
},
{
"package": "GoogleUtilities",
"repositoryURL": "https://github.com/google/GoogleUtilities.git",
"state": {
"branch": null,
"revision": "53156c7ec267db846e6b64c9f4c4e31ba4cf75eb",
"version": "8.0.2"
}
},
{
"package": "gRPC",
"repositoryURL": "https://github.com/google/grpc-binary.git",
"state": {
"branch": null,
"revision": "f56d8fc3162de9a498377c7b6cea43431f4f5083",
"version": "1.65.1"
}
},
{
"package": "GTMSessionFetcher",
"repositoryURL": "https://github.com/google/gtm-session-fetcher.git",
"state": {
"branch": null,
"revision": "5cfe5f090c982de9c58605d2a82a4fc77b774fbd",
"version": "4.1.0"
}
},
{
"package": "InteropForGoogle",
"repositoryURL": "https://github.com/google/interop-ios-for-google-sdks.git",
"state": {
"branch": null,
"revision": "2d12673670417654f08f5f90fdd62926dc3a2648",
"version": "100.0.0"
}
},
{
"package": "leveldb",
"repositoryURL": "https://github.com/firebase/leveldb.git",
"state": {
"branch": null,
"revision": "a0bc79961d7be727d258d33d5a6b2f1023270ba1",
"version": "1.22.5"
}
},
{
"package": "nanopb",
"repositoryURL": "https://github.com/firebase/nanopb.git",
"state": {
"branch": null,
"revision": "b7e1104502eca3a213b46303391ca4d3bc8ddec1",
"version": "2.30910.0"
}
},
{
"package": "Promises",
"repositoryURL": "https://github.com/google/promises.git",
"state": {
"branch": null,
"revision": "540318ecedd63d883069ae7f1ed811a2df00b6ac",
"version": "2.4.0"
}
},
{
"package": "SwiftProtobuf",
"repositoryURL": "https://github.com/apple/swift-protobuf.git",
"state": {
"branch": null,
"revision": "ebc7251dd5b37f627c93698e4374084d98409633",
"version": "1.28.2"
}
}
]
},
"version": 1
}
30 changes: 27 additions & 3 deletions sample-apps/swift-sample-app/swift-sample-app/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

import UIKit
import UserNotifications

import IterableSDK
import Firebase
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, IterableAuthDelegate {
Expand Down Expand Up @@ -37,14 +38,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate, IterableAuthDelegate {
func application(_: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// ITBL: Setup Notification
setupNotifications()
FirebaseApp.configure()
Messaging.messaging().delegate = self

// ITBL: Initialize API
let config = IterableConfig()
config.customActionDelegate = self
config.urlDelegate = self
config.inAppDisplayInterval = 1
config.anonUserDelegate = self
config.enableAnonTracking = true
config.enableAnonActivation = true
config.authDelegate = self
IterableAPI.initialize(apiKey: iterableApiKey,
launchOptions: launchOptions,
Expand Down Expand Up @@ -104,6 +107,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, IterableAuthDelegate {

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
IterableAppIntegration.application(application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler)
Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler(.noData)
}

// MARK: Deep link
Expand All @@ -121,7 +126,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate, IterableAuthDelegate {

// ITBL:
func application(_: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
IterableAPI.register(token: deviceToken)
// For Firebase Notification Intigration
if !deviceToken.hexString.isEmpty {
Messaging.messaging().apnsToken = deviceToken
}
// For APNS
// IterableAPI.register(token: deviceToken)
}

func application(_: UIApplication, didFailToRegisterForRemoteNotificationsWithError _: Error) {}
Expand Down Expand Up @@ -152,6 +162,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate, IterableAuthDelegate {
}
}

extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
if let token = fcmToken {
IterableAPI.registerFCM(token: token)
}
}
}

// MARK: UNUserNotificationCenterDelegate

extension AppDelegate: UNUserNotificationCenterDelegate {
Expand Down Expand Up @@ -196,3 +214,9 @@ extension AppDelegate: IterableCustomActionDelegate {
}
}


extension Data {
public var hexString: String {
return map { String(format: "%02.2hhx", arguments: [$0]) }.joined()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ class CoffeeListTableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "anonymousUsageTrackCell", for: indexPath)
cell.textLabel?.text = IterableAPI.getAnonymousUsageTracked() ? "Tap to disable Anonymous Usage Track" : "Tap to enable Anonymous Usage Track"
cell.textLabel?.text = IterableAPI.getVisitorUsageTracked() ? "Tap to disable Anonymous Usage Track" : "Tap to enable Anonymous Usage Track"
cell.textLabel?.numberOfLines = 0
cell.accessoryType = IterableAPI.getAnonymousUsageTracked() ? .checkmark : .none
cell.accessoryType = IterableAPI.getVisitorUsageTracked() ? .checkmark : .none
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "coffeeCell", for: indexPath)
Expand All @@ -90,8 +90,8 @@ class CoffeeListTableViewController: UITableViewController {

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
let permissionToTrack = IterableAPI.getAnonymousUsageTracked()
IterableAPI.setAnonymousUsageTracked(isAnonymousUsageTracked: !permissionToTrack)
let permissionToTrack = IterableAPI.getVisitorUsageTracked()
IterableAPI.setVisitorUsageTracked(isVisitorUsageTracked: !permissionToTrack)
self.tableView.reloadData()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class CoffeeViewController: UIViewController {

// ITBL: Track attribution to purchase
IterableAPI.track(purchase: 10.0, items: [CommerceItem(id: coffee.name.lowercased(), name: coffee.name, price: 10.0, quantity: 1)], dataFields: dataFields)
IterableAPI.updateUser(["firstName":"Leah"], mergeNestedObjects: false)
}
}

Expand Down
Binary file not shown.
8 changes: 8 additions & 0 deletions swift-sdk/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ enum JsonValue {
static let applicationJson = "application/json"
static let apnsSandbox = "APNS_SANDBOX"
static let apnsProduction = "APNS"
static let gcmSandbox = "GCM"
static let gcmProduction = "GCM"
static let iOS = "iOS"
static let bearer = "Bearer"

Expand Down Expand Up @@ -432,6 +434,12 @@ extension String: JsonValueRepresentable {
}
}

extension String {
func toJsonDict() -> [AnyHashable: Any] {
try! JSONSerialization.jsonObject(with: data(using: .utf8)!, options: []) as! [AnyHashable: Any]
}
}

extension Bool: JsonValueRepresentable {
public var jsonValue: Any {
self
Expand Down
4 changes: 2 additions & 2 deletions swift-sdk/Internal/ApiClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ class ApiClient {

extension ApiClient: ApiClientProtocol {

func register(registerTokenInfo: RegisterTokenInfo, notificationsEnabled: Bool) -> Pending<SendRequestValue, SendRequestError> {
func register(registerTokenInfo: RegisterTokenInfo, notificationsEnabled: Bool, isFromFCM: Bool) -> Pending<SendRequestValue, SendRequestError> {
let result = createRequestCreator().flatMap { $0.createRegisterTokenRequest(registerTokenInfo: registerTokenInfo,
notificationsEnabled: notificationsEnabled) }
notificationsEnabled: notificationsEnabled, isFromFCM: isFromFCM) }
return send(iterableRequestResult: result)
}

Expand Down
2 changes: 1 addition & 1 deletion swift-sdk/Internal/ApiClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Foundation

protocol ApiClientProtocol: AnyObject {
func register(registerTokenInfo: RegisterTokenInfo, notificationsEnabled: Bool) -> Pending<SendRequestValue, SendRequestError>
func register(registerTokenInfo: RegisterTokenInfo, notificationsEnabled: Bool, isFromFCM: Bool) -> Pending<SendRequestValue, SendRequestError>

func updateUser(_ dataFields: [AnyHashable: Any], mergeNestedObjects: Bool) -> Pending<SendRequestValue, SendRequestError>

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

// MARK: - API Request Calls

func register(token: Data,
func register(token: String,
isFromFCM: Bool,
onSuccess: OnSuccessHandler? = nil,
onFailure: OnFailureHandler? = nil) {

Expand All @@ -270,14 +271,14 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {

if !isEitherUserIdOrEmailSet() && localStorage.userIdAnnon == nil {
if config.enableAnonActivation {
anonymousUserManager.trackAnonTokenRegistration(token: token.hexString())
anonymousUserManager.trackAnonTokenRegistration(token: token)
}
onFailure?("Iterable SDK must be initialized with an API key and user email/userId before calling SDK methods", nil)
return
}

hexToken = token.hexString()
let registerTokenInfo = RegisterTokenInfo(hexToken: token.hexString(),
hexToken = token
let registerTokenInfo = RegisterTokenInfo(hexToken: token,
appName: appName,
pushServicePlatform: config.pushPlatform,
apnsType: dependencyContainer.apnsTypeChecker.apnsType,
Expand All @@ -286,6 +287,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
sdkVersion: localStorage.sdkVersion)
requestHandler.register(registerTokenInfo: registerTokenInfo,
notificationStateProvider: notificationStateProvider,
isFromFCM: isFromFCM,
onSuccess: { (_ data: [AnyHashable: Any]?) in
self._successCallback?(data)
onSuccess?(data)
Expand Down
9 changes: 8 additions & 1 deletion swift-sdk/Internal/InternalIterableAppIntegration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,14 @@ struct InternalIterableAppIntegration {
// Normally itblValue would be the value stored in "itbl" key inside of userInfo.
// But it is possible to save them at root level for debugging purpose.
private static func itblValue(fromUserInfo userInfo: [AnyHashable: Any]) -> [AnyHashable: Any]? {
let itbl = userInfo[JsonKey.Payload.metadata] as? [AnyHashable: Any]
var itbl: [AnyHashable: Any]? = nil
if let itblData = userInfo[JsonKey.Payload.metadata] as? [AnyHashable: Any] {
itbl = itblData
} else if let itblString = userInfo[JsonKey.Payload.metadata] as? String {
itbl = itblString.toJsonDict()
} else {
return nil
}

#if DEBUG
guard let value = itbl else {
Expand Down
7 changes: 4 additions & 3 deletions swift-sdk/Internal/OnlineRequestProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ struct OnlineRequestProcessor: RequestProcessorProtocol {
}

func register(registerTokenInfo: RegisterTokenInfo,
notificationStateProvider: NotificationStateProviderProtocol,
notificationStateProvider: NotificationStateProviderProtocol, isFromFCM: Bool,
onSuccess: OnSuccessHandler? = nil,
onFailure: OnFailureHandler? = nil) {
notificationStateProvider.isNotificationsEnabled { enabled in
self.register(registerTokenInfo: registerTokenInfo,
notificationsEnabled: enabled,
isFromFCM: isFromFCM,
onSuccess: onSuccess,
onFailure: onFailure)
}
Expand Down Expand Up @@ -324,11 +325,11 @@ struct OnlineRequestProcessor: RequestProcessorProtocol {

@discardableResult
private func register(registerTokenInfo: RegisterTokenInfo,
notificationsEnabled: Bool,
notificationsEnabled: Bool, isFromFCM: Bool,
onSuccess: OnSuccessHandler? = nil,
onFailure: OnFailureHandler? = nil) -> Pending<SendRequestValue, SendRequestError> {
sendRequest(requestProvider: { apiClient.register(registerTokenInfo: registerTokenInfo,
notificationsEnabled: notificationsEnabled) },
notificationsEnabled: notificationsEnabled, isFromFCM: isFromFCM) },
successHandler: onSuccess,
failureHandler: onFailure,
requestIdentifier: "registerToken")
Expand Down
Loading
Loading