Наблюдатель в моем super.viewDidLoad ViewController
Код: Выделить всё
NotificationCenter.default.addObserver(forName: NSNotification.Name("EnableButtonNotification"), object: nil, queue: nil) { [weak self] _ in
self?.dayUnlockButton.isEnabled = true
Код: Выделить всё
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in }
BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.TimeCapsule.removeBlur", using: nil) { task in
self.handleAppRefresh(task: task as! BGAppRefreshTask)
}
return true
func initializeBlurStates() {
if UserDefaults.standard.array(forKey: "blurStates") == nil {
let initialStates = [false, false, false, false]
UserDefaults.standard.set(initialStates, forKey: "blurStates")
}
}
}
public func scheduleNotificationAndBackgroundTask() {
let content = UNMutableNotificationContent()
content.title = "Time Capsule Ready"
content.body = "Your Time Capsule is Ready!"
content.sound = .default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "Time Capsule Ready", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
let bgRequest = BGAppRefreshTaskRequest(identifier: "com.TimeCapsule.removeBlur")
bgRequest.earliestBeginDate = Date(timeIntervalSinceNow: 5) // Adjust as necessary
do {
try BGTaskScheduler.shared.submit(bgRequest)
} catch {
print("Could not schedule background task: \(error)")
}
}
func handleAppRefresh(task: BGAppRefreshTask) {
task.expirationHandler = {
task.setTaskCompleted(success: false)
}
// Perform data fetching or update the UI
DispatchQueue.main.async {
NotificationCenter.default.post(name: NSNotification.Name("RemoveBlurEffect"), object: nil)
}
task.setTaskCompleted(success: true)
}
}
...
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
NotificationCenter.default.post(name: NSNotification.Name("EnableButtonNotification"), object: nil)
completionHandler([.banner, .sound])
}
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... o-the-user
Мобильная версия