if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Background Fetch"
content.subtitle = "fetch working"
content.body = "background fetch is working fine"
content.sound = UNNotificationSound.default()
let requ = UNNotificationRequest(identifier: "ContentIdentifier", content: content, trigger: nil)
if var urlComponents = URLComponents(string: "*****") {
urlComponents.query = "media=music&entity=song&term=\(searchTerm)"
guard let url = urlComponents.url else {
return
}
dataTask =
defaultSession.dataTask(with: url) { [weak self] data, response, error in
defer {
self?.dataTask = nil
}
if let error = error {
self?.errorMessage += "DataTask error: " +
error.localizedDescription + "\n"
} else if
let data = data,
let response = response as? HTTPURLResponse,
response.statusCode == 200 {
UserDefaults.standard.set("Success", forKey: "backgroundFetch")
center.add(requ, withCompletionHandler: { (error) in
if error != nil {
UserDefaults.standard.set("localsucess", forKey: "LocalNotificationsuccess")
}
})
completionHandler(.newData)
DispatchQueue.main.async {
completionHandler(.failed)
completion(self?.tracks, self?.errorMessage ?? "")
}
}
}
dataTask?.resume()
}