CarAppExtender
class CarAppExtender : NotificationCompat.Extender
Helper class to add car app extensions to notifications.
By default, notifications in a car screen have the properties provided by NotificationCompat.Builder. This helper class provides methods to override those properties for the car screen. However, notifications only show up in the car screen if it is extended with CarAppExtender, even if the extender does not override any properties. To create a notification with car extensions:
- Create a
NotificationCompat.Builder, setting any desired properties. - Create a
CarAppExtender.Builder. - Set car-specific properties using the
setmethods ofCarAppExtender.Builder. - Create a
CarAppExtenderby callingbuild. - Call
extendto apply the extensions to a notification. - Post the notification to the notification system with the
CarNotificationManager.notify(...)methods. Do not use theNotificationManager.notify(...), nor the NotificationManagerCompat.notify(...)} methods.
Notification notification = new NotificationCompat.Builder(context) ... .extend(new CarAppExtender.Builder() .set*(...) .build()) .build();
Car extensions can be accessed on an existing notification by using the
CarAppExtender(Notification) constructor, and then using the get methods to access values.
The car screen UI is affected by the notification channel importance (Android O and above) or notification priority (below Android O) in the following ways:
- A heads-up-notification (HUN) will show if the importance is set to
IMPORTANCE_HIGH, or the priority is set toPRIORITY_HIGHor above. - The notification center icon, which opens a screen with all posted notifications when tapped, will show a badge for a new notification if the importance is set to
IMPORTANCE_DEFAULTor above, or the priority is set toPRIORITY_DEFAULTor above. - The notification entry will show in the notification center for all priority levels.
setImportance will override the importance for the notification in the car screen.
Calling NotificationCompat.Builder#setOnlyAlertOnce(true) will alert a high-priority notification only once in the HUN. Updating the same notification will not trigger another HUN event.
For a navigation app's turn-by-turn (TBT) notifications, which update the same notification frequently with navigation information, the notification UI has a slightly different behavior. The app can post a TBT notification by calling
NotificationCompat.Builder#setOngoing(true) and
NotificationCompat.Builder#setCategory(NotificationCompat.CATEGORY_NAVIGATION).
TBT notifications behave the same as regular notifications with the following exceptions:
- The notification will not be displayed if the navigation app is not the currently active navigation app, or if the app is already displaying routing information in the navigation template.
- The heads-up-notification (HUN) can be customized with a background color through
setColor. - The notification will not be displayed in the notification center.
In addition to that, the information in the navigation notification will be displayed in the rail widget at the bottom of the screen when the app is in the background.
Note that frequent HUNs distract the driver. The recommended practice is to update the TBT notification regularly on distance changes, which updates the rail widget, but call
NotificationCompat.Builder#setOnlyAlertOnce(true) unless there is a significant navigation turn event.
Summary
Nested types |
|---|
class CarAppExtender.BuilderA builder of |
Public constructors |
|---|
CarAppExtender(notification: Notification)Creates a |
Public functions |
|
|---|---|
Notification.Builder |
extend(builder: Notification.Builder)Applies car extensions to a notification that is being built. |
NotificationCompat.Builder |
extend(builder: NotificationCompat.Builder)Applies car extensions to a notification that is being built. |
(Mutable)List<Notification.Action!> |
Returns the list of |
String? |
Returns the channel id of the notification channel to use in the car. |
CarColor? |
getColor()Returns the background color of the notification or |
PendingIntent? |
Returns the |
CharSequence? |
Returns the content text of the notification or |
CharSequence? |
Returns the content title for the notification or |
PendingIntent? |
Returns the |
Int |
Returns the importance of the notification in the car screen. |
Bitmap? |
Returns the large icon bitmap to display in the notification or |
@DrawableRes Int |
Returns the resource ID of the small icon drawable to use. |
java-static Boolean |
isExtended(notification: Notification)Returns whether the given notification was extended with |
Public constructors
CarAppExtender
CarAppExtender(notification: Notification)
Creates a CarAppExtender from the CarAppExtender of an existing notification.
Public functions
extend
fun extend(builder: Notification.Builder): Notification.Builder
Applies car extensions to a notification that is being built.
For the most part, developers should be building notifications via NotificationCompat.Builder and not Notification.Builder; however, there may be reasons to not use the compat version, so this non-compat method is provided for convenience in those situations.
| Throws | |
|---|---|
java.lang.NullPointerException |
if |
extend
fun extend(builder: NotificationCompat.Builder): NotificationCompat.Builder
Applies car extensions to a notification that is being built.
This is automatically called when the style is applied to the builder via extend so this method does not need to be manually called.
| Throws | |
|---|---|
java.lang.NullPointerException |
if |
getActions
fun getActions(): (Mutable)List<Notification.Action!>
Returns the list of Action present on this car notification.
| See also | |
|---|---|
addAction |
getChannelId
fun getChannelId(): String?
Returns the channel id of the notification channel to use in the car.
| See also | |
|---|---|
setChannelId |
getColor
fun getColor(): CarColor?
Returns the background color of the notification or null if a default color is to be used.
| See also | |
|---|---|
setColor |
getContentIntent
fun getContentIntent(): PendingIntent?
Returns the PendingIntent to send when the notification is clicked in the car or null if not set.
| See also | |
|---|---|
setContentIntent |
getContentText
fun getContentText(): CharSequence?
Returns the content text of the notification or null if not set.
| See also | |
|---|---|
setContentText |
getContentTitle
fun getContentTitle(): CharSequence?
Returns the content title for the notification or null if not set.
| See also | |
|---|---|
setContentTitle |
getDeleteIntent
fun getDeleteIntent(): PendingIntent?
Returns the PendingIntent to send when the notification is cleared by the user or null if not set.
| See also | |
|---|---|
setDeleteIntent |
getImportance
fun getImportance(): Int
Returns the importance of the notification in the car screen.
| See also | |
|---|---|
setImportance |
getLargeIcon
fun getLargeIcon(): Bitmap?
Returns the large icon bitmap to display in the notification or null if not set.
| See also | |
|---|---|
setLargeIcon |
getSmallIcon
fun getSmallIcon(): @DrawableRes Int
Returns the resource ID of the small icon drawable to use.
| See also | |
|---|---|
setSmallIcon |
isExtended
java-static fun isExtended(notification: Notification): Boolean
Returns whether the given notification was extended with CarAppExtender.
| Throws | |
|---|---|
java.lang.NullPointerException |
if |