Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kylebrowning
Created February 7, 2019 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylebrowning/240be40a92bf219481f05dd3f4bc9c94 to your computer and use it in GitHub Desktop.
Save kylebrowning/240be40a92bf219481f05dd3f4bc9c94 to your computer and use it in GitHub Desktop.
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