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])
}