NotificationCompat.WearableExtender
public final class NotificationCompat.WearableExtender implements NotificationCompat.Extender
Helper class to add wearable extensions to notifications.
See Creating Notifications for Android Wear for more information on how to use this class.
To create a notification with wearable extensions:
- Create a
NotificationCompat.Builder, setting any desired properties. - Create a
NotificationCompat.WearableExtender. - Set wearable-specific properties using the
addandsetmethods ofNotificationCompat.WearableExtender. - Call
extendto apply the extensions to a notification. - Post the notification to the notification system with the
NotificationManagerCompat.notify(...)methods and not theNotificationManager.notify(...)methods.
Notification notification = new NotificationCompat.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.extend(new NotificationCompat.WearableExtender()
.setContentIcon(R.drawable.new_mail))
.build();
NotificationManagerCompat.from(mContext).notify(0, notification);Wearable extensions can be accessed on an existing notification by using the WearableExtender(Notification) constructor, and then using the get methods to access values.
NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender(notification);
List<Notification> pages = wearableExtender.getPages();Summary
Constants |
|
|---|---|
static final int |
SCREEN_TIMEOUT_LONG = -1This field is deprecated. This feature is no longer supported. |
static final int |
This field is deprecated. This feature is no longer supported. |
static final int |
SIZE_DEFAULT = 0This field is deprecated. Display intents are no longer supported. |
static final int |
SIZE_FULL_SCREEN = 5This field is deprecated. Display intents are no longer supported. |
static final int |
SIZE_LARGE = 4This field is deprecated. Display intents are no longer supported. |
static final int |
SIZE_MEDIUM = 3This field is deprecated. Display intents are no longer supported. |
static final int |
SIZE_SMALL = 2This field is deprecated. Display intents are no longer supported. |
static final int |
SIZE_XSMALL = 1This field is deprecated. Display intents are no longer supported. |
static final int |
UNSET_ACTION_INDEX = -1Sentinel value for an action index that is unset. |
Public constructors |
|---|
|
Create a |
WearableExtender(@NonNull Notification notification) |
Public methods |
|
|---|---|
@NonNull NotificationCompat.WearableExtender |
addAction(@NonNull NotificationCompat.Action action)Add a wearable action to this notification. |
@NonNull NotificationCompat.WearableExtender |
addActions(@NonNull List<NotificationCompat.Action> actions)Adds wearable actions to this notification. |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. Multiple content pages are no longer supported. |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. Multiple content pages are no longer supported. |
@NonNull NotificationCompat.WearableExtender |
Clear all wearable actions present on this builder. |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. Multiple content pages are no longer supported. |
@NonNull NotificationCompat.WearableExtender |
clone() |
@NonNull NotificationCompat.Builder |
extend(@NonNull NotificationCompat.Builder builder)Apply wearable extensions to a notification that is being built. |
@NonNull List<NotificationCompat.Action> |
Get the wearable actions present on this notification. |
@Nullable Bitmap |
This method is deprecated. Background images are no longer supported. |
@Nullable String |
Returns the bridge tag of the notification. |
int |
Get the index of the notification action, if any, that was specified as the primary action. |
int |
This method is deprecated. This method has no effect starting with Wear 2.0. |
int |
This method is deprecated. This method has no effect starting with Wear 2.0. |
boolean |
Get whether the content intent is available when the wearable device is not connected to a companion device. |
int |
This method is deprecated. This method has no effect starting with Wear 2.0. |
int |
This method is deprecated. This method has no effect starting with Wear 2.0. |
@Nullable String |
Returns the dismissal id of the notification. |
@Nullable PendingIntent |
This method is deprecated. Display intents are no longer supported. |
int |
This method is deprecated. This method has no effect starting with Wear 2.0. |
boolean |
This method is deprecated. This feature is no longer supported. |
boolean |
This method is deprecated. This method has no effect starting with Wear 2.0. |
boolean |
Get a hint that this notification's content intent will launch an |
boolean |
This method is deprecated. This method has no effect starting with Wear 2.0. |
int |
This method is deprecated. This method has no effect starting with Wear 2.0. |
boolean |
This method is deprecated. This method has no effect starting with Wear 2.0. |
@NonNull List<Notification> |
This method is deprecated. Multiple content pages are no longer supported. |
boolean |
Get whether the scrolling position for the contents of this notification should start at the bottom of the contents instead of the top when the contents are too long to display within the screen. |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. Background images are no longer supported. |
@NonNull NotificationCompat.WearableExtender |
setBridgeTag(@Nullable String bridgeTag)Sets a bridge tag for this notification. |
@NonNull NotificationCompat.WearableExtender |
setContentAction(int actionIndex)Set an action from this notification's actions as the primary action. |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. This method has no effect starting with Wear 2.0. |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. This method has no effect starting with Wear 2.0. |
@NonNull NotificationCompat.WearableExtender |
setContentIntentAvailableOffline(Set whether the content intent is available when the wearable device is not connected to a companion device. |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. This method has no effect starting with Wear 2.0. |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. This method has no effect starting with Wear 2.0. |
@NonNull NotificationCompat.WearableExtender |
setDismissalId(@Nullable String dismissalId)Sets the dismissal id for this notification. |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. Display intents are no longer supported. |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. This method has no effect starting with Wear 2.0. |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. This feature is no longer supported. |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. This method has no effect starting with Wear 2.0. |
@NonNull NotificationCompat.WearableExtender |
setHintContentIntentLaunchesActivity(Set a hint that this notification's content intent will launch an |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. This method has no effect starting with Wear 2.0. |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. This method has no effect. |
@NonNull NotificationCompat.WearableExtender |
This method is deprecated. This method has no effect starting with Wear 2.0. |
@NonNull NotificationCompat.WearableExtender |
setStartScrollBottom(boolean startScrollBottom)Set whether the scrolling position for the contents of this notification should start at the bottom of the contents instead of the top when the contents are too long to display within the screen. |
Constants
public static final int SCREEN_TIMEOUT_LONG = -1
Sentinel value for use with setHintScreenTimeout to keep the screen on for a longer amount of time when this notification is displayed on the screen.
public static final int SCREEN_TIMEOUT_SHORT = 0
Sentinel value for use with setHintScreenTimeout to keep the screen on for a short amount of time when this notification is displayed on the screen. This is the default value.
public static final int SIZE_DEFAULT = 0
Size value for use with setCustomSizePreset to show this notification with default sizing.
For custom display notifications created using setDisplayIntent, the default is SIZE_MEDIUM. All other notifications size automatically based on their content.
public static final int SIZE_FULL_SCREEN = 5
Size value for use with setCustomSizePreset to show this notification full screen.
This value is only applicable for custom display notifications created using setDisplayIntent.
public static final int SIZE_LARGE = 4
Size value for use with setCustomSizePreset to show this notification with a large size.
This value is only applicable for custom display notifications created using setDisplayIntent.
public static final int SIZE_MEDIUM = 3
Size value for use with setCustomSizePreset to show this notification with a medium size.
This value is only applicable for custom display notifications created using setDisplayIntent.
public static final int SIZE_SMALL = 2
Size value for use with setCustomSizePreset to show this notification with a small size.
This value is only applicable for custom display notifications created using setDisplayIntent.
public static final int SIZE_XSMALL = 1
Size value for use with setCustomSizePreset to show this notification with an extra small size.
This value is only applicable for custom display notifications created using setDisplayIntent.
UNSET_ACTION_INDEX
public static final int UNSET_ACTION_INDEX = -1
Sentinel value for an action index that is unset.
Public constructors
WearableExtender
public WearableExtender()
Create a NotificationCompat.WearableExtender with default options.
Public methods
addAction
public @NonNull NotificationCompat.WearableExtender addAction(@NonNull NotificationCompat.Action action)
Add a wearable action to this notification.
When wearable actions are added using this method, the set of actions that show on a wearable device splits from devices that only show actions added using addAction. This allows for customization of which actions display on different devices.
| Parameters | |
|---|---|
@NonNull NotificationCompat.Action action |
the action to add to this notification |
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining |
| See also | |
|---|---|
NotificationCompat.Action |
addActions
public @NonNull NotificationCompat.WearableExtender addActions(@NonNull List<NotificationCompat.Action> actions)
Adds wearable actions to this notification.
When wearable actions are added using this method, the set of actions that show on a wearable device splits from devices that only show actions added using addAction. This allows for customization of which actions display on different devices.
| Parameters | |
|---|---|
@NonNull List<NotificationCompat.Action> actions |
the actions to add to this notification |
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining |
| See also | |
|---|---|
NotificationCompat.Action |
public @NonNull NotificationCompat.WearableExtenderaddPage(@NonNull Notification page)
Add an additional page of content to display with this notification. The current notification forms the first page, and pages added using this function form subsequent pages. This field can be used to separate a notification into multiple sections.
| Parameters | |
|---|---|
@NonNull Notification page |
the notification to add as another page |
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining |
| See also | |
|---|---|
getPages |
public @NonNull NotificationCompat.WearableExtenderaddPages(@NonNull List<Notification> pages)
Add additional pages of content to display with this notification. The current notification forms the first page, and pages added using this function form subsequent pages. This field can be used to separate a notification into multiple sections.
| Parameters | |
|---|---|
@NonNull List<Notification> pages |
a list of notifications |
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining |
| See also | |
|---|---|
getPages |
clearActions
public @NonNull NotificationCompat.WearableExtender clearActions()
Clear all wearable actions present on this builder.
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining. |
| See also | |
|---|---|
addAction |
public @NonNull NotificationCompat.WearableExtenderclearPages()
Clear all additional pages present on this builder.
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining. |
| See also | |
|---|---|
addPage |
extend
public @NonNull NotificationCompat.Builder extend(@NonNull NotificationCompat.Builder builder)
Apply wearable extensions to a notification that is being built. This is typically called by the extend method of NotificationCompat.Builder.
getActions
public @NonNull List<NotificationCompat.Action> getActions()
Get the wearable actions present on this notification.
public @Nullable BitmapgetBackground()
Get a background image to be displayed behind the notification content. Contrary to the NotificationCompat.BigPictureStyle, this background will work with any notification style.
| See also | |
|---|---|
setBackground |
getBridgeTag
public @Nullable String getBridgeTag()
Returns the bridge tag of the notification.
getContentAction
public int getContentAction()
Get the index of the notification action, if any, that was specified as the primary action.
If wearable specific actions were added to the main notification, this index will apply to that list, otherwise it will apply to the regular actions list.
| Returns | |
|---|---|
int |
the action index or |
public int getContentIconGravity()Get the gravity that the content icon should have within the notification display. Supported values include START and END. The default value is END.
| See also | |
|---|---|
getContentIcon |
getContentIntentAvailableOffline
public boolean getContentIntentAvailableOffline()
Get whether the content intent is available when the wearable device is not connected to a companion device. The user can still trigger this intent when the wearable device is offline, but a visual hint will indicate that the content intent may not be available. Defaults to true.
public int getCustomContentHeight()Get the custom height in pixels for the display of this notification's content.
This option is only available for custom display notifications created using setDisplayIntent. See also setCustomSizePreset and setCustomContentHeight.
public int getCustomSizePreset()Get the custom size preset for the display of this notification out of the available presets found in NotificationCompat.WearableExtender, e.g. SIZE_LARGE.
Some custom size presets are only applicable for custom display notifications created using setDisplayIntent. Check the documentation for the preset in question. See also setCustomContentHeight and setCustomSizePreset.
getDismissalId
public @Nullable String getDismissalId()
Returns the dismissal id of the notification.
public @Nullable PendingIntentgetDisplayIntent()
Get the intent to launch inside of an activity view when displaying this notification. This PendingIntent should be for an activity.
public int getGravity()Get the gravity that this notification should have within the available viewport space. Supported values include TOP, CENTER_VERTICAL and BOTTOM. The default value is BOTTOM.
public boolean getHintAmbientBigPicture()Get a hint that this notification's BigPictureStyle (if present) should be converted to low-bit and displayed in ambient mode, especially useful for barcodes and qr codes, as well as other simple black-and-white tickets.
| Returns | |
|---|---|
boolean |
|
public boolean getHintAvoidBackgroundClipping()Get a hint that this notification's background should not be clipped if possible, and should instead be resized to fully display on the screen, retaining the aspect ratio of the image. This can be useful for images like barcodes or qr codes.
| Returns | |
|---|---|
boolean |
|
getHintContentIntentLaunchesActivity
public boolean getHintContentIntentLaunchesActivity()
Get a hint that this notification's content intent will launch an Activity directly, telling the platform that it can generate the appropriate transitions
| Returns | |
|---|---|
boolean |
|
public boolean getHintHideIcon()Get a hint that this notification's icon should not be displayed.
| Returns | |
|---|---|
boolean |
|
public int getHintScreenTimeout()Get the duration, in milliseconds, that the screen should remain on for when this notification is displayed.
| Returns | |
|---|---|
int |
the duration in milliseconds if >0, or either one of the sentinel values |
public boolean getHintShowBackgroundOnly()Get a visual hint that only the background image of this notification should be displayed, and other semantic content should be hidden. This hint is only applicable to sub-pages added using addPage.
public @NonNull List<Notification>getPages()
Get the array of additional pages of content for displaying this notification. The current notification forms the first page, and elements within this array form subsequent pages. This field can be used to separate a notification into multiple sections.
| Returns | |
|---|---|
@NonNull List<Notification> |
the pages for this notification |
getStartScrollBottom
public boolean getStartScrollBottom()
Get whether the scrolling position for the contents of this notification should start at the bottom of the contents instead of the top when the contents are too long to display within the screen. Default is false (start scroll at the top).
public @NonNull NotificationCompat.WearableExtendersetBackground(@Nullable Bitmap background)
Set a background image to be displayed behind the notification content. Contrary to the NotificationCompat.BigPictureStyle, this background will work with any notification style.
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining |
| See also | |
|---|---|
getBackground |
setBridgeTag
public @NonNull NotificationCompat.WearableExtender setBridgeTag(@Nullable String bridgeTag)
Sets a bridge tag for this notification. A bridge tag can be set for notifications posted from a phone to provide finer-grained control on what notifications are bridged to wearables. See Adding Wearable Features to Notifications for more information.
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining |
setContentAction
public @NonNull NotificationCompat.WearableExtender setContentAction(int actionIndex)
Set an action from this notification's actions as the primary action. If the action has a RemoteInput associated with it, shortcuts to the options for that input are shown directly on the notification.
| Parameters | |
|---|---|
int actionIndex |
The index of the primary action. If wearable actions were added to the main notification, this index will apply to that list, otherwise it will apply to the regular actions list. |
public @NonNull NotificationCompat.WearableExtendersetContentIcon(int icon)
Set an icon that goes with the content of this notification.
public @NonNull NotificationCompat.WearableExtendersetContentIconGravity(int contentIconGravity)
Set the gravity that the content icon should have within the notification display. Supported values include START and END. The default value is END.
| See also | |
|---|---|
setContentIcon |
setContentIntentAvailableOffline
public @NonNull NotificationCompat.WearableExtender setContentIntentAvailableOffline(
boolean contentIntentAvailableOffline
)
Set whether the content intent is available when the wearable device is not connected to a companion device. The user can still trigger this intent when the wearable device is offline, but a visual hint will indicate that the content intent may not be available. Defaults to true.
public @NonNull NotificationCompat.WearableExtendersetCustomContentHeight(int height)
Set the custom height in pixels for the display of this notification's content.
This option is only available for custom display notifications created using setDisplayIntent. See also setCustomSizePreset and getCustomContentHeight.
public @NonNull NotificationCompat.WearableExtendersetCustomSizePreset(int sizePreset)
Set the custom size preset for the display of this notification out of the available presets found in NotificationCompat.WearableExtender, e.g. SIZE_LARGE.
Some custom size presets are only applicable for custom display notifications created using setDisplayIntent. Check the documentation for the preset in question. See also setCustomContentHeight and getCustomSizePreset.
setDismissalId
public @NonNull NotificationCompat.WearableExtender setDismissalId(@Nullable String dismissalId)
Sets the dismissal id for this notification. If a notification is posted with a dismissal id, then when that notification is canceled, notifications on other wearables and the paired Android phone having that same dismissal id will also be canceled. See Adding Wearable Features to Notifications for more information.
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining |
public @NonNull NotificationCompat.WearableExtendersetDisplayIntent(@Nullable PendingIntent intent)
Set an intent to launch inside of an activity view when displaying this notification. The PendingIntent provided should be for an activity.
Intent displayIntent = new Intent(context, MyDisplayActivity.class); PendingIntent displayPendingIntent = PendingIntent.getActivity(context, 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT); Notification notification = new NotificationCompat.Builder(context) .extend(new NotificationCompat.WearableExtender() .setDisplayIntent(displayPendingIntent) .setCustomSizePreset(NotificationCompat.WearableExtender.SIZE_MEDIUM)) .build();
The activity to launch needs to allow embedding, must be exported, and should have an empty task affinity. It is also recommended to use the device default light theme.
Example AndroidManifest.xml entry:
<activity android:name="com.example.MyDisplayActivity" android:exported="true" android:allowEmbedded="true" android:taskAffinity="" android:theme="@android:style/Theme.DeviceDefault.Light" />
| Parameters | |
|---|---|
@Nullable PendingIntent intent |
the |
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining |
| See also | |
|---|---|
getDisplayIntent |
public @NonNull NotificationCompat.WearableExtendersetGravity(int gravity)
Set the gravity that this notification should have within the available viewport space. Supported values include TOP, CENTER_VERTICAL and BOTTOM. The default value is BOTTOM.
public @NonNull NotificationCompat.WearableExtendersetHintAmbientBigPicture(boolean hintAmbientBigPicture)
Set a hint that this notification's BigPictureStyle (if present) should be converted to low-bit and displayed in ambient mode, especially useful for barcodes and qr codes, as well as other simple black-and-white tickets.
| Parameters | |
|---|---|
boolean hintAmbientBigPicture |
|
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining |
public @NonNull NotificationCompat.WearableExtendersetHintAvoidBackgroundClipping(boolean hintAvoidBackgroundClipping)
Set a hint that this notification's background should not be clipped if possible, and should instead be resized to fully display on the screen, retaining the aspect ratio of the image. This can be useful for images like barcodes or qr codes.
| Parameters | |
|---|---|
boolean hintAvoidBackgroundClipping |
|
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining |
setHintContentIntentLaunchesActivity
public @NonNull NotificationCompat.WearableExtender setHintContentIntentLaunchesActivity(
boolean hintContentIntentLaunchesActivity
)
Set a hint that this notification's content intent will launch an Activity directly, telling the platform that it can generate the appropriate transitions.
| Parameters | |
|---|---|
boolean hintContentIntentLaunchesActivity |
|
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining |
public @NonNull NotificationCompat.WearableExtendersetHintHideIcon(boolean hintHideIcon)
Set a hint that this notification's icon should not be displayed.
| Parameters | |
|---|---|
boolean hintHideIcon |
|
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining |
public @NonNull NotificationCompat.WearableExtendersetHintScreenTimeout(int timeout)
Set a hint that the screen should remain on for at least this duration when this notification is displayed on the screen.
| Parameters | |
|---|---|
int timeout |
The requested screen timeout in milliseconds. Can also be either |
| Returns | |
|---|---|
@NonNull NotificationCompat.WearableExtender |
this object for method chaining |
public @NonNull NotificationCompat.WearableExtendersetHintShowBackgroundOnly(boolean hintShowBackgroundOnly)
Set a visual hint that only the background image of this notification should be displayed, and other semantic content should be hidden. This hint is only applicable to sub-pages added using addPage.
setStartScrollBottom
public @NonNull NotificationCompat.WearableExtender setStartScrollBottom(boolean startScrollBottom)
Set whether the scrolling position for the contents of this notification should start at the bottom of the contents instead of the top when the contents are too long to display within the screen. Default is false (start scroll at the top).