66 lines
2.2 KiB
Swift
66 lines
2.2 KiB
Swift
//
|
|
// AppDelegate.swift
|
|
// SportsTime
|
|
//
|
|
// Handles push notification registration and CloudKit subscription notifications
|
|
//
|
|
|
|
import UIKit
|
|
import CloudKit
|
|
import UserNotifications
|
|
|
|
class AppDelegate: NSObject, UIApplicationDelegate {
|
|
|
|
func application(
|
|
_ application: UIApplication,
|
|
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
|
|
) -> Bool {
|
|
// Register for remote notifications (required for CloudKit subscriptions)
|
|
application.registerForRemoteNotifications()
|
|
return true
|
|
}
|
|
|
|
func application(
|
|
_ application: UIApplication,
|
|
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
|
|
) {
|
|
print("📡 [Push] Registered for remote notifications")
|
|
}
|
|
|
|
func application(
|
|
_ application: UIApplication,
|
|
didFailToRegisterForRemoteNotificationsWithError error: Error
|
|
) {
|
|
print("📡 [Push] Failed to register: \(error.localizedDescription)")
|
|
}
|
|
|
|
func application(
|
|
_ application: UIApplication,
|
|
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
|
|
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
|
|
) {
|
|
guard let notification = CKNotification(fromRemoteNotificationDictionary: userInfo) as? CKQueryNotification,
|
|
let subscriptionID = notification.subscriptionID,
|
|
CloudKitService.canonicalSubscriptionIDs.contains(subscriptionID),
|
|
let recordType = CloudKitService.recordType(forSubscriptionID: subscriptionID) else {
|
|
completionHandler(.noData)
|
|
return
|
|
}
|
|
|
|
Task { @MainActor in
|
|
var changed = false
|
|
|
|
if notification.queryNotificationReason == .recordDeleted,
|
|
let recordID = notification.recordID {
|
|
changed = await BackgroundSyncManager.shared.applyDeletionHint(
|
|
recordType: recordType,
|
|
recordName: recordID.recordName
|
|
)
|
|
}
|
|
|
|
let updated = await BackgroundSyncManager.shared.triggerSyncFromPushNotification(subscriptionID: subscriptionID)
|
|
completionHandler((changed || updated) ? .newData : .noData)
|
|
}
|
|
}
|
|
}
|