Ad

How To Trigger An Apple Watch Haptic Feedback (or Notification) From An Active App After The Screen Just Turns Off?

- 1 answer

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:

  1. 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.
  2. The app is in the background: Using notifications seems to be the obvious choice, and it already works very well.
  3. 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?

Ad

Answer

Use Extended Runtime Sessions to create smart alarms

like this:

func notifyUser(hapticType type: WKHapticType, 
  repeatHandler: ((UnsafeMutablePointer<WKHapticType>) -> Time
Interval)? = nil)

Apple Documentation

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

enter image description here

Ad
source: stackoverflow.com
Ad