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.

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

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

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

Parameters
actions: (Mutable)List<NotificationCompat.Action!>

The list of actions to add.

Returns
NotificationCompat.ProjectedExtender

This ProjectedExtender object for chaining.

clearActions

fun clearActions(): NotificationCompat.ProjectedExtender

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
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

fun getActions(): (Mutable)List<NotificationCompat.Action!>

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

fun shouldUseExtenderActions(): 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.

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.