Я пытаюсь загрузить файл при нажатии кнопки уведомления, но когда приложение находится в фоновом режиме, вызов API не работает.
Код уведомления:
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(fetchCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)?) {
let downloadPDFOperation = PDFDownloadOperation.init() { result in
switch result {
case .success:
DispatchQueue.main.async {
}
completionHandler?(UIBackgroundFetchResult.newData)
case .failure:
completionHandler?(UIBackgroundFetchResult.newData)
}
}
QueueManager.shared.enqueueNotification(downloadPDFOperation)
}
Класс сети:
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()
}
Подробнее здесь: https://stackoverflow.com/questions/782 ... background