CarNotificationManager

public final class CarNotificationManager
extends Object

java.lang.Object
   ↳ androidx.car.app.notification.CarNotificationManager


A manager for car apps to send notifications. This class wraps a NotificationManagerCompat to manage the actual sending of the Notification.

Summary

Public methods

boolean areNotificationsEnabled()

Returns whether notifications from the calling package are not blocked.

void cancel(String tag, int id)

Cancels a previously shown notification.

void cancel(int id)

Cancels a previously shown notification.

void cancelAll()

Cancels all previously shown notifications.

void createNotificationChannel(NotificationChannelCompat channel)

Creates a notification channel that notifications can be posted to.

void createNotificationChannelGroup(NotificationChannelGroupCompat group)

Creates a group container for NotificationChannel objects.

void createNotificationChannelGroups(List<NotificationChannelGroupCompat> groups)

Creates multiple notification channel groups.

void createNotificationChannels(List<NotificationChannelCompat> channels)

Creates multiple notification channels that different notifications can be posted to.

void deleteNotificationChannel(String channelId)

Deletes the given notification channel.

void deleteNotificationChannelGroup(String groupId)

Deletes the given notification channel group, and all notification channels that belong to it.

void deleteUnlistedNotificationChannels(Collection<String> channelIds)

Deletes notification channels for which ids are NOT given.

static CarNotificationManager from(Context context)

Returns a CarNotificationManager instance for a provided context.

static Set<String> getEnabledListenerPackages(Context context)

Get the set of packages that have an enabled notification listener component within them.

int getImportance()

Returns the user specified importance for notifications from the calling package.

NotificationChannelCompat getNotificationChannel(String channelId)

Returns the notification channel settings for a given channel id.

NotificationChannelCompat getNotificationChannel(String channelId, String conversationId)

Returns the notification channel settings for a given channel and conversation id.

NotificationChannelGroupCompat getNotificationChannelGroup(String channelGroupId)

Returns the notification channel group settings for a given channel group id.

List<NotificationChannelGroupCompat> getNotificationChannelGroups()

Returns all notification channel groups belonging to the calling app or an empty list on older SDKs which don't support Notification Channels.

List<NotificationChannelCompat> getNotificationChannels()

Returns all notification channels belonging to the calling app or an empty list on older SDKs which don't support Notification Channels.

void notify(String tag, int id, NotificationCompat.Builder notification)

Post a notification to be shown in the status bar, stream, etc.

void notify(int id, NotificationCompat.Builder notification)

Posts a notification.

Inherited methods

Public methods

areNotificationsEnabled

public boolean areNotificationsEnabled ()

Returns whether notifications from the calling package are not blocked.

Returns
boolean

cancel

public void cancel (String tag, 
                int id)

Cancels a previously shown notification.

Parameters
tag String

id int

cancel

public void cancel (int id)

Cancels a previously shown notification.

Parameters
id int

cancelAll

public void cancelAll ()

Cancels all previously shown notifications.

createNotificationChannel

public void createNotificationChannel (NotificationChannelCompat channel)

Creates a notification channel that notifications can be posted to.

Parameters
channel NotificationChannelCompat

Throws
NullPointerException if channel is null

createNotificationChannelGroup

public void createNotificationChannelGroup (NotificationChannelGroupCompat group)

Creates a group container for NotificationChannel objects.

Parameters
group NotificationChannelGroupCompat

Throws
NullPointerException if group is null

createNotificationChannelGroups

public void createNotificationChannelGroups (List<NotificationChannelGroupCompat> groups)

Creates multiple notification channel groups. See createNotificationChannelGroup(NotificationChannelGroupCompat).

Parameters
groups List

Throws
NullPointerException if groups is null

createNotificationChannels

public void createNotificationChannels (List<NotificationChannelCompat> channels)

Creates multiple notification channels that different notifications can be posted to. See createNotificationChannel(NotificationChannelCompat).

Parameters
channels List

Throws
NullPointerException if channels is null

deleteNotificationChannel

public void deleteNotificationChannel (String channelId)

Deletes the given notification channel.

Parameters
channelId String

Throws
NullPointerException if channelId is null

deleteNotificationChannelGroup

public void deleteNotificationChannelGroup (String groupId)

Deletes the given notification channel group, and all notification channels that belong to it.

Parameters
groupId String

Throws
NullPointerException if groupId is null

deleteUnlistedNotificationChannels

public void deleteUnlistedNotificationChannels (Collection<String> channelIds)

Deletes notification channels for which ids are NOT given.

Parameters
channelIds Collection

Throws
NullPointerException if channelIds is null

from

public static CarNotificationManager from (Context context)

Returns a CarNotificationManager instance for a provided context.

Parameters
context Context

Returns
CarNotificationManager

Throws
NullPointerException if context is null

getEnabledListenerPackages

public static Set<String> getEnabledListenerPackages (Context context)

Get the set of packages that have an enabled notification listener component within them.

Parameters
context Context

Returns
Set<String>

Throws
NullPointerException if context is null

getImportance

public int getImportance ()

Returns the user specified importance for notifications from the calling package.

Returns
int

getNotificationChannel

public NotificationChannelCompat getNotificationChannel (String channelId)

Returns the notification channel settings for a given channel id.

Parameters
channelId String

Returns
NotificationChannelCompat

Throws
NullPointerException if channelId is null

getNotificationChannel

public NotificationChannelCompat getNotificationChannel (String channelId, 
                String conversationId)

Returns the notification channel settings for a given channel and conversation id.

Parameters
channelId String

conversationId String

Returns
NotificationChannelCompat

Throws
NullPointerException if either channelId of conversationId are null

getNotificationChannelGroup

public NotificationChannelGroupCompat getNotificationChannelGroup (String channelGroupId)

Returns the notification channel group settings for a given channel group id.

Parameters
channelGroupId String

Returns
NotificationChannelGroupCompat

Throws
NullPointerException if channelGroupId is null

getNotificationChannelGroups

public List<NotificationChannelGroupCompat> getNotificationChannelGroups ()

Returns all notification channel groups belonging to the calling app or an empty list on older SDKs which don't support Notification Channels.

Returns
List<NotificationChannelGroupCompat>

getNotificationChannels

public List<NotificationChannelCompat> getNotificationChannels ()

Returns all notification channels belonging to the calling app or an empty list on older SDKs which don't support Notification Channels.

Returns
List<NotificationChannelCompat>

notify

public void notify (String tag, 
                int id, 
                NotificationCompat.Builder notification)

Post a notification to be shown in the status bar, stream, etc.

Callers are expected to extend the notification with CarAppExtender to ensure the notification will show up on all car environments. This method will extend the notification if it is not already extended for the car using a CarAppExtender.

Parameters
tag String: the string identifier for a notification. Can be null.

id int: the ID of the notification. The pair (tag, id) must be unique within your app.

notification NotificationCompat.Builder: the notification to post to the system

Throws
NullPointerException if notification is null

notify

public void notify (int id, 
                NotificationCompat.Builder notification)

Posts a notification.

Callers are expected to extend the notification with CarAppExtender to ensure the notification will show up on all car environments. This method will extend the notification if it is not already extended for the car using a CarAppExtender.

Parameters
id int

notification NotificationCompat.Builder

Throws
NullPointerException if notification is null