Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
func application( _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
print("Device Token: \(token)")
sendDeviceToken(device: Device(platform: "ios", deviceToken: token))
}
struct Device: Codable {
var platform: String
var deviceToken: String
}
func sendDeviceToken(device: Device) {
var request = URLRequest(url: URL(string: "http://172.20.3.158:9090/devices")!)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.httpBody = try? JSONEncoder().encode(device)
URLSession.shared.dataTask(with:request, completionHandler: {(data, response, error) in
if error != nil {
print(error)
} else {
print(data)
}
}).resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.