NotificationCompat.MetricStyle


@RequiresApi(value = 26)
public final class NotificationCompat.MetricStyle extends NotificationCompat.Style


A notification style which shows up to 3 metrics when expanded. Metrics usually represent quantities that change over time, such as fitness information collected by a tracker, timers, weather information, etc.

To use this style with your Notification, feed it to setStyle like so:

Notification notif = new NotificationCompat.Builder(context)
  .setStyle(new MetricStyle()
      .addMetric(new Metric(new Metric.FixedInt(1979), "Steps"))
      .addMetric(new Metric(
          Metric.TimeDifference.forStopwatch(startTime, FORMAT_CHRONOMETER_AUTOMATIC),
          "Time elapsed")))

A MetricStyle must contain at least one Metric object to be valid; an invalid style will be rejected when build is called.

If a notification with this style is promoted ongoing, then one of its metrics might be displayed in the status bar chip.

Note that this style doesn't display the large icon set via setLargeIcon.

This style is only supported in devices with CINNAMON_BUN or greater. If used on a device with a lower SDK_INT then the notification will be displayed with the standard appearance (i.e. showing content text, but not metrics).

Summary

Constants

static final int

Special value for setCriticalMetric to indicate that none of the metrics should be considered the "most important" one.

Public constructors

@RequiresApi(value = 37)
MetricStyle()

Constructs a new instance of MetricStyle.

Public methods

void

This is called with the extras of the framework Notification during the build process, after apply() has been called.

@NonNull NotificationCompat.MetricStyle

Adds a Metric to this MetricStyle.

boolean
@Nullable NotificationCompat.Metric

Returns which, if any, of the metrics is considered the "most important", or null if none are.

@NonNull List<NotificationCompat.Metric>

Returns an immutable view of the list of Metric instances in this MetricStyle.

int
@NonNull NotificationCompat.MetricStyle
setCriticalMetric(int index)

Indicates which of the metrics is considered the "most important".

@NonNull NotificationCompat.MetricStyle

Sets the list of Metric instances for this MetricStyle, replacing whatever was added before.

@NonNull String

Protected methods

void
void

Inherited methods

From androidx.core.app.NotificationCompat.Style
@Nullable Notification

If this Style object has been set on a notification builder, this method will build that notification and return it.

void

Link this rich notification style with a notification builder.

Constants

METRIC_INDEX_NONE

public static final int METRIC_INDEX_NONE = -1

Special value for setCriticalMetric to indicate that none of the metrics should be considered the "most important" one.

Public constructors

MetricStyle

@RequiresApi(value = 37)
public MetricStyle()

Constructs a new instance of MetricStyle. Use addMetric to populate it.

This style is only supported in devices with CINNAMON_BUN or greater. If used on a device with a lower SDK_INT then the notification will be displayed with the standard appearance (i.e. showing content text, but not metrics).

Public methods

addCompatExtras

public void addCompatExtras(@NonNull Bundle extras)

This is called with the extras of the framework Notification during the build process, after apply() has been called. This means that you only need to add data which won't be populated by the framework Notification which was built so far. Moreover, recovering builders and styles is only supported at API 19 and above, no implementation is required for current BigTextStyle, BigPictureStyle, or InboxStyle.

equals

public boolean equals(Object obj)

getCriticalMetric

public @Nullable NotificationCompat.Metric getCriticalMetric()

Returns which, if any, of the metrics is considered the "most important", or null if none are. This may be used when the notification is displayed in other surfaces (such as a status bar chip).

By default, unless setCriticalMetric has been set, the first metric in the list is considered the critical one.

getMetrics

public @NonNull List<NotificationCompat.MetricgetMetrics()

Returns an immutable view of the list of Metric instances in this MetricStyle.

hashCode

public int hashCode()

setCriticalMetric

public @NonNull NotificationCompat.MetricStyle setCriticalMetric(int index)

Indicates which of the metrics is considered the "most important". This may be used when the notification is displayed in other surfaces (such as a status bar chip).

Parameters
int index

either the index (0-based) of an item in getMetrics, or METRIC_INDEX_NONE to indicate no Metric should be used for this purpose

setMetrics

public @NonNull NotificationCompat.MetricStyle setMetrics(@NonNull List<NotificationCompat.Metric> metrics)

Sets the list of Metric instances for this MetricStyle, replacing whatever was added before.

toString

public @NonNull String toString()

Protected methods

clearCompatExtraKeys

protected void clearCompatExtraKeys(@NonNull Bundle extras)

restoreFromCompatExtras

protected void restoreFromCompatExtras(@NonNull Bundle extras)