From 8ffad16ddbdf9e389e3e3a255b5c7f706808f3e9 Mon Sep 17 00:00:00 2001 From: Bilal Date: Mon, 23 Sep 2024 09:20:29 +0530 Subject: [PATCH] bgtask --- .../xcshareddata/xcschemes/WOKA.xcscheme | 8 +- WOKA/Info.plist | 6 + WOKA/Main/Delegate/AppDelegate.swift | 108 ++++++++++++++++++ 3 files changed, 118 insertions(+), 4 deletions(-) diff --git a/WOKA.xcodeproj/xcshareddata/xcschemes/WOKA.xcscheme b/WOKA.xcodeproj/xcshareddata/xcschemes/WOKA.xcscheme index 8a76eba..2ecdd02 100644 --- a/WOKA.xcodeproj/xcshareddata/xcschemes/WOKA.xcscheme +++ b/WOKA.xcodeproj/xcshareddata/xcschemes/WOKA.xcscheme @@ -77,19 +77,19 @@ + isEnabled = "NO"> + isEnabled = "NO"> + isEnabled = "YES"> + isEnabled = "YES"> diff --git a/WOKA/Info.plist b/WOKA/Info.plist index 9668053..78e1eb8 100644 --- a/WOKA/Info.plist +++ b/WOKA/Info.plist @@ -2,6 +2,10 @@ + BGTaskSchedulerPermittedIdentifiers + + com.woka.refresh + API_KEY_ID $(API_KEY_ID) API_KEY_PASS @@ -44,6 +48,8 @@ audio remote-notification + fetch + processing diff --git a/WOKA/Main/Delegate/AppDelegate.swift b/WOKA/Main/Delegate/AppDelegate.swift index 1b4d850..013b019 100644 --- a/WOKA/Main/Delegate/AppDelegate.swift +++ b/WOKA/Main/Delegate/AppDelegate.swift @@ -12,6 +12,7 @@ import JWPlayerKit import Firebase import GoogleMobileAds import OneSignalFramework +import BackgroundTasks let appDelegate = UIApplication.shared.delegate as! AppDelegate @@ -20,7 +21,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var deviceOrientation = UIInterfaceOrientationMask.portrait + let taskID = "com.woka.refresh" + func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { return deviceOrientation } @@ -69,8 +72,45 @@ class AppDelegate: UIResponder, UIApplicationDelegate { //Configure JWPlayer Liscense JWPlayerKitLicense.setLicenseKey("Lgok1t7H4PKY+M8FZqmCx54ibUF+NeCTn+xgd+/LVTaRdc+L") + + // Register the background refresh task +// BGTaskScheduler.shared.register(forTaskWithIdentifier: taskID, using: nil) { task in +// // This block is called when the task is triggered +// guard let task = task as? BGAppRefreshTask else{return} +// self.handleTask(task: task) +// } +// +// scheduleTasks() + return true } + +// func scheduleTasks(){ +// BGTaskScheduler.shared.getPendingTaskRequests { requests in +// print("Request :- ", requests.count) +// +// guard requests.isEmpty else{ +// return +// } +// +// // submit task to be scheduled +// do{ +// let newTask = BGAppRefreshTaskRequest(identifier: self.taskID) +// // Set when you want the task to run (optional) +// newTask.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60) // minutes * seconds ie. 1 min * 60 second, it ill trigger in 1 min. +// print("Task Scheduled.") +// try BGTaskScheduler.shared.submit(newTask) +// } catch{ +// print("Issue with task") +// } +// } +// +// } +// +// func handleTask(task : BGAppRefreshTask){ +// self.localNoti(body: "sad") +// task.setTaskCompleted(success: true) +// } // MARK: UISceneSession Lifecycle @@ -85,7 +125,75 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. // Use this method to release any resources that were specific to the discarded scenes, as they will not return. } + + func scheduleAppRefresh() { + let request = BGAppRefreshTaskRequest(identifier: taskID) + + // Set when you want the task to run (optional) + request.earliestBeginDate = Date(timeIntervalSinceNow: 2 * 60) // 15 minutes from now + + do { + try BGTaskScheduler.shared.submit(request) + print("Could not schedule app refresh scheduled") + } catch { + print("Could not schedule app refresh: \(error)") + } + } + + func handleAppRefresh(task: BGAppRefreshTask) { + // Schedule a new refresh task + scheduleAppRefresh() + + // Perform the background task (e.g., fetching data from a server) + let operationQueue = OperationQueue() + operationQueue.maxConcurrentOperationCount = 1 + + let fetchOperation = BlockOperation { + // Perform your background fetch task here (e.g., network call) + self.performBackgroundFetch { newData in + task.setTaskCompleted(success: newData) + } + } + + task.expirationHandler = { + // Cancel the operation if it takes too long + operationQueue.cancelAllOperations() + } + + operationQueue.addOperation(fetchOperation) + } + func performBackgroundFetch(completion: @escaping (Bool) -> Void) { + // Simulate a network call + DispatchQueue.global().asyncAfter(deadline: .now() + 5) { + // Perform your background work here + let newDataFetched = true // Assume data was fetched + completion(newDataFetched) + print("sdaasdasdasdas") + self.localNoti(body: newDataFetched ? "true" : "false") + } + } + + func localNoti(body : String? = nil){ + //creating the notification content + let content = UNMutableNotificationContent() + + //adding title, subtitle, body and badge + content.title = "Your are out of Geofence, Battery testing" + content.subtitle = "" + content.body = body == nil ? "Please contact your Careperson" : body! + content.badge = 1 + + //getting the notification trigger + //it will be called after 5 seconds + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false) + + //getting the notification request + let request = UNNotificationRequest(identifier: "SimplifiedIOSNotification", content: content, trigger: trigger) + + //adding the notification to notification center + UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) + } } extension AppDelegate {