NotificationCompat.ProjectedExtender


public final class NotificationCompat.ProjectedExtender implements NotificationCompat.Extender


Helper class to add projection-specific extensions to a notification. This class is used to specify metadata that is specific to notifications that may be displayed on a Projected device.

To create a notification with Projected extensions:

  1. Create a NotificationCompat.Builder for the notification.
  2. Create a ProjectedExtender.
  3. Set projection-specific properties using the add and set methods on the ProjectedExtender.
  4. Call extend to apply the extensions to the notification.
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
    .setContentTitle("Example Title")
    .setContentText("Example Text")
    .setSmallIcon(R.drawable.ic_notification);

ProjectedExtender projectedExtender = new ProjectedExtender()
    .setContentIntent(projectedTapIntent)
    .addAction(projectedAction);

builder.extend(projectedExtender);
notificationManager.notify(NOTIFICATION_ID, builder.build());

Projected extensions can be accessed on an existing notification by using the ProjectedExtender(Notification) constructor, and then using the get methods to access values.

NotificationCompat.ProjectedExtender projectedExtender =
    new NotificationCompat.ProjectedExtender(notification);
List<NotificationCompat.Action> actions = projectedExtender.getActions();

Summary

Public constructors

Create a ProjectedExtender with default options.

Creates an ProjectedExtender from the extensions in a Notification.

Public methods

@NonNull NotificationCompat.ProjectedExtender

Add an action to this notification on projected devices.

@NonNull NotificationCompat.ProjectedExtender

Adds multiple actions to this notification on projected devices.

@NonNull NotificationCompat.ProjectedExtender

Clear all projected actions from this extender and reset shouldUseExtenderActions to false.

@NonNull NotificationCompat.Builder

Applies the Projected extensions to the notification builder.

@NonNull List<NotificationCompat.Action>

Get the projected actions present on this notification.

@Nullable PendingIntent

Returns the PendingIntent to be fired when the notification is tapped on the Projected device.

@NonNull NotificationCompat.ProjectedExtender

Sets the PendingIntent to be opened on the Projected device when the notification is tapped on the that device.

boolean

Returns whether the notifications shown on the projected device should use the actions from this extender (configured via addAction or addActions) instead of the main notification actions.

Public constructors

ProjectedExtender

Added in 1.18.0
public ProjectedExtender()

Create a ProjectedExtender with default options.

ProjectedExtender

Added in 1.18.0
public ProjectedExtender(@NonNull Notification notification)

Creates an ProjectedExtender from the extensions in a Notification.

Parameters
@NonNull Notification notification

the notification to extract extensions from.

Public methods

addAction

Added in 1.20.0-rc01
public @NonNull NotificationCompat.ProjectedExtender addAction(@NonNull NotificationCompat.Action action)

Add an action to this notification on projected devices.

When projected actions are added using this method, they are displayed instead of the main notification actions (added via addAction) when the notification is presented on a projected device. If no projected actions are set (or after clearActions is called), the projected device may display compatible actions from the main notification by default.

Parameters
@NonNull NotificationCompat.Action action

The action to add.

Returns
@NonNull NotificationCompat.ProjectedExtender

This ProjectedExtender object for chaining.

addActions

Added in 1.20.0-rc01
public @NonNull NotificationCompat.ProjectedExtender addActions(@NonNull List<NotificationCompat.Action> actions)

Adds multiple actions to this notification on projected devices.

When projected actions are added using this method, they are displayed instead of the main notification actions (added via addAction) when the notification is presented on a projected device. If no projected actions are set (or after clearActions is called), the projected device may display compatible actions from the main notification by default.

Passing an empty list marks shouldUseExtenderActions as true without adding any actions. On an extender with no previously added actions, this prevents any actions from being displayed on a projected device. Because this method appends to existing projected actions, if actions were previously added, call clearActions().addActions(Collections.emptyList()) to display no actions (calling clearActions alone reverts to displaying compatible actions from the main notification).

Parameters
@NonNull List<NotificationCompat.Action> actions

The list of actions to add.

Returns
@NonNull NotificationCompat.ProjectedExtender

This ProjectedExtender object for chaining.

clearActions

Added in 1.20.0-rc01
public @NonNull NotificationCompat.ProjectedExtender clearActions()

Clear all projected actions from this extender and reset shouldUseExtenderActions to false.

Calling this method reverts the extender to its default state so the projected device may display compatible actions from the main notification (matching clearActions). To explicitly display no actions on a projected device, call addActions with an empty list (or clearActions().addActions(Collections.emptyList()) if projected actions were previously added).

Returns
@NonNull NotificationCompat.ProjectedExtender

This ProjectedExtender object for chaining.

extend

public @NonNull NotificationCompat.Builder extend(@NonNull NotificationCompat.Builder builder)

Applies the Projected extensions to the notification builder. This method is called by the extend method and should not be called directly.

Parameters
@NonNull NotificationCompat.Builder builder

the notification builder to extend

Returns
@NonNull NotificationCompat.Builder

the modified notification builder

getActions

Added in 1.20.0-rc01
public @NonNull List<NotificationCompat.Action> getActions()

Get the projected actions present on this notification.

If shouldUseExtenderActions returns false (the default, or after calling clearActions), this returns an empty list and the projected device may display compatible actions from the main notification by default. If shouldUseExtenderActions returns true and this returns an empty list (for example, after calling addActions with an empty list), no actions are displayed on the projected device.

Returns
@NonNull List<NotificationCompat.Action>

List of projected actions, or an empty list if none are present.

getContentIntent

Added in 1.18.0
public @Nullable PendingIntent getContentIntent()

Returns the PendingIntent to be fired when the notification is tapped on the Projected device.

Returns
@Nullable PendingIntent

the PendingIntent to fire on tap, or null if not set

setContentIntent

Added in 1.18.0
public @NonNull NotificationCompat.ProjectedExtender setContentIntent(@Nullable PendingIntent intent)

Sets the PendingIntent to be opened on the Projected device when the notification is tapped on the that device. This is distinct from the notification's main contentIntent, which is typically fired when the notification is tapped on the host device (e.g., the phone).

Parameters
@Nullable PendingIntent intent

the PendingIntent to fire on tap

Returns
@NonNull NotificationCompat.ProjectedExtender

this ProjectedExtender object for chaining

shouldUseExtenderActions

Added in 1.20.0-rc01
public boolean shouldUseExtenderActions()

Returns whether the notifications shown on the projected device should use the actions from this extender (configured via addAction or addActions) instead of the main notification actions.

If this returns false (the default, or after calling clearActions), no projected actions have been configured and the projected device may display compatible actions from the main notification by default. If this returns true, the projected device should display the actions returned by getActions instead of the main notification actions (or display no actions if getActions is empty, such as after calling addActions with an empty list).

Returns
boolean

true if the extender's actions should be used instead of the main notification actions, false otherwise.