Skip to content

Most visited

Recently visited

navigation

NotificationManager

public class NotificationManager
extends Object

java.lang.Object
   ↳ android.app.NotificationManager


Class to notify the user of events that happen. This is how you tell the user that something has happened in the background.

Notifications can take different forms:

  • A persistent icon that goes in the status bar and is accessible through the launcher, (when the user selects it, a designated Intent can be launched),
  • Turning on or flashing LEDs on the device, or
  • Alerting the user by flashing the backlight, playing a sound, or vibrating.

Each of the notify methods takes an int id parameter and optionally a String tag parameter, which may be null. These parameters are used to form a pair (tag, id), or (null, id) if tag is unspecified. This pair identifies this notification from your app to the system, so that pair should be unique within your app. If you call one of the notify methods with a (tag, id) pair that is currently active and a new set of notification parameters, it will be updated. For example, if you pass a new status bar icon, the old icon in the status bar will be replaced with the new one. This is also the same tag and id you pass to the cancel(int) or cancel(String, int) method to clear this notification.

Developer Guides

For a guide to creating notifications, read the Status Bar Notifications developer guide.

Instances of this class must be obtained using Context.getSystemService(Class) with the argument NotificationManager.class or Context.getSystemService(String) with the argument Context.NOTIFICATION_SERVICE.

See also:

Summary

Nested classes

class NotificationManager.Policy

Notification policy configuration. 

Constants

String ACTION_INTERRUPTION_FILTER_CHANGED

Intent that is broadcast when the state of getCurrentInterruptionFilter() changes.

String ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED

Intent that is broadcast when the state of isNotificationPolicyAccessGranted() changes.

String ACTION_NOTIFICATION_POLICY_CHANGED

Intent that is broadcast when the state of getNotificationPolicy() changes.

int IMPORTANCE_DEFAULT

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

int IMPORTANCE_HIGH

Higher notification importance: shows everywhere, makes noise and peeks.

int IMPORTANCE_LOW

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

int IMPORTANCE_MAX

Unused.

int IMPORTANCE_MIN

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

int IMPORTANCE_NONE

A notification with no importance: does not show in the shade.

int IMPORTANCE_UNSPECIFIED

Value signifying that the user has not expressed an importance.

int INTERRUPTION_FILTER_ALARMS

Interruption filter constant - Alarms only interruption filter - all notifications except those of category CATEGORY_ALARM are suppressed.

int INTERRUPTION_FILTER_ALL

Interruption filter constant - Normal interruption filter - no notifications are suppressed.

int INTERRUPTION_FILTER_NONE

Interruption filter constant - No interruptions filter - all notifications are suppressed and all audio streams (except those used for phone calls) and vibrations are muted.

int INTERRUPTION_FILTER_PRIORITY

Interruption filter constant - Priority interruption filter - all notifications are suppressed except those that match the priority criteria.

int INTERRUPTION_FILTER_UNKNOWN

Interruption filter constant - returned when the value is unavailable for any reason.

Public methods

String addAutomaticZenRule(AutomaticZenRule automaticZenRule)

Creates the given zen rule.

boolean areNotificationsEnabled()

Returns whether notifications from the calling package are blocked.

void cancel(int id)

Cancel a previously shown notification.

void cancel(String tag, 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.

StatusBarNotification[] getActiveNotifications()

Recover a list of active notifications: ones that have been posted by the calling app that have not yet been dismissed by the user or cancel(String, int)ed by the app.

AutomaticZenRule getAutomaticZenRule(String id)

Returns the AutomaticZenRule with the given id, if it exists and the caller has access.

Map<StringAutomaticZenRule> getAutomaticZenRules()

Returns AutomaticZenRules owned by the caller.

final int getCurrentInterruptionFilter()

Gets the current notification interruption filter.

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.

List<NotificationChannelGroup> getNotificationChannelGroups()

Returns all notification channel groups belonging to the calling app.

List<NotificationChannel> getNotificationChannels()

Returns all notification channels belonging to the calling package.

NotificationManager.Policy getNotificationPolicy()

Gets the current notification policy.

boolean isNotificationPolicyAccessGranted()

Checks the ability to read/modify notification policy for the calling package.

void notify(int id, Notification notification)

Post a notification to be shown in the status bar.

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

Post a notification to be shown in the status bar.

boolean removeAutomaticZenRule(String id)

Deletes the automatic zen rule with the given id.

final void setInterruptionFilter(int interruptionFilter)

Sets the current notification interruption filter.

void setNotificationPolicy(NotificationManager.Policy policy)

Sets the current notification policy.

boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule)

Updates the given zen rule.

Inherited methods

From class java.lang.Object

Constants

ACTION_INTERRUPTION_FILTER_CHANGED

added in API level 23
String ACTION_INTERRUPTION_FILTER_CHANGED

Intent that is broadcast when the state of getCurrentInterruptionFilter() changes. This broadcast is only sent to registered receivers.

Constant Value: "android.app.action.INTERRUPTION_FILTER_CHANGED"

ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED

added in API level 23
String ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED

Intent that is broadcast when the state of isNotificationPolicyAccessGranted() changes. This broadcast is only sent to registered receivers, and only to the apps that have changed.

Constant Value: "android.app.action.NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED"

ACTION_NOTIFICATION_POLICY_CHANGED

added in API level 23
String ACTION_NOTIFICATION_POLICY_CHANGED

Intent that is broadcast when the state of getNotificationPolicy() changes. This broadcast is only sent to registered receivers.

Constant Value: "android.app.action.NOTIFICATION_POLICY_CHANGED"

IMPORTANCE_DEFAULT

added in API level 24
int IMPORTANCE_DEFAULT

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

Constant Value: 3 (0x00000003)

IMPORTANCE_HIGH

added in API level 24
int IMPORTANCE_HIGH

Higher notification importance: shows everywhere, makes noise and peeks. May use full screen intents.

Constant Value: 4 (0x00000004)

IMPORTANCE_LOW

added in API level 24
int IMPORTANCE_LOW

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

Constant Value: 2 (0x00000002)

IMPORTANCE_MAX

added in API level 24
int IMPORTANCE_MAX

Unused.

Constant Value: 5 (0x00000005)

IMPORTANCE_MIN

added in API level 24
int IMPORTANCE_MIN

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

Constant Value: 1 (0x00000001)

IMPORTANCE_NONE

added in API level 24
int IMPORTANCE_NONE

A notification with no importance: does not show in the shade.

Constant Value: 0 (0x00000000)

IMPORTANCE_UNSPECIFIED

added in API level 24
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)

INTERRUPTION_FILTER_ALARMS

added in API level 23
int INTERRUPTION_FILTER_ALARMS

Interruption filter constant - Alarms only interruption filter - all notifications except those of category CATEGORY_ALARM are suppressed. Some audio streams are muted.

Constant Value: 4 (0x00000004)

INTERRUPTION_FILTER_ALL

added in API level 23
int INTERRUPTION_FILTER_ALL

Interruption filter constant - Normal interruption filter - no notifications are suppressed.

Constant Value: 1 (0x00000001)

INTERRUPTION_FILTER_NONE

added in API level 23
int INTERRUPTION_FILTER_NONE

Interruption filter constant - No interruptions filter - all notifications are suppressed and all audio streams (except those used for phone calls) and vibrations are muted.

Constant Value: 3 (0x00000003)

INTERRUPTION_FILTER_PRIORITY

added in API level 23
int INTERRUPTION_FILTER_PRIORITY

Interruption filter constant - Priority interruption filter - all notifications are suppressed except those that match the priority criteria. Some audio streams are muted. See priorityCallSenders, priorityCategories, priorityMessageSenders to define or query this criteria. Users can additionally specify packages that can bypass this interruption filter.

Constant Value: 2 (0x00000002)

INTERRUPTION_FILTER_UNKNOWN

added in API level 23
int INTERRUPTION_FILTER_UNKNOWN

Interruption filter constant - returned when the value is unavailable for any reason.

Constant Value: 0 (0x00000000)

Public methods

addAutomaticZenRule

added in API level 24
String addAutomaticZenRule (AutomaticZenRule automaticZenRule)

Creates the given zen rule.

Throws a SecurityException if policy access is granted to this package. See isNotificationPolicyAccessGranted().

Parameters
automaticZenRule AutomaticZenRule: the rule to create.

Returns
String The id of the newly created rule; null if the rule could not be created.

areNotificationsEnabled

added in API level 24
boolean areNotificationsEnabled ()

Returns whether notifications from the calling package are blocked.

Returns
boolean

cancel

added in API level 1
void cancel (int id)

Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar.

Parameters
id int

cancel

added in API level 5
void cancel (String tag, 
                int id)

Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar.

Parameters
tag String

id int

cancelAll

added in API level 1
void cancelAll ()

Cancel all previously shown notifications. See cancel(int) for the detailed behavior.

createNotificationChannel

added in API level 26
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 and description.

The name and description should only be changed if the locale changes or in response to the user renaming this channel. For example, if a user has a channel named 'John Doe' that represents messages from a 'John Doe', and 'John Doe' changes his name to 'John Smith,' the channel can be renamed to match. All other fields are ignored for channels that already exist.

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.

This value must never be null.

createNotificationChannelGroup

added in API level 26
void createNotificationChannelGroup (NotificationChannelGroup group)

Creates a group container for NotificationChannel objects. This can be used to rename an existing group.

Group information is only used for presentation, not for behavior. Groups are optional for channels, and you can have a mix of channels that belong to groups and channels that do not.

For example, if your application supports multiple accounts, and those accounts will have similar channels, you can create a group for each account with account specific labels instead of appending account information to each channel's label.

Parameters
group NotificationChannelGroup: The group to create

This value must never be null.

createNotificationChannelGroups

added in API level 26
void createNotificationChannelGroups (List<NotificationChannelGroup> groups)

Creates multiple notification channel groups.

Parameters
groups List: The list of groups to create

This value must never be null.

createNotificationChannels

added in API level 26
void createNotificationChannels (List<NotificationChannel> channels)

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

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

This value must never be null.

deleteNotificationChannel

added in API level 26
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.

Parameters
channelId String

deleteNotificationChannelGroup

added in API level 26
void deleteNotificationChannelGroup (String groupId)

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

Parameters
groupId String

getActiveNotifications

added in API level 23
StatusBarNotification[] getActiveNotifications ()

Recover a list of active notifications: ones that have been posted by the calling app that have not yet been dismissed by the user or cancel(String, int)ed by the app. Each notification is embedded in a StatusBarNotification object, including the original tag and id supplied to notify() (via getTag() and getId()) as well as a copy of the original Notification object (via getNotification()).

Returns
StatusBarNotification[] An array of StatusBarNotification.

getAutomaticZenRule

added in API level 24
AutomaticZenRule getAutomaticZenRule (String id)

Returns the AutomaticZenRule with the given id, if it exists and the caller has access.

Throws a SecurityException if policy access is granted to this package. See isNotificationPolicyAccessGranted().

Returns null if there are no zen rules that match the given id, or if the calling package doesn't own the matching rule. See getOwner().

Parameters
id String

Returns
AutomaticZenRule

getAutomaticZenRules

added in API level 24
Map<StringAutomaticZenRule> getAutomaticZenRules ()

Returns AutomaticZenRules owned by the caller.

Throws a SecurityException if policy access is granted to this package. See isNotificationPolicyAccessGranted().

Returns
Map<StringAutomaticZenRule>

getCurrentInterruptionFilter

added in API level 23
int getCurrentInterruptionFilter ()

Gets the current notification interruption filter.

The interruption filter defines which notifications are allowed to interrupt the user (e.g. via sound & vibration) and is applied globally.

Returns
int

Value is INTERRUPTION_FILTER_NONE, INTERRUPTION_FILTER_PRIORITY, INTERRUPTION_FILTER_ALARMS, INTERRUPTION_FILTER_ALL or INTERRUPTION_FILTER_UNKNOWN.

getImportance

added in API level 24
int getImportance ()

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

Returns
int

Value is IMPORTANCE_UNSPECIFIED, IMPORTANCE_NONE, IMPORTANCE_MIN, IMPORTANCE_LOW, IMPORTANCE_DEFAULT or IMPORTANCE_HIGH.

getNotificationChannel

added in API level 26
NotificationChannel getNotificationChannel (String channelId)

Returns the notification channel settings for a given channel id. The channel must belong to your package, or it will not be returned.

Parameters
channelId String

Returns
NotificationChannel

getNotificationChannelGroups

added in API level 26
List<NotificationChannelGroup> getNotificationChannelGroups ()

Returns all notification channel groups belonging to the calling app.

Returns
List<NotificationChannelGroup>

getNotificationChannels

added in API level 26
List<NotificationChannel> getNotificationChannels ()

Returns all notification channels belonging to the calling package.

Returns
List<NotificationChannel>

getNotificationPolicy

added in API level 23
NotificationManager.Policy getNotificationPolicy ()

Gets the current notification policy.

Only available if policy access is granted to this package. See isNotificationPolicyAccessGranted().

Returns
NotificationManager.Policy

isNotificationPolicyAccessGranted

added in API level 23
boolean isNotificationPolicyAccessGranted ()

Checks the ability to read/modify notification policy for the calling package.

Returns true if the calling package can read/modify notification policy.

Request policy access by sending the user to the activity that matches the system intent action ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS.

Use ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED to listen for user grant or denial of this access.

Returns
boolean

notify

added in API level 1
void notify (int id, 
                Notification notification)

Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

Parameters
id int: An identifier for this notification unique within your application.

notification Notification: A Notification object describing what to show the user. Must not be null.

notify

added in API level 5
void notify (String tag, 
                int id, 
                Notification notification)

Post a notification to be shown in the status bar. If a notification with the same tag and id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

Parameters
tag String: A string identifier for this notification. May be null.

id int: An identifier for this notification. The pair (tag, id) must be unique within your application.

notification Notification: A Notification object describing what to show the user. Must not be null.

removeAutomaticZenRule

added in API level 24
boolean removeAutomaticZenRule (String id)

Deletes the automatic zen rule with the given id.

Throws a SecurityException if policy access is granted to this package. See isNotificationPolicyAccessGranted().

Callers can only delete rules that they own. See getOwner().

Parameters
id String: the id of the rule to delete.

Returns
boolean Whether the rule was successfully deleted.

setInterruptionFilter

added in API level 23
void setInterruptionFilter (int interruptionFilter)

Sets the current notification interruption filter.

The interruption filter defines which notifications are allowed to interrupt the user (e.g. via sound & vibration) and is applied globally.

Only available if policy access is granted to this package. See isNotificationPolicyAccessGranted().

Parameters
interruptionFilter int

Value is INTERRUPTION_FILTER_NONE, INTERRUPTION_FILTER_PRIORITY, INTERRUPTION_FILTER_ALARMS, INTERRUPTION_FILTER_ALL or INTERRUPTION_FILTER_UNKNOWN.

setNotificationPolicy

added in API level 23
void setNotificationPolicy (NotificationManager.Policy policy)

Sets the current notification policy.

Only available if policy access is granted to this package. See isNotificationPolicyAccessGranted().

Parameters
policy NotificationManager.Policy: The new desired policy.

This value must never be null.

updateAutomaticZenRule

added in API level 24
boolean updateAutomaticZenRule (String id, 
                AutomaticZenRule automaticZenRule)

Updates the given zen rule.

Throws a SecurityException if policy access is granted to this package. See isNotificationPolicyAccessGranted().

Callers can only update rules that they own. See getOwner().

Parameters
id String: The id of the rule to update

automaticZenRule AutomaticZenRule: the rule to update.

Returns
boolean Whether the rule was successfully updated.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)