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.
You do not instantiate this class directly; instead, retrieve it through
getSystemService(Class.
Developer Guides
For a guide to creating notifications, read the Status Bar Notifications developer guide.
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 |
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
|
int |
INTERRUPTION_FILTER_ALL
|
int |
INTERRUPTION_FILTER_NONE
|
int |
INTERRUPTION_FILTER_PRIORITY
|
int |
INTERRUPTION_FILTER_UNKNOWN
|
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 |
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 |
AutomaticZenRule
|
getAutomaticZenRule(String id)
Returns the AutomaticZenRule with the given id, if it exists and the caller has access. |
Map<String, AutomaticZenRule>
|
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 | |
|---|---|
java.lang.Object
| |
Constants
ACTION_INTERRUPTION_FILTER_CHANGED
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
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
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
int IMPORTANCE_DEFAULT
Default notification importance: shows everywhere, makes noise, but does not visually intrude.
Constant Value: 3 (0x00000003)
IMPORTANCE_HIGH
int IMPORTANCE_HIGH
Higher notification importance: shows everywhere, makes noise and peeks. May use full screen intents.
Constant Value: 4 (0x00000004)
IMPORTANCE_LOW
int IMPORTANCE_LOW
Low notification importance: shows everywhere, but is not intrusive.
Constant Value: 2 (0x00000002)
IMPORTANCE_MIN
int IMPORTANCE_MIN
Min notification importance: only shows in the shade, below the fold.
Constant Value: 1 (0x00000001)
IMPORTANCE_NONE
int IMPORTANCE_NONE
A notification with no importance: does not show in the shade.
Constant Value: 0 (0x00000000)
IMPORTANCE_UNSPECIFIED
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
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
int INTERRUPTION_FILTER_ALL
Interruption filter constant -
Normal interruption filter - no notifications are suppressed.
Constant Value: 1 (0x00000001)
INTERRUPTION_FILTER_NONE
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
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
int INTERRUPTION_FILTER_UNKNOWN
Interruption filter constant - returned when
the value is unavailable for any reason.
Constant Value: 0 (0x00000000)
Public methods
addAutomaticZenRule
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
boolean areNotificationsEnabled ()
Returns whether notifications from the calling package are blocked.
| Returns | |
|---|---|
boolean |
|
cancel
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
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
void cancelAll ()
Cancel all previously shown notifications. See cancel(int) for the
detailed behavior.
createNotificationChannel
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 |
createNotificationChannelGroup
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 |
createNotificationChannelGroups
void createNotificationChannelGroups (List<NotificationChannelGroup> groups)
Creates multiple notification channel groups.
| Parameters | |
|---|---|
groups |
List: The list of groups to create
This value must never be |
createNotificationChannels
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 |
deleteNotificationChannel
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
void deleteNotificationChannelGroup (String groupId)
Deletes the given notification channel group, and all notification channels that belong to it.
| Parameters | |
|---|---|
groupId |
String |
getActiveNotifications
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
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
Map<String, AutomaticZenRule> getAutomaticZenRules ()
Returns AutomaticZenRules owned by the caller.
Throws a SecurityException if policy access is granted to this package.
See isNotificationPolicyAccessGranted().
| Returns | |
|---|---|
Map<String, AutomaticZenRule> |
|
getCurrentInterruptionFilter
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 |
getImportance
int getImportance ()
Returns the user specified importance for notifications from the calling package.
| Returns | |
|---|---|
int |
Value is |
getNotificationChannel
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
List<NotificationChannelGroup> getNotificationChannelGroups ()
Returns all notification channel groups belonging to the calling app.
| Returns | |
|---|---|
List<NotificationChannelGroup> |
|
getNotificationChannels
List<NotificationChannel> getNotificationChannels ()
Returns all notification channels belonging to the calling package.
| Returns | |
|---|---|
List<NotificationChannel> |
|
getNotificationPolicy
NotificationManager.Policy getNotificationPolicy ()
Gets the current notification policy.
Only available if policy access is granted to this package.
See isNotificationPolicyAccessGranted().
| Returns | |
|---|---|
NotificationManager.Policy |
|
isNotificationPolicyAccessGranted
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
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
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
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
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 |
setNotificationPolicy
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 |
updateAutomaticZenRule
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. |
Interfaces
- ActionBar.OnMenuVisibilityListener
- ActionBar.OnNavigationListener
- ActionBar.TabListener
- AlarmManager.OnAlarmListener
- Application.ActivityLifecycleCallbacks
- Application.OnProvideAssistDataListener
- AppOpsManager.OnOpChangedListener
- DatePickerDialog.OnDateSetListener
- FragmentBreadCrumbs.OnBreadCrumbClickListener
- FragmentManager.BackStackEntry
- FragmentManager.OnBackStackChangedListener
- KeyguardManager.OnKeyguardExitResult
- LoaderManager.LoaderCallbacks
- Notification.Action.Extender
- Notification.Extender
- PendingIntent.OnFinished
- SearchManager.OnCancelListener
- SearchManager.OnDismissListener
- SharedElementCallback.OnSharedElementsReadyListener
- TimePickerDialog.OnTimeSetListener
- UiAutomation.AccessibilityEventFilter
- UiAutomation.OnAccessibilityEventListener
Classes
- ActionBar
- ActionBar.LayoutParams
- ActionBar.Tab
- Activity
- ActivityGroup
- ActivityManager
- ActivityManager.AppTask
- ActivityManager.MemoryInfo
- ActivityManager.ProcessErrorStateInfo
- ActivityManager.RecentTaskInfo
- ActivityManager.RunningAppProcessInfo
- ActivityManager.RunningServiceInfo
- ActivityManager.RunningTaskInfo
- ActivityManager.TaskDescription
- ActivityOptions
- AlarmManager
- AlarmManager.AlarmClockInfo
- AlertDialog
- AlertDialog.Builder
- AliasActivity
- Application
- ApplicationErrorReport
- ApplicationErrorReport.AnrInfo
- ApplicationErrorReport.BatteryInfo
- ApplicationErrorReport.CrashInfo
- ApplicationErrorReport.RunningServiceInfo
- AppOpsManager
- AutomaticZenRule
- DatePickerDialog
- Dialog
- DialogFragment
- DownloadManager
- DownloadManager.Query
- DownloadManager.Request
- ExpandableListActivity
- Fragment
- Fragment.SavedState
- FragmentBreadCrumbs
- FragmentContainer
- FragmentController
- FragmentHostCallback
- FragmentManager
- FragmentManager.FragmentLifecycleCallbacks
- FragmentManagerNonConfig
- FragmentTransaction
- Instrumentation
- Instrumentation.ActivityMonitor
- Instrumentation.ActivityResult
- IntentService
- KeyguardManager
- KeyguardManager.KeyguardDismissCallback
- KeyguardManager.KeyguardLock
- LauncherActivity
- LauncherActivity.IconResizer
- LauncherActivity.ListItem
- ListActivity
- ListFragment
- LoaderManager
- LocalActivityManager
- MediaRouteActionProvider
- MediaRouteButton
- NativeActivity
- Notification
- Notification.Action
- Notification.Action.Builder
- Notification.Action.WearableExtender
- Notification.BigPictureStyle
- Notification.BigTextStyle
- Notification.Builder
- Notification.CarExtender
- Notification.CarExtender.Builder
- Notification.CarExtender.UnreadConversation
- Notification.DecoratedCustomViewStyle
- Notification.DecoratedMediaCustomViewStyle
- Notification.InboxStyle
- Notification.MediaStyle
- Notification.MessagingStyle
- Notification.MessagingStyle.Message
- Notification.Style
- Notification.WearableExtender
- NotificationChannel
- NotificationChannelGroup
- NotificationManager
- NotificationManager.Policy
- PendingIntent
- PictureInPictureParams
- PictureInPictureParams.Builder
- Presentation
- ProgressDialog
- RemoteAction
- RemoteInput
- RemoteInput.Builder
- SearchableInfo
- SearchManager
- Service
- SharedElementCallback
- TabActivity
- TaskStackBuilder
- TimePickerDialog
- UiAutomation
- UiModeManager
- VoiceInteractor
- VoiceInteractor.AbortVoiceRequest
- VoiceInteractor.CommandRequest
- VoiceInteractor.CompleteVoiceRequest
- VoiceInteractor.ConfirmationRequest
- VoiceInteractor.PickOptionRequest
- VoiceInteractor.PickOptionRequest.Option
- VoiceInteractor.Prompt
- VoiceInteractor.Request
- WallpaperInfo
- WallpaperManager
Exceptions