My iOS Swift app is receiving the silent push notification even if the is not in use for 2-3 days, however I’ve to call api to update user location when the silent push notification is received. After 2-3 days of inactive app API to update server is getting timeout 9 out of 10 times. if I open the app for a while it will work again normally for 2 days.
Server response time is fine no issue with low response time.
What I’ve to do to get API success in inactive app for 2-3 days
I’m using URLSession for api calling
func sendLoc(data: [String: Any]) {
let url = URL(string: "https://abcd.com/api/profile/update_location")!
var request = URLRequest(url: url)
var accessToken = UserDefaultsManager.shared.getBearerToken()
request.setValue("Bearer (accessToken)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.httpMethod = "POST"
var parameters: [String: Any] = [:]
for (key, value) in data {
parameters[key] = value
}
request.httpBody = parameters.percentEncoded()
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard
let data = data,
let response = response as? HTTPURLResponse,
error == nil
else { // check for fundamental networking error
print("error", error ?? URLError(.badServerResponse))
return
}
guard (200 ... 299) ~= response.statusCode else { // check for http errors
print("statusCode should be 2xx, but is (response.statusCode)")
print("response = (response)")
return
}
do {
let responseObject = try JSONDecoder().decode(LocationResponse.self, from: data)
print(responseObject)
} catch {
print(error) // parsing error
if let responseString = String(data: data, encoding: .utf8) {
print("responseString = (responseString)")
} else {
print("unable to parse response as string")
}
}
}
task.resume()
}