How To Trigger An Apple Watch Haptic Feedback (or Notification) From An Active App After The Screen Just Turns Off?
I'm working on a standalone countdown App for Apple Watch. When the timer is over, the watch rings or vibrate.
There are three different situations where this can happen:
- The app screen is active on the user's wrist: that's the easiest part; when the timer is over, the app runs a sound/haptic feedback, no need for anything running in the background.No problem with that.
- The app is in the background: Using notifications seems to be the obvious choice, and it already works very well.
- The app is in the foreground, but the screen turns off either because it reached the maximum wake duration (15 or 70 seconds) or because the user rotated his wrist back in rest position, which automatically turns the apple watch's screen off.
This third and last situation is where I'm confused. Neither the first nor the second situation works here: when the app is active, but the screen turns off, the app stops running, but the notifications are still not triggered since the app is technically not in the background.
Are there any straightforward ways around this problem?
Use Extended Runtime Sessions to create
func notifyUser(hapticType type: WKHapticType, repeatHandler: ((UnsafeMutablePointer<WKHapticType>) -> Time Interval)? = nil)
For schedulable sessions such as smart alarms, call this method during the session to alert the user. When you call the method, the system plays repeating haptic feedback. If the app isn’t active, the system also displays a system alarm alert on the watch.
The haptic feedback repeats at the interval specified by the repeatHandler, and continues to repeat until the application or system alert invalidates the session.
If the app isn’t active, the user can tap the Stop button to invalidate the session or tap the Open button to activate the app.
If the app is active, the app must invalidate the session by calling its invalidate method.
Only call this method on a schedulable session that’s running: you must schedule the session using the startAtDate: method, and the session’s state must equal WKExtendedRuntimeSessionStateRunning. During a smart alarm session, your app must call this method before the session expires.
Also you should check out the life cycle of watchOS app
- → Hi, How do I control (minimize) the gap between the search Bar and the two text parts above? Is my code optimal or can I combine text parts with nav?
- → Prevent Text("string") from being exported as localization
- → Is there any way to turn off autocorrection / set keyboard type on the searchable navigation modifier in SwiftUI
- → Updating a path before Animating it in SwiftUI
- → How do I execute a function on ClassB when something changes in ClassA?
- → How to add tooltip to Map Annotation in order to show the location name on the Map using MapKit (SwiftUI)
- → SwiftUI: On tap gesture switches views
- → Multiple NavigationView in a screen
- → Swift snapshot and mapping a single document from Firestore
- → Initialization of a nested struct in SwiftUI
- → Turning function that has a combination of publishers/sinks into an async function
- → KeyNotFound(CodinkgKeys) when Fetching API SwiftUI
- → Showing Overlay Aligning to the Bottom