Thanks, I think the notification will be the best approch.
For the count down, I used this code and it solves the problem perfectly. Thanks
.onReceive(NotificationCenter.default.publisher(for: WKExtension.applicationWillResignActiveNotification)) { _ in
print("Moving to the background")
notificationDate = Date()
}
.onReceive(NotificationCenter.default.publisher(for: WKExtension.applicationDidBecomeActiveNotification)) { _ in
print("Moving to the foreground")
let deltaTime: Int = Int(Date().timeIntervalSince(notificationDate))
timeRemaining -= deltaTime
percent += Double(deltaTime) / 1.2
}
For the notification, I used a simple test notification with a 5 second timer.
It works perfectly when I quit the app before the 5 sec countdown, but not when I stay in the app with a wrist down (to put the watch in sleep mode)
func scheduleNotification() {
let content = UNMutableNotificationContent()
content.title = "Rest time is over"
content.subtitle = "Be ready for the next set"
content.sound = UNNotificationSound.default
// show this notification five seconds from now
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
// choose a random identifier
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
// add our notification request
UNUserNotificationCenter.current().add(request)
}
Should I use something different in order to make this notification work even if I stay in the app in sleep mode?
I am not too familiar with the UserNotifications framework :(