Tuesday, May 31, 2016

Creating Actionable Notifications on Apple Watch

In iOS 8 and later, apps are required to register the types of notification-generated alerts they display using a UIUserNotificationSettings object. When registering that information, the app can also register a set of custom notification categories, which include the actions that can be performed for that category. Apple Watch uses this category information to add the corresponding action buttons to the long-look interface.

Support for text input is new in watchOS 2 and iOS 9. When enabled, text input lets the user respond to the notification with a short message. On Apple Watch, the user can either dictate a text response or select from a set of predefined messages. to enable the text input, we need to add the UIUserNotificationActionBehaviorTextInput value to the behaviour property of the UIUserNotificationAction that we create in iOS. 

Below given some details on how to respond to the user actions

When the user taps an action button for a notification, the system uses the information in the registered UIUserNotificationAction object to determine how to process the action. Actions can be processed in the foreground or the background. Foreground and background actions are processed differently:

Foreground actions launch your Watch app and deliver the ID of the tapped button to your extension delegate’s notification methods.
For remote notifications, the WatchKit extension calls the handleActionWithIdentifier:forRemoteNotification: or handleActionWithIdentifier:forRemoteNotification:withResponseInfo: method.
For local notifications, the WatchKit extension calls the handleActionWithIdentifier:forLocalNotification: or handleActionWithIdentifier:forLocalNotification:withResponseInfo: method.

Background actions launch the iOS app in the background so that it can process the action. Information about the selected action is delivered to the app delegate’s notification methods.

For remote notifications, iOS calls the application:handleActionWithIdentifier:forRemoteNotification:completionHandler: or application:handleActionWithIdentifier:forRemoteNotification:withResponseInfo:completionHandler: method.
For local notifications, iOS calls the application:handleActionWithIdentifier:forLocalNotification:completionHandler: or application:handleActionWithIdentifier:forRemoteNotification:withResponseInfo:completionHandler: method.

Selecting the foreground action launches the app to handle the action. Your app’s extension delegate must implement the appropriate methods to process actions.

The specific method called by watchOS depends on whether the action included a text response from the user. Text input triggers the method that includes the responseInfo parameter. Action buttons trigger the method without responseInfo. 

The user’s string is passed inside the responseInfo dictionary. You can access the string using the UIUserNotificationActionResponseTypedTextKey key, and then incorporate that string into any actions that handle the notification. For example, if the user declined a meeting invite, you can add the text response to the invite so that the originator knows why the user declined.

To provide suggested responses for the text input, override the notification interface controller’s suggestionsForResponseToActionWithIdentifier:forRemoteNotification:inputLanguage: or suggestionsForResponseToActionWithIdentifier:forLocalNotification:inputLanguage: method and return an array of strings. watchOS presents these strings as suggestions.

If a notification arrives while the user is interacting with your Watch app, watchOS delivers the notification to the didReceiveRemoteNotification: or didReceiveLocalNotification: method of your extension delegate. You can use these methods to refresh your interface or take other actions based on the data in the notification.


references:

No comments:

Post a Comment