Notifications

This guide explains how to receive a notification on the SmartWatch 2. Notifications in Smart on SmartWatch 2 are similar to standard Android notifications. They can be used to notify users when they have received an SMS, email, or Facebook update, for example.

Notification API

The Notification API is the API that is used to send and receive notifications on SmartWatch 2, and other Sony accessories. Not all accessories that support the SmartWatch 2 app framework can display notifications, so refer to the API compatibility table to ensure that the accessory you are targeting supports the Notification API.

Each accessory displays notifications in its own way. The rendering of the notification is done by the host application and only minor customizations can be done by the developer. You can control the content of what is displayed but not how it is displayed. If you require full control over the UI to display your notifications, you will need to use the Control API instead and implement your own notification handling.

SW2_notificationdiagram_400x189

Overview of notification handling.

The basics

To support notifications in your SmartWatch 2 app, perform the following steps:

  1. Register your app.
  2. Update your AndroidManifest.xml.
  3. Include the common classes.
  4. Add a notification source.
  5. Display the notification.

Register your app

First, you need to register your app by extending the RegistrationInformation class in the SmartExtensionUtil Android library. See the Registration guide for more details.

You will need to override the getRequiredNotificationApiVersion() method as follows:

@Override
public int getRequiredNotificationApiVersion() {
    return 1;
}
}

Update your AndroidManifest.xml

Next, you need to update your AndroidManifest.xml file. See the Android Manifest guide for more details on updating the Android Manifest for your app.

Include the common classes for Smart Extensions

Now, add the common classes for Smart Extensions in your project. See the Common classes guide for more details.

Add a notification source

Next, you need to add a notification source. The purpose of a source is to identify where notifications are coming from. It is possible to have notifications come from multiple sources, such as from multiple RSS feeds. If you plan to support multiple sources, you will need to create a ContentValue list of different source configurations and return all of them as an array as shown below:

@Override
    public ContentValues[] getSourceRegistrationConfigurations() {
        List bulkValues = new ArrayList();
        bulkValues
                .add(getSourceRegistrationConfiguration(SampleExtensionService.EXTENSION_SPECIFIC_ID));
        return bulkValues.toArray(new ContentValues[bulkValues.size()]);
    }

When you have registered your app to support the Notification API, the host app will call getSourceRegistrationConfiguration(). You will need to override this method and define the values of your notification source such as icon, menu actions, whether it supports text to speech. For more info on which values are supported, see the API References for Notification API.

Important to note is the EXTENSION_SPECIFIC_ID, which is a unique identifier for your app.

    /**
     * Get source configuration associated with apps specific id
     *
     * @param extensionSpecificId
     * @return The source configuration
     */
    public ContentValues getSourceRegistrationConfiguration(String extensionSpecificId) {
        ContentValues sourceValues = null;
        String iconSource1 = ExtensionUtils.getUriString(mContext,
                R.drawable.icn_30x30_message_notification);
        String textToSpeech = mContext.getString(R.string.text_to_speech);
        sourceValues = new ContentValues();
        sourceValues.put(Notification.SourceColumns.ENABLED, true);
        sourceValues.put(Notification.SourceColumns.ICON_URI_1, iconSource1);
        sourceValues.put(Notification.SourceColumns.UPDATE_TIME, System.currentTimeMillis());
        sourceValues.put(Notification.SourceColumns.NAME, mContext.getString(R.string.source_name));
        sourceValues.put(Notification.SourceColumns.EXTENSION_SPECIFIC_ID, extensionSpecificId);
        sourceValues.put(Notification.SourceColumns.PACKAGE_NAME, mContext.getPackageName());
        sourceValues.put(Notification.SourceColumns.TEXT_TO_SPEECH, textToSpeech);
// Menu actions and icons
        sourceValues.put(Notification.SourceColumns.ACTION_1,
                mContext.getString(R.string.action_event_1));
        sourceValues.put(Notification.SourceColumns.ACTION_2,
                mContext.getString(R.string.action_event_2));
        sourceValues.put(Notification.SourceColumns.ACTION_3,
                mContext.getString(R.string.action_event_3));
        sourceValues.put(Notification.SourceColumns.ACTION_ICON_1,
                ExtensionUtils.getUriString(mContext, R.drawable.actions_1));
        sourceValues.put(Notification.SourceColumns.ACTION_ICON_2,
                ExtensionUtils.getUriString(mContext, R.drawable.actions_2));
        sourceValues.put(Notification.SourceColumns.ACTION_ICON_3,
                ExtensionUtils.getUriString(mContext, R.drawable.actions_3));
        return sourceValues;
    }

Display the notification

To display the notification, you need to add it to the notification database. This is done by inserting it to the notification ContentResolver. Once it is added, the host application will push the notification out to the accessory to be displayed. Here, you can specify the title (DISPLAY_NAME) and message (MESSAGE) of the notification. These values are then added to a ContentValues object and then inserted into the database. Note that the values for PERSONAL, PUBLISHED_TIME, and SOURCE_ID are also required.

For details on the values supported, see the API References for Notification.EventColumns.

        String name = “Earth”;
        String message = “Hello World!”;
        long time = System.currentTimeMillis();
        long sourceId = NotificationUtil
                .getSourceId(this, EXTENSION_SPECIFIC_ID);
      …
        ContentValues eventValues = new ContentValues();
        eventValues.put(Notification.EventColumns.DISPLAY_NAME, name);
        eventValues.put(Notification.EventColumns.MESSAGE, message);
        eventValues.put(Notification.EventColumns.PERSONAL, 1);
        eventValues.put(Notification.EventColumns.PUBLISHED_TIME, time);
        eventValues.put(Notification.EventColumns.SOURCE_ID, sourceId);
…
getContentResolver().insert(Notification.Event.URI, eventValues);
…

Add an image to your notification

To add an image to your notification, such as image shown in the example below, you can add a  PROFILE_IMAGE_URI as part of your ContentValues object shown below:

...
    String profileImage = ExtensionUtils.getUriString(this,
                R.drawable.widget_default_userpic_bg);
...
                eventValues.put(Notification.EventColumns.PROFILE_IMAGE_URI, profileImage);
...
Example of an image added to a notification.

Example of an image added to a notification.

How to handle menu actions

You can define a menu to be shown in the notification view when the user taps the menu hard key on a supported accessory. To handle the menu actions, you will need to override the onViewEvent() method and add your action handling code.

@Override
    protected void onViewEvent(Intent intent) {
        String action = intent.getStringExtra(Notification.Intents.EXTRA_ACTION);
…
                if (Notification.SourceColumns.ACTION_1.equals(action)) {
            Toast.makeText(this, "Action 1", Toast.LENGTH_LONG).show();
        } else if (Notification.SourceColumns.ACTION_2.equals(action)) {
            Toast.makeText(this, "Action 2", Toast.LENGTH_LONG).show();
        } else if (Notification.SourceColumns.ACTION_3.equals(action)) {
            Toast.makeText(this, "Action 3", Toast.LENGTH_LONG).show();
        }
    }

Notification code example

For the full code example showing how to use notifications, see the SampleNotificationExtension project in the Sony Add-on SDK.