· 6 years ago · Jul 03, 2019, 08:40 PM
1 func sendAlert(intrest: String) -> Void {
2 // Interests array.
3 let inputedTitle = "Title"
4 let inputedBody = "Body"
5 let rand = randomString(length: 12)
6 let url = URL(string: "https://cc82158f-4719-4b7f-a0fe-fa42c9678a16.pushnotifications.pusher.com/publish_api/v1/instances/cc82158f-4719-4b7f-a0fe-fa42c9678a16/publishes/interests")
7
8 var request = URLRequest(url: url!)
9
10 let json: [String: Any] = ["interests": [intrest], "title": inputedTitle, "body": inputedBody, "publishId": rand]
11 let jsonData = try? JSONSerialization.data(withJSONObject: json)
12
13
14 request.httpMethod = "POST"
15
16 request.addValue("Bearer \(secretKey)", forHTTPHeaderField: "Authorization")
17 request.addValue("application/json", forHTTPHeaderField: "Content-Type")
18 request.httpBody = jsonData
19
20 let task = URLSession.shared.dataTask(with: request) { data, response, error in
21 guard error == nil else {
22 print(error!)
23 return
24 }
25 guard let data = data else {
26 print("Data is empty")
27 return
28 }
29
30 let json = try! JSONSerialization.jsonObject(with: data, options: [])
31 print(json)
32 }
33
34 task.resume()
35 }