swiftwatchkituilocalnotificationapple-watchwatchos-2

How to trigger haptic alert when inactive watch receives local notification


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

Solution

  • Where notifications are shown

    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...)

    That's correct. It's up to iOS to determine where to show the notification.

    When your local notification fires in the future, it may be shown on the phone. In that case, the watch won't receive any notification at all.

    When the notification is handled by the watch

    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)

    This is correct. You're expecting didReceiveLocalNotification to fire on the watch, but this only occurs when the watch is activated:

    If a local notification arrives while your app is active, WatchKit calls this method to deliver the notification payload. Use this method to respond to the notification.

    This is the reason why your haptic feedback doesn't happen when you expect, as the call to play the haptic feedback doesn't occur until later.

    A workaround for your approach

    You can conditionally play your haptic by first examining the local notification's fireDate. Only play your haptic feedback if the notification handling just triggered.

    A better solution

    It appears that you're trying to handle playing a haptic independent of the default feedback which already occurs when watchOS displays the notification for you.

    You may want to skip your extension from playing its own haptic feedback, as the user has already been notified by the system.

    Make a feature request

    The real issue you're trying to work around is that only Apple's timer app can schedule its own local notifications.

    If you haven't already, you may want to submit a feature request asking the Apple let third-party developers post local notifications on watchOS.