I am working on a timer app for the Apple Watch.
At a specific time (end of timer) the iPhone fires a scheduled UILocalNotification
which on the inactive Apple Watch should trigger a haptic alert to tell the user that the timer has ended.
I've done this since timers don't continue to run on Apple Watch once the watch is inactive.
The problem I am facing is:
a) the notification is sometimes shown on the iPhone rather than on the apple watch (based on other posts, I fear that this cannot be changed...)
b) no haptic alert is given on the watch (purely display of notification and this only at next time when the user is activating the watch, not at the time when the notification is actually triggered)
func sendAwakeNotification(nextTime:NSDate) {
print("AppDelegate: - sendAwakeNotification")
dispatch_async(dispatch_get_main_queue()) { () -> Void in
let localNotification = UILocalNotification()
localNotification.fireDate = nextTime
localNotification.timeZone = NSTimeZone.defaultTimeZone()
localNotification.alertBody = "Finished Step"
localNotification.alertTitle = "Alert"
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.category = "myTimer"
let userInfo: [NSObject : AnyObject] = [
"notification_id" : "myTimerNotification"
]
localNotification.userInfo = userInfo
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
}
The code in Notification
override func didReceiveLocalNotification(localNotification: UILocalNotification, withCompletion completionHandler: ((WKUserNotificationInterfaceType) -> Void)) {
print("received notification")
dispatch_async(dispatch_get_main_queue()) { () -> Void in
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.postNotificationName(NotificationAlertFromPhone, object: nil)
}
completionHandler(.Custom)
}
I've tried to trigger a haptic alert directly in NotificationController (WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.Notification))
, but it did not work. I therefore reverted to send notification which should be received by ExtensionDelegate to trigger the haptic.
Here is the code in ExtensionDelegate:
private func setupNotificationCenter() {
print("ExtensionDelegate: - setupNotificationCenter")
notificationCenter.addObserverForName(NotificationAlertFromPhone, object: nil, queue: nil) { (notification:NSNotification) -> Void in
WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.Notification)
}
}