Responding to Notifications

The extension delegate also handles local and remote notifications for watchOS apps. The methods to implement in the protocol mirror those for responding to notifications on iOS and for good reason—one of the primary use cases of the Apple Watch is to act as a conduit for iOS app notifications—whether or not the app has an Apple Watch--specific app. When the user’s iPhone is locked and the Apple Watch is unlocked, all notifications are routed to the watch by default. The real power of notifications on the watch comes with actions associated with these notifications; as you can see in this screenshot, a notification can include buttons that perform actions when the user selects them. When a notification comes in, you use the extension delegate to handle the user’s interaction with it on the watch.

images/NotificationActionsExample.png

The four methods available for handling notifications that come in when your app isn’t running are all very similar. For instance, handleActionWithIdentifier(_:forRemoteNotification:withResponseInfo:) is called for remote notifications that include an optional text reply. The first parameter is a string identifier for the notification button the user tapped—something like Accept for a calendar invitation. The remoteNotification parameter contains the notification information from the remote server, and the responseInfo parameter can optionally contain text from the user—such as a reply to a text message. The other three methods like this one are simple variations on a theme: exchange “Local” for “Remote” when dealing with local notifications, and omit the “withResponseInfo:” portion for notifications without extra info. When implementing these methods, you simply perform the action the user selected, using the text entered if needed. Handling notifications this way is a great tool to allow users to do things quickly.

Of course, the user may be using the app when a notification comes in. Perhaps your app allows the user to start a potentially long-running task, like preheating an oven, and the server sends a push notification when it’s done. If the user is currently using the app, the extension delegate’s didReceiveRemoteNotification(_:) method is called, with a userInfo dictionary containing the notification payload. There’s a corresponding local notification version as well. In this method, it’s best to present the user with the information from the notification—or at least update the app’s UI.

There may not be much to notifications in watchOS, but they’re incredibly useful on the device. Since it’s always with the user, the watch is the perfect device to make quick decisions on notifications. Enabling users to perform quick tasks with the tap of a button on their wrist can save them valuable time and make your app indispensable in their workflow.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.137.252