Interactive Notifications for iOS and Apple Watch

Interactive Notifications for iOS and Apple Watch

Titanium & Apple Watch

Titanium 4.1 already supports linking a WatchKit Extension built in Xcode, and our upcoming September 2015 release adds support for linking and communicating with Watch OS 2 apps built using Swift. Of course we are also working on the ability to build an actual Watch OS 2 app in JavaScript using Titanium. But today I will show how you can support the Watch without any of that using Titanium 3.4.0 or later.

Interactive Notifications

What I personally use most on the Apple Watch are interactive notifications. Third party apps often are painfully slow, although I assume that will improve once they are able to run on the watch with Watch OS 2.

Interactive notifications have been available for phone and tablet since iOS 8 and Titanium 3.4.0, but proved to be even more useful on the Watch. Getting a notification about an incoming message is fine, but if I don’t have to grab my phone to reply we are really talking — literally! I’d still rather use my phone instead of having to type or dictate on my watch, but interactive notifications are great for selecting a quick reply or actions like Mark as Read.

Sample App

I’ve built a sample messenger app to show you how it works. It’s a simple WhatsApp-like messaging app. Every time you send a message, a pretty dumb bot will reply with a random answer after 4 seconds. This allows you to move the app to the background or lock your phone to see what happens in different scenarios.

Registering categories

A category may define two sets of actions. One of 1-4 actions for alert-style and Apple Watch notifications and another of 1-2 actions for other styles. If you don’t set the latter it will simply take the first two of the first.

NOTE: Apple Watch will only display actions that can be processed in the background.

Registering actions

Apart from an identifier and title there’s a few things you can set:

activationMode: If set to Ti.App.iOS.USER_NOTIFICATION_ACTIVATION_MODE_BACKGROUND it will just let the app execute the action without opening the app for the user.

authenticationRequired: If set to true and only when the user is not selecting the action via his watch, he must authenticate first, even if the device is not locked.

destructive: Will display the action in red if presented on the lock screen or watch.

NOTE: Using authenticationRequired:false and activationMode:Ti.App.iOS.USER_NOTIFICATION_ACTIVATION_MODE_FOREGROUND together will still require authentication if the device is locked.

Receiving local notifications

The notification event is fired for both regular and interactive notifications when the app is opened via a local notification, but only when the user has not selected an action. It also fires when the app was in the foreground while receiving the notification. There’s no way to tell the difference, but in both cases we want to present the message to the user so he can respond to it in the app.

NOTE: When the app is in foreground iOS will never show the notification to the user. It expects the app to handle it.

A separate localnotificationaction event is fired when the user does select an action. It will have the notification payload as well as the category and selected action identifiers. You can see how we handle this around line ~190. Since we have push notifications as well, I choose to use an event dispatcher created in alloy.js and trigger a custom action event with the required payload. I’ll listen to this event in the console and chat controllers.

Then finally there’s some logic to update the app and tab badges and mark messages as read when you see or reply to them.

See the comments throughout the code for more details.

Bonus: Handoff

As a bonus, viewing a notification on your Watch automatically triggers handoff for the Titanium app. Your app icon will show in the bottom left of the lock screen and swiping up will open the app. However, consecutive tests using the sample didn’t always get the same results for me and only in Titanium 5.0 and later will you be able to listen to the handoff event in your app.

Follow Us

What can we help you with today?

Let us know how we can help you today! We're ready to get down to business. To contact our sales click here or fill out the form below: Need support? Log in to our support portal for assistance.

Axway respects your privacy. Your personal data will not be shared with or sold to a third party (unless to Axway legal entities where you can find the list at "Contact us"). Please note that you can withdraw your consent at any time by clicking here.