I am trying to download file on click of notification Download button but when application is in background then api call is not working.
API call is working when application is in foreground but when application is in background api call not working.
Notification Code:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo as! [String: AnyObject]
switch response.actionIdentifier {
case "DownloadDutyPlanAction":
print("Download tap")
downloadDutyPlanFromNotification(fetchCompletionHandler: nil)
completionHandler()
case "CancelAction":
print("Cancel tap")
default:
completionHandler()
}
}
func downloadFromNotification() {
let downloadPDFOperation = request(forDownloadUrl route: "URL") { result in
switch result {
case .success:
// Success
case .failure:
// Error
}
}
QueueManager.shared.enqueueNotification(downloadPDFOperation)
}
Network class:
private var task: URLSessionTask?
private var session = URLSession.init(configuration: URLSessionConfiguration.default, delegate: NSURLSessionPinningDelegate(), delegateQueue: nil)
func request(forDownloadUrl route: EndPoint, completion: @escaping NetworkDownloadRouterCompletion) {
do {
let request = try self.buildRequest(from: route)
task = session.downloadTask(with: request) { localURL, response, error in
if let tempLocation = localURL {
completion(response, error, tempLocation)
} else {
completion(response, error, nil)
}
}
} catch {
completion(nil, error, nil)
}
self.task?.resume()
}