0

I've been struggling with this for days and am desperate for some help. I am using Swift 4 and WatchOS 4. I have created a simple timer app which once the timer is up I send a local notification to alert me that the time is up. I also have started a workoutSession in the app as this is the only way to have the notification work at all. So on the app I start the timer which counts down from 5min. I lower my wrist which makes the screen sleep. (I'm not using awake on wrist raise). When the timer is up I get the haptic notification but the screen stays blank. If I then tap the screen the notification appears.

Why does it not show the notification on the screen without me having to tap on the watch after hearing/feeling the alert?

Timer start code:

intervalTimer = Timer.scheduledTimer(timeInterval: 300, target: self, selector: #selector (self.timerDidEnd), userInfo: nil, repeats: false)
    WKInterfaceDevice.current().play(.start)

Timer Did End func with Notification Request:

@objc func timerDidEnd() {
        if self.intervalTimer.isValid{self.intervalTimer.invalidate()}
    let content = UNMutableNotificationContent()
    content.title = "Timer Up!"
    content.body = "Do Something"
    content.sound = UNNotificationSound.default() // This is optional
    content.setValue("YES", forKeyPath: "shouldAlwaysAlertWhileAppIsForeground")

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
    content.categoryIdentifier = "myCategory"
    let request = UNNotificationRequest(identifier: NSUUID().uuidString,
                                        content: content,
                                        trigger: trigger)
    let center = UNUserNotificationCenter.current()

    center.add(request) { (error) in
        if error != nil {
            print(error!)
        } else {
            print("notification: ok")
        }
    }
    }

In the Extension Delegate:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert, .sound])
}
TazmanNZL
  • 372
  • 3
  • 16
  • You don't mention it, but have you elsewhere performed the requestAuthorizationWithOptions? Docs says this is required to interact with the user, although it also says this is required to play sounds. It doesn't mention haptic though. Also, timers will not run in background, so if you are planning to stop using the workout, you will not be able to use them in the future. – Cobra Feb 19 '18 at 02:01
  • Yes I have that method in the Extension Delegate. The haptic comes from the alert notification: completionHandler([.alert, .sound]). If I don't drop my wrist and keep the watch screen awake whether I'm in the app or on the clock face the notification appears. Apple documentation states that it wakes the app but it doesn't mention anything about waking the watch/screen. – TazmanNZL Feb 19 '18 at 06:34

1 Answers1

0

As it turns out. If not using "wake screen on wrist raise" you have to tap the screen before the notification will show on screen. You still get the alert in way of haptic feedback and audible alert.

When having "wake screen on wrist raise" enabled the notification does appear on screen because you have already woken the screen by raising the wrist.

TazmanNZL
  • 372
  • 3
  • 16