Skip to content

Commit

Permalink
✨ Add setPushToken
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaatttt committed May 6, 2024
1 parent 06a5f6b commit 869c28c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
11 changes: 11 additions & 0 deletions Sources/AppcuesKit/Appcues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ public class Appcues: NSObject {
}
}

/// Provide the APNs device token to Appcues.
///
/// - Parameter deviceToken: A globally unique token that identifies this device to APNs.
///
/// This function is intended to be called from your `UIApplicationDelegate`'s
/// `application(_:didRegisterForRemoteNotificationsWithDeviceToken:)` function:
@objc
public func setPushToken(_ deviceToken: Data?) {
storage.pushToken = deviceToken?.map { String(format: "%02x", $0) }.joined()
}

/// Register a trait that modifies an `Experience`.
/// - Parameter trait: Trait to register.
/// - Returns: Whether the trait was successfully registered.
Expand Down
15 changes: 11 additions & 4 deletions Sources/AppcuesKit/Data/Analytics/AutoPropertyDecorator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,23 @@ internal class AutoPropertyDecorator: AnalyticsDecorating {
}

private func deviceAutoProperties() -> [String: Any] {
[
// Explicitly encode null value
let pushToken: Any = storage.pushToken ?? NSNull()

var properties = [
"_deviceId": storage.deviceID,
"_language": deviceLanguage,
"_pushToken": pushToken,
// TODO: more properties
// _pushToken
// _pushSubscriptionStatus
// _pushEnabled
// _pushEnabledBackground
]
.compactMapValues { $0 }

if let language = deviceLanguage {
properties["_language"] = language
}

return properties
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/AppcuesKit/Data/Networking/DynamicCodingKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ extension KeyedEncodingContainer where K == DynamicCodingKeys {
try self.encode(bool, forKey: codingKey)
case let date as Date:
try self.encode(date, forKey: codingKey)
case is NSNull:
try self.encodeNil(forKey: codingKey)
default:
encodingErrorKeys.append(codingKey.stringValue)
}
Expand Down
13 changes: 13 additions & 0 deletions Sources/AppcuesKit/Data/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ internal protocol DataStoring: AnyObject {

/// Optional, base 64 encoded signature to use as bearer token on API requests from the current user
var userSignature: String? { get set }

/// Push token for APNs
var pushToken: String? { get set }
}

internal class Storage: DataStoring {
Expand All @@ -37,6 +40,7 @@ internal class Storage: DataStoring {
case lastContentShownAt
case groupID
case userSignature
case pushToken
}

private let config: Appcues.Config
Expand Down Expand Up @@ -99,6 +103,15 @@ internal class Storage: DataStoring {
}
}

internal var pushToken: String? {
get {
return read(.pushToken, defaultValue: nil)
}
set {
write(.pushToken, newValue: newValue)
}
}

init(container: DIContainer) {
self.config = container.resolve(Appcues.Config.self)
self.deviceID = UIDevice.identifier
Expand Down

0 comments on commit 869c28c

Please sign in to comment.