Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

NotificationManagerCompat

class NotificationManagerCompat
kotlin.Any
   ↳ androidx.core.app.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 String

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

static String

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

static Int

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

static Int

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

static Int

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

static Int

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

static Int

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

static Int

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

static Int

Value signifying that the user has not expressed an importance.

Public methods

Boolean

Returns whether notifications from the calling package are not blocked.

Unit
cancel(id: Int)

Cancel a previously shown notification.

Unit
cancel(@Nullable tag: String?, id: Int)

Cancel a previously shown notification.

Unit

Cancel all previously shown notifications.

Unit

Creates a notification channel that notifications can be posted to.

Unit

Creates a group container for NotificationChannel objects.

Unit

Creates multiple notification channel groups.

Unit

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

Unit
deleteNotificationChannel(@NonNull channelId: String)

Deletes the given notification channel.

Unit

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

static NotificationManagerCompat
from(@NonNull context: Context)

Get a NotificationManagerCompat instance for a provided context.

static MutableSet<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.

NotificationChannel?
getNotificationChannel(@NonNull channelId: String)

Returns the notification channel settings for a given channel id.

NotificationChannelGroup?
getNotificationChannelGroup(@NonNull channelGroupId: String)

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

MutableList<NotificationChannelGroup!>

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

MutableList<NotificationChannel!>

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

Unit
notify(id: Int, @NonNull notification: Notification)

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

Unit
notify(@Nullable tag: String?, id: Int, @NonNull notification: Notification)

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

Constants

ACTION_BIND_SIDE_CHANNEL

static val ACTION_BIND_SIDE_CHANNEL: String

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.

Value: "android.support.BIND_NOTIFICATION_SIDE_CHANNEL"

EXTRA_USE_SIDE_CHANNEL

static val EXTRA_USE_SIDE_CHANNEL: String

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

Value: "android.support.useSideChannel"

IMPORTANCE_DEFAULT

static val IMPORTANCE_DEFAULT: Int

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

Value: 3

IMPORTANCE_HIGH

static val IMPORTANCE_HIGH: Int

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

Value: 4

IMPORTANCE_LOW

static val IMPORTANCE_LOW: Int

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

Value: 2

IMPORTANCE_MAX

static val IMPORTANCE_MAX: Int

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

Value: 5

IMPORTANCE_MIN

static val IMPORTANCE_MIN: Int

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

Value: 1

IMPORTANCE_NONE

static val IMPORTANCE_NONE: Int

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

Value: 0

IMPORTANCE_UNSPECIFIED

static val IMPORTANCE_UNSPECIFIED: Int

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.

Value: -1000

Public methods

areNotificationsEnabled

fun areNotificationsEnabled(): Boolean

Returns whether notifications from the calling package are not blocked.

cancel

fun cancel(id: Int): Unit

Cancel a previously shown notification.

Parameters
id Int: the ID of the notification

cancel

fun cancel(@Nullable tag: String?, id: Int): Unit

Cancel a previously shown notification.

Parameters
tag String?: the string identifier of the notification.
id String?: the ID of the notification

cancelAll

fun cancelAll(): Unit

Cancel all previously shown notifications.

createNotificationChannel

fun createNotificationChannel(@NonNull channel: NotificationChannel): Unit

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

fun createNotificationChannelGroup(@NonNull group: NotificationChannelGroup): Unit

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

fun createNotificationChannelGroups(@NonNull groups: MutableList<NotificationChannelGroup!>): Unit

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

Parameters
groups MutableList<NotificationChannelGroup!>: The list of groups to create

createNotificationChannels

fun createNotificationChannels(@NonNull channels: MutableList<NotificationChannel!>): Unit

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 MutableList<NotificationChannel!>: the list of channels to attempt to create.

deleteNotificationChannel

fun deleteNotificationChannel(@NonNull channelId: String): Unit

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.

deleteNotificationChannelGroup

fun deleteNotificationChannelGroup(@NonNull groupId: String): Unit

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.

from

@NonNull static fun from(@NonNull context: Context): NotificationManagerCompat

Get a NotificationManagerCompat instance for a provided context.

getEnabledListenerPackages

@NonNull static fun getEnabledListenerPackages(@NonNull context: Context): MutableSet<String!>

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

getImportance

fun getImportance(): Int

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

Return
Int: An importance level, such as IMPORTANCE_DEFAULT.

getNotificationChannel

@Nullable fun getNotificationChannel(@NonNull channelId: String): NotificationChannel?

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

getNotificationChannelGroup

@Nullable fun getNotificationChannelGroup(@NonNull channelGroupId: String): NotificationChannelGroup?

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

getNotificationChannelGroups

@NonNull fun getNotificationChannelGroups(): MutableList<NotificationChannelGroup!>

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

getNotificationChannels

@NonNull fun getNotificationChannels(): MutableList<NotificationChannel!>

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

notify

fun notify(id: Int, @NonNull notification: Notification): Unit

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

Parameters
id Int: the ID of the notification
notification Int: the notification to post to the system

notify

fun notify(@Nullable tag: String?, id: Int, @NonNull notification: Notification): Unit

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 String?: the ID of the notification. The pair (tag, id) must be unique within your app.
notification String?: the notification to post to the system