we have a socket.io consumer for chats and for some information fetching additionally,
so the issue is once we put the app in background in IOS, after 30 seconds it disconnects, sure it connects once more if you put the app on foreground, however i want it to remain related for no less than 5 minutes.
Tried this code additionally from chatgpt.
import UIKit
import Capacitor
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var backgroundTask: UIBackgroundTaskIdentifier = .invalid
var timer: Timer?
func software(_ software: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
func keepSocketAliveForTenMinutes() {
// Begin a background job to increase runtime
backgroundTask = UIApplication.shared.beginBackgroundTask(withName: "KeepSocketAlive") {
// Finish the duty if time expires
UIApplication.shared.endBackgroundTask(self.backgroundTask)
self.backgroundTask = .invalid
}
// Begin a timer to maintain the socket alive
timer = Timer.scheduledTimer(withTimeInterval: 10 * 60, repeats: false) { [weak self] _ in
self?.endBackgroundTask()
}
}
func endBackgroundTask() {
if backgroundTask != .invalid {
UIApplication.shared.endBackgroundTask(backgroundTask)
backgroundTask = .invalid
}
timer?.invalidate()
timer = nil
}
func applicationDidEnterBackground(_ software: UIApplication) {
// Begin preserving the socket alive for 10 minutes
keepSocketAliveForTenMinutes()
}
func applicationWillEnterForeground(_ software: UIApplication) {
// Finish the background job when the app returns to the foreground
endBackgroundTask()
}
func applicationWillTerminate(_ software: UIApplication) {
endBackgroundTask()
}
// Different strategies ...
}