I am try to post data back to server when the app is in background or suspended state. I have implemented actionable push notification with yes and No actions. I have to update the backend with the yes or no is tapped. My below code works fine if the app is running in foreground but it is failing in background or suspended state. Could any one know how to handle this.
func updateEmployeeStatus(){
let json = ["id": "23", "empId": "3242", "status": "Success"] as Dictionary<String, String>
let jsonData = try? JSONSerialization.data(withJSONObject: json)
// create post request
let url = URL(string: "https://10.91.60.14/api/employee/status")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
// insert json data to the request
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
print("The response is",responseJSON)
}
}
task.resume()
}