Using a timer when the watch goes in sleep mode

I am trying to use a timer for an exercise app on Apple Watch (timer used for resting time after an exercise) but this timer stops when the watch goes in sleep mode (triggered by a wrist down). This is very unpractical as the user does the wrist down naturally during resting and the timer is stopped.
I am using a basic timer with publish and on receive:

Code Block Swift
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()


Code Block Swift
.onReceive(timer) { _ in
if timeRemaining > -1 {
timeRemaining -= 1
}


but it does not work whenever the user does a wrist down.
Can you please let me know how should I use the timer in order to work even in sleep mode? I see that the Apple Exercise app timer works even in sleep mode. Therefore, that should be doable.

Thank you in advance for your help


For the count down, I used this code and it solves the problem perfectly.

Code Block Swift
.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
}


But when the count down reaches 0, I need to provide a sort of Haptic Feedback to the user in order to tell him/her that the resting time is over. I have an issue when the watch is in sleep mode (wrist down).

I am thinking on using User Notification for this purpose.
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)

How should I use local notification to work even if I stay in the app in sleep mode?
I am not too familiar with the UserNotifications framework :(

Thanks for your feedback
Using a timer when the watch goes in sleep mode
 
 
Q