Hi All,
I really need your help, I have been racking my brain to work out why, after a push notification triggers a fetchdata function from the server, my new bookings dont dynamically update the counter against the booking types.
print("Received remote notification: \(userInfo)")
if let dataInfo = userInfo["data"] as? [String: Any],
let jsonData = try? JSONSerialization.data(withJSONObject: dataInfo) {
print("Processing data from notification...")
DispatchQueue.main.async {
self.eventsViewModel.updateFromPushNotification(data: jsonData) { result in
completionHandler(result)
}
}
} else {
print("Failed to parse notification data")
completionHandler(.noData)
}
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register for remote notifications: \(error)")
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Will present notification: \(notification.request.content.userInfo)")
DispatchQueue.main.async {
self.eventsViewModel.fetchData()
}
completionHandler([.banner, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Did receive notification response: \(response.notification.request.content.userInfo)")
let userInfo = response.notification.request.content.userInfo
if let fetchNeeded = userInfo["fetchNeeded"] as? Bool, fetchNeeded {
print("Initiating data fetch due to user interaction...")
DispatchQueue.main.async {
self.eventsViewModel.fetchData()
}
}
completionHandler()
}
@Published var bookings: [AnyBooking] = []
@Published var newBookings: [AnyBooking] = []
@Published var calendarBookings: [String: [AnyBooking]] = [:]
@Published var selectedBooking: AnyBooking?
private var cancellables = Set<AnyCancellable>()
private let calendarManager = CalendarManager.shared // Add calendarManager
func fetchData() {
guard let url = URL(string: "https://allsound.wisewms.uk/webhook_get") else {
print("Invalid URL for webhook request")
return
}
var request = URLRequest(url: url)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { [weak self] (data, response, error) in
guard let self = self else { return }
if let error = error {
print("Error fetching data: \(error.localizedDescription)")
return
}
if let data = data, !data.isEmpty {
if let newBookings = self.processBookings(data: data) {
DispatchQueue.main.async {
self.bookings = newBookings
self.separateAndOrganizeBookings(bookings: newBookings)
}
} else {
print("Failed to process bookings.")
}
} else {
print("No data received from server.")
}
}
task.resume()
}
@main
struct AllSoundApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@StateObject var eventsViewModel = EventsViewModel()
@Environment(\.scenePhase) var scenePhase
@AppStorage("selectedTheme") private var selectedTheme: Theme = .system
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(eventsViewModel)
.environmentObject(appDelegate.navigationCoordinator)
.preferredColorScheme(selectedTheme.colorScheme)
.onChange(of: scenePhase) { oldPhase, newPhase in
if newPhase == .active {
eventsViewModel.fetchData()
}
}
}
}
}