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.

@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, addActions, or clearActions) 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

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 (and clearActions is not 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

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 (and clearActions is not called), the projected device may display compatible actions from the main notification by default.

Parameters
@NonNull List<NotificationCompat.Action> actions

The list of actions to add.

Returns
@NonNull NotificationCompat.ProjectedExtender

This ProjectedExtender object for chaining.

clearActions

public @NonNull NotificationCompat.ProjectedExtender clearActions()

Clear all projected actions from this extender.

Calling this method sets the projected actions to an empty list and marks shouldUseExtenderActions as true, which prevents the projected device from using the main notification actions so that no actions are displayed on a projected device.

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

public @NonNull List<NotificationCompat.Action> getActions()

Get the projected actions present on this notification.

If shouldUseExtenderActions returns false, 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 clearActions), 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

public boolean shouldUseExtenderActions()

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

If this returns false (the default), 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 clearActions).

Returns
boolean

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