NotificationCompat.ProjectedExtender


class NotificationCompat.ProjectedExtender : 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 functions

NotificationCompat.ProjectedExtender

Add an action to this notification on projected devices.

NotificationCompat.ProjectedExtender

Adds multiple actions to this notification on projected devices.

NotificationCompat.ProjectedExtender

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

NotificationCompat.Builder

Applies the Projected extensions to the notification builder.

(Mutable)List<NotificationCompat.Action!>

Get the projected actions present on this notification.

PendingIntent?

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

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
ProjectedExtender()

Create a ProjectedExtender with default options.

ProjectedExtender

Added in 1.18.0
ProjectedExtender(notification: Notification)

Creates an ProjectedExtender from the extensions in a Notification.

Parameters
notification: Notification

the notification to extract extensions from.

Public functions

addAction

Added in 1.20.0-rc01
fun addAction(action: NotificationCompat.Action): NotificationCompat.ProjectedExtender

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
action: NotificationCompat.Action

The action to add.

Returns
NotificationCompat.ProjectedExtender

This ProjectedExtender object for chaining.

addActions

Added in 1.20.0-rc01
fun addActions(actions: (Mutable)List<NotificationCompat.Action!>): NotificationCompat.ProjectedExtender

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
actions: (Mutable)List<NotificationCompat.Action!>

The list of actions to add.

Returns
NotificationCompat.ProjectedExtender

This ProjectedExtender object for chaining.

clearActions

Added in 1.20.0-rc01
fun clearActions(): NotificationCompat.ProjectedExtender

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
NotificationCompat.ProjectedExtender

This ProjectedExtender object for chaining.

extend

fun extend(builder: NotificationCompat.Builder): NotificationCompat.Builder

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

Parameters
builder: NotificationCompat.Builder

the notification builder to extend

Returns
NotificationCompat.Builder

the modified notification builder

getActions

Added in 1.20.0-rc01
fun getActions(): (Mutable)List<NotificationCompat.Action!>

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
(Mutable)List<NotificationCompat.Action!>

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

getContentIntent

Added in 1.18.0
fun getContentIntent(): PendingIntent?

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

Returns
PendingIntent?

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

setContentIntent

Added in 1.18.0
fun setContentIntent(intent: PendingIntent?): NotificationCompat.ProjectedExtender

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
intent: PendingIntent?

the PendingIntent to fire on tap

Returns
NotificationCompat.ProjectedExtender

this ProjectedExtender object for chaining

shouldUseExtenderActions

Added in 1.20.0-rc01
fun shouldUseExtenderActions(): 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.

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.