Skip to content

Commit

Permalink
fix: convert APN device token to string (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
levibostian authored Sep 15, 2021
1 parent d74adb7 commit 1f64a13
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/MessagingPush/MessagingPush.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class MessagingPush: MessagingPushInstance {
*/
public func registerDeviceToken(_ deviceToken: Data,
onComplete: @escaping (Result<Void, CustomerIOError>) -> Void) {
let device = Device(token: String(decoding: deviceToken, as: UTF8.self), lastUsed: Date())
let device = Device(token: String(apnDeviceToken: deviceToken), lastUsed: Date())
guard let bodyData = jsonAdapter.toJson(RegisterDeviceRequest(device: device)) else {
return onComplete(.failure(.http(.noRequestMade(nil))))
}
Expand Down
8 changes: 7 additions & 1 deletion Sources/Tracking/Extensions/StringExtensions.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import Foundation

internal extension String {
public extension String {
init(apnDeviceToken: Data) {
/// Convert `Data` to `String` for APN device token.
/// [Reference](https://nshipster.com/apns-device-tokens/)
self = apnDeviceToken.map { String(format: "%02x", $0) }.joined()
}

var data: Data! {
data(using: .utf8)
}
Expand Down

0 comments on commit 1f64a13

Please sign in to comment.