NotificationManagerCompat

public final class NotificationManagerCompat


Compatibility library for NotificationManager with fallbacks for older platforms.

To use this class, call the static function from to get a NotificationManagerCompat object, and then call one of its methods to post or cancel notifications.

Summary

Constants

static final String

Intent action to register for on a service to receive side channel notifications.

static final String

Notification extras key: if set to true, the posted notification should use the side channel for delivery instead of using notification manager.

static final int

Default notification importance: shows everywhere, allowed to makes noise, but does not visually intrude.

static final int

Higher notification importance: shows everywhere, allowed to makes noise and peek.

static final int

Low notification importance: shows everywhere, but is not intrusive.

static final int

Highest notification importance: shows everywhere, allowed to makes noise, peek, and use full screen intents.

static final int

Min notification importance: only shows in the shade, below the fold.

static final int

A notification with no importance: shows nowhere, is blocked.

static final int

Value signifying that the user has not expressed an importance.

Public constructors

Public methods

boolean

Returns whether notifications from the calling package are not blocked.

void
cancel(int id)

Cancel a previously shown notification.

void
cancel(@Nullable String tag, int id)

Cancel a previously shown notification.

void

Cancel all previously shown notifications.

void

Creates a notification channel that notifications can be posted to.

void

Creates a notification channel that notifications can be posted to.

void

Creates a group container for NotificationChannel objects.

void

Creates a group container for NotificationChannel objects.

void

Creates multiple notification channel groups.

void

Creates multiple notification channel groups.

void

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

void

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

void

Deletes the given notification channel.

void

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

void

Deletes notification channels for which ids are NOT given.

static @NonNull NotificationManagerCompat
from(@NonNull Context context)

Get a NotificationManagerCompat instance for a provided context.

static @NonNull Set<String>

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

int

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

@Nullable NotificationChannel

Returns the notification channel settings for a given channel id.

@Nullable NotificationChannel
getNotificationChannel(
    @NonNull String channelId,
    @NonNull String conversationId
)

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

@Nullable NotificationChannelCompat

Returns the notification channel settings for a given channel id.

@Nullable NotificationChannelCompat
getNotificationChannelCompat(
    @NonNull String channelId,
    @NonNull String conversationId
)

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

@Nullable NotificationChannelGroup

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

@Nullable NotificationChannelGroupCompat

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

@NonNull List<NotificationChannelGroup>

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

@NonNull List<NotificationChannelGroupCompat>

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

@NonNull List<NotificationChannel>

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

@NonNull List<NotificationChannelCompat>

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

void
notify(int id, @NonNull Notification notification)

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

void
notify(@Nullable String tag, int id, @NonNull Notification notification)

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

Constants

ACTION_BIND_SIDE_CHANNEL

public static final String ACTION_BIND_SIDE_CHANNEL

Intent action to register for on a service to receive side channel notifications. The listening service must be in the same package as an enabled android.service.notification.NotificationListenerService.

EXTRA_USE_SIDE_CHANNEL

public static final String EXTRA_USE_SIDE_CHANNEL

Notification extras key: if set to true, the posted notification should use the side channel for delivery instead of using notification manager.

IMPORTANCE_DEFAULT

public static final int IMPORTANCE_DEFAULT

Default notification importance: shows everywhere, allowed to makes noise, but does not visually intrude.

IMPORTANCE_HIGH

public static final int IMPORTANCE_HIGH

Higher notification importance: shows everywhere, allowed to makes noise and peek.

IMPORTANCE_LOW

public static final int IMPORTANCE_LOW

Low notification importance: shows everywhere, but is not intrusive.

IMPORTANCE_MAX

public static final int IMPORTANCE_MAX

Highest notification importance: shows everywhere, allowed to makes noise, peek, and use full screen intents.

IMPORTANCE_MIN

public static final int IMPORTANCE_MIN

Min notification importance: only shows in the shade, below the fold.

IMPORTANCE_NONE

public static final int IMPORTANCE_NONE

A notification with no importance: shows nowhere, is blocked.

IMPORTANCE_UNSPECIFIED

public static final int IMPORTANCE_UNSPECIFIED

Value signifying that the user has not expressed an importance. This value is for persisting preferences, and should never be associated with an actual notification.

Public constructors

NotificationManagerCompat

public final NotificationManagerCompat()

Public methods

areNotificationsEnabled

public boolean areNotificationsEnabled()

Returns whether notifications from the calling package are not blocked.

cancel

public void cancel(int id)

Cancel a previously shown notification.

Parameters
int id

the ID of the notification

cancel

public void cancel(@Nullable String tag, int id)

Cancel a previously shown notification.

Parameters
@Nullable String tag

the string identifier of the notification.

int id

the ID of the notification

cancelAll

public void cancelAll()

Cancel all previously shown notifications.

createNotificationChannel

public void createNotificationChannel(@NonNull NotificationChannel channel)

Creates a notification channel that notifications can be posted to. This can also be used to restore a deleted channel and to update an existing channel's name, description, group, and/or importance.

The importance of an existing channel will only be changed if the new importance is lower than the current value and the user has not altered any settings on this channel.

The group of an existing channel will only be changed if the channel does not already belong to a group. All other fields are ignored for channels that already exist. It doesn't do anything on older SDKs which don't support Notification Channels.

Parameters
@NonNull NotificationChannel channel

the channel to create. Note that the created channel may differ from this value. If the provided channel is malformed, a RemoteException will be thrown.

createNotificationChannel

public void createNotificationChannel(@NonNull NotificationChannelCompat channel)

Creates a notification channel that notifications can be posted to. This can also be used to restore a deleted channel and to update an existing channel's name, description, group, and/or importance.

The importance of an existing channel will only be changed if the new importance is lower than the current value and the user has not altered any settings on this channel.

The group of an existing channel will only be changed if the channel does not already belong to a group. All other fields are ignored for channels that already exist. It doesn't do anything on older SDKs which don't support Notification Channels.

Parameters
@NonNull NotificationChannelCompat channel

the channel to create. Note that the created channel may differ from this value. If the provided channel is malformed, a RemoteException will be thrown.

createNotificationChannelGroup

public void createNotificationChannelGroup(@NonNull NotificationChannelGroup group)

Creates a group container for NotificationChannel objects. This can be used to rename an existing group. It doesn't do anything on older SDKs which don't support Notification Channels.

Parameters
@NonNull NotificationChannelGroup group

The group to create

createNotificationChannelGroup

public void createNotificationChannelGroup(
    @NonNull NotificationChannelGroupCompat group
)

Creates a group container for NotificationChannel objects. This can be used to rename an existing group. It doesn't do anything on older SDKs which don't support Notification Channels.

Parameters
@NonNull NotificationChannelGroupCompat group

The group to create

createNotificationChannelGroups

public void createNotificationChannelGroups(
    @NonNull List<NotificationChannelGroup> groups
)

Creates multiple notification channel groups. See createNotificationChannelGroup. It doesn't do anything on older SDKs which don't support Notification Channels.

Parameters
@NonNull List<NotificationChannelGroup> groups

The list of groups to create

createNotificationChannelGroupsCompat

public void createNotificationChannelGroupsCompat(
    @NonNull List<NotificationChannelGroupCompat> groups
)

Creates multiple notification channel groups. See createNotificationChannelGroup. It doesn't do anything on older SDKs which don't support Notification Channels.

Parameters
@NonNull List<NotificationChannelGroupCompat> groups

The list of groups to create

createNotificationChannels

public void createNotificationChannels(@NonNull List<NotificationChannel> channels)

Creates multiple notification channels that different notifications can be posted to. See createNotificationChannel. It doesn't do anything on older SDKs which don't support Notification Channels.

Parameters
@NonNull List<NotificationChannel> channels

the list of channels to attempt to create.

createNotificationChannelsCompat

public void createNotificationChannelsCompat(
    @NonNull List<NotificationChannelCompat> channels
)

Creates multiple notification channels that different notifications can be posted to. See createNotificationChannel. It doesn't do anything on older SDKs which don't support Notification Channels.

Parameters
@NonNull List<NotificationChannelCompat> channels

the list of channels to attempt to create.

deleteNotificationChannel

public void deleteNotificationChannel(@NonNull String channelId)

Deletes the given notification channel.

If you create a new channel with this same id, the deleted channel will be un-deleted with all of the same settings it had before it was deleted. It doesn't do anything on older SDKs which don't support Notification Channels.

deleteNotificationChannelGroup

public void deleteNotificationChannelGroup(@NonNull String groupId)

Deletes the given notification channel group, and all notification channels that belong to it. It doesn't do anything on older SDKs which don't support Notification Channels.

deleteUnlistedNotificationChannels

public void deleteUnlistedNotificationChannels(
    @NonNull Collection<String> channelIds
)

Deletes notification channels for which ids are NOT given. This will NOT delete channels which are conversation children of the given channels. It doesn't do anything on older SDKs which don't support Notification Channels.

Parameters
@NonNull Collection<String> channelIds

the IDs of any channels which should NOT be deleted by this method.

from

public static @NonNull NotificationManagerCompat from(@NonNull Context context)

Get a NotificationManagerCompat instance for a provided context.

getEnabledListenerPackages

public static @NonNull Set<StringgetEnabledListenerPackages(@NonNull Context context)

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

getImportance

public int getImportance()

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

Returns
int

An importance level, such as IMPORTANCE_DEFAULT.

getNotificationChannel

public @Nullable NotificationChannel getNotificationChannel(@NonNull String channelId)

Returns the notification channel settings for a given channel id. Returns null on older SDKs which don't support Notification Channels.

getNotificationChannel

public @Nullable NotificationChannel getNotificationChannel(
    @NonNull String channelId,
    @NonNull String conversationId
)

Returns the notification channel settings for a given channel and conversation id. Returns the channel for the channelId on older SDKs which don't support Conversations. Returns null on older SDKs which don't support Notification Channels.

getNotificationChannelCompat

public @Nullable NotificationChannelCompat getNotificationChannelCompat(@NonNull String channelId)

Returns the notification channel settings for a given channel id. Returns null on older SDKs which don't support Notification Channels.

getNotificationChannelCompat

public @Nullable NotificationChannelCompat getNotificationChannelCompat(
    @NonNull String channelId,
    @NonNull String conversationId
)

Returns the notification channel settings for a given channel and conversation id. Returns the channel for the channelId on older SDKs which don't support Conversations. Returns null on older SDKs which don't support Notification Channels.

getNotificationChannelGroup

public @Nullable NotificationChannelGroup getNotificationChannelGroup(@NonNull String channelGroupId)

Returns the notification channel group settings for a given channel group id. Returns null on older SDKs which don't support Notification Channels.

getNotificationChannelGroupCompat

public @Nullable NotificationChannelGroupCompat getNotificationChannelGroupCompat(@NonNull String channelGroupId)

Returns the notification channel group settings for a given channel group id. Returns null on older SDKs which don't support Notification Channels.

getNotificationChannelGroups

public @NonNull List<NotificationChannelGroupgetNotificationChannelGroups()

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

getNotificationChannelGroupsCompat

public @NonNull List<NotificationChannelGroupCompatgetNotificationChannelGroupsCompat()

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

getNotificationChannels

public @NonNull List<NotificationChannelgetNotificationChannels()

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

getNotificationChannelsCompat

public @NonNull List<NotificationChannelCompatgetNotificationChannelsCompat()

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

notify

public void notify(int id, @NonNull Notification notification)

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

Parameters
int id

the ID of the notification

@NonNull Notification notification

the notification to post to the system

notify

public void notify(@Nullable String tag, int id, @NonNull Notification notification)

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

Parameters
@Nullable String tag

the string identifier for a notification. Can be null.

int id

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

@NonNull Notification notification

the notification to post to the system