Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

NotificationManagerCompat

public final class NotificationManagerCompat
extends Object

java.lang.Object
   ↳ androidx.core.app.NotificationManagerCompat


Compatibility library for NotificationManager with fallbacks for older platforms.

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

Summary

Constants

String ACTION_BIND_SIDE_CHANNEL

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

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.

int IMPORTANCE_DEFAULT

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

int IMPORTANCE_HIGH

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

int IMPORTANCE_LOW

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

int IMPORTANCE_MAX

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

int IMPORTANCE_MIN

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

int IMPORTANCE_NONE

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

int IMPORTANCE_UNSPECIFIED

Value signifying that the user has not expressed an importance.

Public methods

boolean areNotificationsEnabled()

Returns whether notifications from the calling package are not blocked.

void cancel(String tag, int id)

Cancel a previously shown notification.

void cancel(int id)

Cancel a previously shown notification.

void cancelAll()

Cancel all previously shown notifications.

void createNotificationChannel(NotificationChannel channel)

Creates a notification channel that notifications can be posted to.

void createNotificationChannelGroup(NotificationChannelGroup group)

Creates a group container for NotificationChannel objects.

void createNotificationChannelGroups(List<NotificationChannelGroup> groups)

Creates multiple notification channel groups.

void createNotificationChannels(List<NotificationChannel> 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.

static NotificationManagerCompat from(Context context)

Get a NotificationManagerCompat 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.

NotificationChannel getNotificationChannel(String channelId)

Returns the notification channel settings for a given channel id.

NotificationChannelGroup getNotificationChannelGroup(String channelGroupId)

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

List<NotificationChannelGroup> getNotificationChannelGroups()

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

List<NotificationChannel> getNotificationChannels()

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

void notify(String tag, int id, Notification notification)

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

void notify(int id, Notification notification)

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

Inherited methods

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

Constant Value: "android.support.BIND_NOTIFICATION_SIDE_CHANNEL"

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.

Constant Value: "android.support.useSideChannel"

IMPORTANCE_DEFAULT

public static final int IMPORTANCE_DEFAULT

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

Constant Value: 3 (0x00000003)

IMPORTANCE_HIGH

public static final int IMPORTANCE_HIGH

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

Constant Value: 4 (0x00000004)

IMPORTANCE_LOW

public static final int IMPORTANCE_LOW

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

Constant Value: 2 (0x00000002)

IMPORTANCE_MAX

public static final int IMPORTANCE_MAX

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

Constant Value: 5 (0x00000005)

IMPORTANCE_MIN

public static final int IMPORTANCE_MIN

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

Constant Value: 1 (0x00000001)

IMPORTANCE_NONE

public static final int IMPORTANCE_NONE

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

Constant Value: 0 (0x00000000)

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.

Constant Value: -1000 (0xfffffc18)

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)

Cancel a previously shown notification.

Parameters
tag String: the string identifier of the notification.

id int: the ID of the notification

cancel

public void cancel (int id)

Cancel a previously shown notification.

Parameters
id int: the ID of the notification

cancelAll

public void cancelAll ()

Cancel all previously shown notifications.

createNotificationChannel

public void createNotificationChannel (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 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 doesn't support Notification Channels.

Parameters
channel NotificationChannel: 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 (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 doesn't support Notification Channels.

Parameters
group NotificationChannelGroup: The group to create

createNotificationChannelGroups

public void createNotificationChannelGroups (List<NotificationChannelGroup> groups)

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

Parameters
groups List: The list of groups to create

createNotificationChannels

public void createNotificationChannels (List<NotificationChannel> channels)

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

Parameters
channels List: the list of channels to attempt to create.

deleteNotificationChannel

public void deleteNotificationChannel (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 doesn't support Notification Channels.

Parameters
channelId String

deleteNotificationChannelGroup

public void deleteNotificationChannelGroup (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 doesn't support Notification Channels.

Parameters
groupId String

from

public static NotificationManagerCompat from (Context context)

Get a NotificationManagerCompat instance for a provided context.

Parameters
context Context

Returns
NotificationManagerCompat

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>

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 NotificationChannel getNotificationChannel (String channelId)

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

Parameters
channelId String

Returns
NotificationChannel

getNotificationChannelGroup

public NotificationChannelGroup getNotificationChannelGroup (String channelGroupId)

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

Parameters
channelGroupId String

Returns
NotificationChannelGroup

getNotificationChannelGroups

public List<NotificationChannelGroup> getNotificationChannelGroups ()

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

Returns
List<NotificationChannelGroup>

getNotificationChannels

public List<NotificationChannel> getNotificationChannels ()

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

Returns
List<NotificationChannel>

notify

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

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

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 Notification: the notification to post to the system

notify

public void notify (int id, 
                Notification notification)

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

Parameters
id int: the ID of the notification

notification Notification: the notification to post to the system