NotificationManager | Android Developers (original) (raw)
public class NotificationManager
extends [Object](/reference/java/lang/Object)
``
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](/reference/java/lang/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)](/reference/android/app/NotificationManager#cancel%28int%29)
or [cancel(java.lang.String, int)](/reference/android/app/NotificationManager#cancel%28java.lang.String,%20int%29)
method to clear this notification.
Summary
Nested classes | |
---|---|
class | NotificationManager.Policy Notification policy configuration. |
Constants | |
---|---|
String | ACTION_APP_BLOCK_STATE_CHANGED Intent that is broadcast when an application is blocked or unblocked. |
String | ACTION_AUTOMATIC_ZEN_RULE Activity Action: Launch an Automatic Zen Rule configuration screen Input: Optionally, EXTRA_AUTOMATIC_RULE_ID, if the configuration screen for an existing rule should be displayed. |
String | ACTION_AUTOMATIC_ZEN_RULE_STATUS_CHANGED Intent that is broadcast when the status of an AutomaticZenRule has changed. |
String | ACTION_CONSOLIDATED_NOTIFICATION_POLICY_CHANGED Intent that is broadcast when the state of getConsolidatedNotificationPolicy() changes. |
String | ACTION_INTERRUPTION_FILTER_CHANGED Intent that is broadcast when the state of getCurrentInterruptionFilter() changes. |
String | ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED Intent that is broadcast when a NotificationChannel is blocked (when NotificationChannel#getImportance() is IMPORTANCE_NONE) or unblocked (when NotificationChannel#getImportance() is anything other thanIMPORTANCE_NONE). |
String | ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED Intent that is broadcast when a NotificationChannelGroup isblocked or unblocked. |
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 | AUTOMATIC_RULE_STATUS_ACTIVATED Constant value for EXTRA_AUTOMATIC_ZEN_RULE_STATUS - the given rule has been activated by the user or cross device sync. |
int | AUTOMATIC_RULE_STATUS_DEACTIVATED Constant value for EXTRA_AUTOMATIC_ZEN_RULE_STATUS - the given rule has been deactivated ("snoozed") by the user. |
int | AUTOMATIC_RULE_STATUS_DISABLED Constant value for EXTRA_AUTOMATIC_ZEN_RULE_STATUS - the given rule currently exists but is disabled. |
int | AUTOMATIC_RULE_STATUS_ENABLED Constant value for EXTRA_AUTOMATIC_ZEN_RULE_STATUS - the given rule currently exists and is enabled. |
int | AUTOMATIC_RULE_STATUS_REMOVED Constant value for EXTRA_AUTOMATIC_ZEN_RULE_STATUS - the given rule has been deleted. |
int | AUTOMATIC_RULE_STATUS_UNKNOWN Constant value for EXTRA_AUTOMATIC_ZEN_RULE_STATUS - the current status of the rule is unknown at your target sdk version, and you should continue to provide state changes via setAutomaticZenRuleState(java.lang.String, android.service.notification.Condition). |
int | BUBBLE_PREFERENCE_ALL Indicates that all bubbles are allowed from the app. |
int | BUBBLE_PREFERENCE_NONE Indicates that the no bubbles are allowed from the app. |
int | BUBBLE_PREFERENCE_SELECTED Indicates that only notifications selected by the user will appear as bubbles. |
String | EXTRA_AUTOMATIC_RULE_ID Used as an optional string extra on ACTION_AUTOMATIC_ZEN_RULE intents. |
String | EXTRA_AUTOMATIC_ZEN_RULE_ID String extra for ACTION_AUTOMATIC_ZEN_RULE_STATUS_CHANGED containing the id of theAutomaticZenRule (see addAutomaticZenRule(android.app.AutomaticZenRule)) that has changed. |
String | EXTRA_AUTOMATIC_ZEN_RULE_STATUS Integer extra for ACTION_AUTOMATIC_ZEN_RULE_STATUS_CHANGED containing the state of the AutomaticZenRule. |
String | EXTRA_BLOCKED_STATE Extra for ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED orACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED containing the new blocked state as a boolean. |
String | EXTRA_NOTIFICATION_CHANNEL_GROUP_ID Extra for ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED containing the id of the NotificationChannelGroup which has a new blocked state. |
String | EXTRA_NOTIFICATION_CHANNEL_ID Extra for ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED containing the id of theNotificationChannel which has a new blocked state. |
String | EXTRA_NOTIFICATION_POLICY Extra for ACTION_NOTIFICATION_POLICY_CHANGED andACTION_CONSOLIDATED_NOTIFICATION_POLICY_CHANGED containing the newPolicy value. |
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 in the shade, and potentially in the status bar (see shouldHideSilentStatusBarIcons()), but is not audibly 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 categoryNotification#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. |
String | META_DATA_AUTOMATIC_RULE_TYPE A required meta-data tag for activities that handleACTION_AUTOMATIC_ZEN_RULE. |
String | META_DATA_RULE_INSTANCE_LIMIT An optional meta-data tag for activities that handleACTION_AUTOMATIC_ZEN_RULE. |
Public methods | |
---|---|
String | addAutomaticZenRule(AutomaticZenRule automaticZenRule) Creates the given zen rule. |
boolean | areAutomaticZenRulesUserManaged() Returns true if users can independently and fully manage AutomaticZenRule rules. |
boolean | areBubblesAllowed() This method was deprecated in API level 31. use getBubblePreference() instead. |
boolean | areBubblesEnabled() Returns whether bubbles are enabled at the feature level for the current user. |
boolean | areNotificationsEnabled() Returns whether notifications from the calling package are enabled. |
boolean | areNotificationsPaused() Returns whether notifications from this package are temporarily hidden. |
boolean | canNotifyAsPackage(String pkg) Returns whether you are allowed to post notifications on behalf of a given package, withnotifyAsPackage(java.lang.String, java.lang.String, int, android.app.Notification). |
boolean | canUseFullScreenIntent() Returns whether the calling app can send fullscreen intents. |
void | cancel(int id) Cancels a previously posted notification. |
void | cancel(String tag, int id) Cancels a previously posted notification. |
void | cancelAll() Cancel all previously shown notifications. |
void | cancelAsPackage(String targetPackage, String tag, int id) Cancels a previously posted notification. |
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(java.lang.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. |
int | getAutomaticZenRuleState(String id) Returns the current activation state of an AutomaticZenRule. |
Map<String, AutomaticZenRule> | getAutomaticZenRules() Returns AutomaticZenRules owned by the caller. |
int | getBubblePreference() Gets the bubble preference for the app. |
NotificationManager.Policy | getConsolidatedNotificationPolicy() Returns the currently applied notification policy. |
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. |
NotificationChannel | getNotificationChannel(String channelId, String conversationId) Returns the notification channel settings for a given channel andconversation 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. |
List<NotificationChannel> | getNotificationChannels() Returns all notification channels belonging to the calling package. |
String | getNotificationDelegate() Returns the delegate that can post notifications on your behalf, if there currently is one. |
NotificationManager.Policy | getNotificationPolicy() Gets the current user-specified default notification policy. |
boolean | isNotificationListenerAccessGranted(ComponentName listener) Checks whether the user has approved a givenNotificationListenerService. |
boolean | isNotificationPolicyAccessGranted() Checks the ability to modify notification do not disturb policy for the calling package. |
boolean | matchesCallFilter(Uri uri) Returns whether a call from the provided URI is permitted to notify the user. |
void | notify(int id, Notification notification) Post a notification to be shown in the status bar. |
void | notify(String tag, int id, Notification notification) Posts a notification to be shown in the status bar. |
void | notifyAsPackage(String targetPackage, String tag, int id, Notification notification) Posts a notification as a specified package to be shown in the status bar. |
boolean | removeAutomaticZenRule(String id) Deletes the automatic zen rule with the given id. |
void | setAutomaticZenRuleState(String id, Condition condition) Informs the notification manager that the state of an AutomaticZenRule has changed. |
final void | setInterruptionFilter(int interruptionFilter) Sets the current notification interruption filter. |
void | setNotificationDelegate(String delegate) Allows a package to post notifications on your behalf usingnotifyAsPackage(java.lang.String, java.lang.String, int, android.app.Notification). |
void | setNotificationPolicy(NotificationManager.Policy policy) Sets the current notification policy (which applies when setInterruptionFilter(int) is called with the INTERRUPTION_FILTER_PRIORITY value). |
boolean | shouldHideSilentStatusBarIcons() Returns whether the user wants silent notifications (see IMPORTANCE_LOW to appear in the status bar. |
boolean | updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) Updates the given zen rule. |
Inherited methods |
---|
From class java.lang.Object Object clone() Creates and returns a copy of this object. boolean equals(Object obj) Indicates whether some other object is "equal to" this one. void finalize() Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. finalClass<?> getClass() Returns the runtime class of this Object. int hashCode() Returns a hash code value for the object. final void notify() Wakes up a single thread that is waiting on this object's monitor. final void notifyAll() Wakes up all threads that are waiting on this object's monitor. String toString() Returns a string representation of the object. final void wait(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. final void wait(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. final void wait() Causes the current thread to wait until it is awakened, typically by being notified or interrupted. |
Constants
ACTION_APP_BLOCK_STATE_CHANGED
public static final String ACTION_APP_BLOCK_STATE_CHANGED
Intent that is broadcast when an application is blocked or unblocked. This broadcast is only sent to the app whose block state has changed. Input: nothing Output: [EXTRA_BLOCKED_STATE](/reference/android/app/NotificationManager#EXTRA%5FBLOCKED%5FSTATE)
Constant Value: "android.app.action.APP_BLOCK_STATE_CHANGED"
ACTION_AUTOMATIC_ZEN_RULE
public static final String ACTION_AUTOMATIC_ZEN_RULE
Activity Action: Launch an Automatic Zen Rule configuration screen
Input: Optionally, [EXTRA_AUTOMATIC_RULE_ID](/reference/android/app/NotificationManager#EXTRA%5FAUTOMATIC%5FRULE%5FID)
, if the configuration screen for an existing rule should be displayed. If the rule id is missing or null, apps should display a configuration screen where users can create a new instance of the rule.
Output: Nothing
You can have multiple activities handling this intent, if you support multiple[rules](/reference/android/app/AutomaticZenRule)
. In order for the system to properly display all of your rule types so that users can create new instances or configure existing ones, you need to add some extra metadata ([META_DATA_AUTOMATIC_RULE_TYPE](/reference/android/app/NotificationManager#META%5FDATA%5FAUTOMATIC%5FRULE%5FTYPE)
) to your activity tag in your manifest. If you'd like to limit the number of rules a user can create from this flow, you can additionally optionally include[META_DATA_RULE_INSTANCE_LIMIT](/reference/android/app/NotificationManager#META%5FDATA%5FRULE%5FINSTANCE%5FLIMIT)
. For example,
Constant Value: "android.app.action.AUTOMATIC_ZEN_RULE"
ACTION_CONSOLIDATED_NOTIFICATION_POLICY_CHANGED
public static final String ACTION_CONSOLIDATED_NOTIFICATION_POLICY_CHANGED
Intent that is broadcast when the state of [getConsolidatedNotificationPolicy()](/reference/android/app/NotificationManager#getConsolidatedNotificationPolicy%28%29)
changes.
This broadcast is only sent to registered receivers and receivers in packages that have been granted Do Not Disturb access (see [isNotificationPolicyAccessGranted()](/reference/android/app/NotificationManager#isNotificationPolicyAccessGranted%28%29)
).
Constant Value: "android.app.action.CONSOLIDATED_NOTIFICATION_POLICY_CHANGED"
ACTION_INTERRUPTION_FILTER_CHANGED
public static final String ACTION_INTERRUPTION_FILTER_CHANGED
Intent that is broadcast when the state of getCurrentInterruptionFilter() changes.
This broadcast is only sent to registered receivers and (starting from[Build.VERSION_CODES#Q](/reference/android/os/Build.VERSION%5FCODES#Q)
) receivers in packages that have been granted Do Not Disturb access (see [isNotificationPolicyAccessGranted()](/reference/android/app/NotificationManager#isNotificationPolicyAccessGranted%28%29)
).
Constant Value: "android.app.action.INTERRUPTION_FILTER_CHANGED"
ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED
public static final String ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED
Intent that is broadcast when a [NotificationChannelGroup](/reference/android/app/NotificationChannelGroup)
is[blocked](/reference/android/app/NotificationChannelGroup#isBlocked%28%29)
or unblocked. This broadcast is only sent to the app that owns the channel group that has changed. Input: nothing Output: [EXTRA_NOTIFICATION_CHANNEL_GROUP_ID](/reference/android/app/NotificationManager#EXTRA%5FNOTIFICATION%5FCHANNEL%5FGROUP%5FID)
Output: [EXTRA_BLOCKED_STATE](/reference/android/app/NotificationManager#EXTRA%5FBLOCKED%5FSTATE)
Constant Value: "android.app.action.NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED"
ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED
public static final String ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED
Intent that is broadcast when the state of [isNotificationPolicyAccessGranted()](/reference/android/app/NotificationManager#isNotificationPolicyAccessGranted%28%29)
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"
AUTOMATIC_RULE_STATUS_ACTIVATED
public static final int AUTOMATIC_RULE_STATUS_ACTIVATED
Constant value for [EXTRA_AUTOMATIC_ZEN_RULE_STATUS](/reference/android/app/NotificationManager#EXTRA%5FAUTOMATIC%5FZEN%5FRULE%5FSTATUS)
- the given rule has been activated by the user or cross device sync. Sent from[Build.VERSION_CODES#VANILLA_ICE_CREAM](/reference/android/os/Build.VERSION%5FCODES#VANILLA%5FICE%5FCREAM)
. If the rule owner has a mode that includes a DND component, the rule owner should activate any extra behavior that's part of that mode in response to this broadcast.
Constant Value: 4 (0x00000004)
BUBBLE_PREFERENCE_ALL
public static final int BUBBLE_PREFERENCE_ALL
Indicates that all bubbles are allowed from the app. If the app sends bubbles, the bubble will appear along with the notification.
Constant Value: 1 (0x00000001)
BUBBLE_PREFERENCE_NONE
public static final int BUBBLE_PREFERENCE_NONE
Indicates that the no bubbles are allowed from the app. If the app sends bubbles, only the notification will appear. The notification will have an affordance allowing the user to bubble it. If the user selects this affordance, that notification is approved to bubble and the apps' bubble preference will be upgraded to [BUBBLE_PREFERENCE_SELECTED](/reference/android/app/NotificationManager#BUBBLE%5FPREFERENCE%5FSELECTED)
.
Constant Value: 0 (0x00000000)
BUBBLE_PREFERENCE_SELECTED
public static final int BUBBLE_PREFERENCE_SELECTED
Indicates that only notifications selected by the user will appear as bubbles. If the app sends bubbles that haven't been selected, only the notification appear. If the bubble has been approved by the user, it will appear along with the notification.
Constant Value: 2 (0x00000002)
IMPORTANCE_DEFAULT
public static final int IMPORTANCE_DEFAULT
Default notification importance: shows everywhere, makes noise, but does not visually intrude.
Constant Value: 3 (0x00000003)
IMPORTANCE_HIGH
public static final int IMPORTANCE_HIGH
Higher notification importance: shows everywhere, makes noise and peeks. May use full screen intents.
Constant Value: 4 (0x00000004)
IMPORTANCE_LOW
public static final int IMPORTANCE_LOW
Low notification importance: Shows in the shade, and potentially in the status bar (see [shouldHideSilentStatusBarIcons()](/reference/android/app/NotificationManager#shouldHideSilentStatusBarIcons%28%29)
), but is not audibly intrusive.
Constant Value: 2 (0x00000002)
IMPORTANCE_MAX
public static final int IMPORTANCE_MAX
Unused.
Constant Value: 5 (0x00000005)
IMPORTANCE_MIN
public static final int IMPORTANCE_MIN
Min notification importance: only shows in the shade, below the fold. This should not be used with [Service.startForeground](/reference/android/app/Service#startForeground%28int,%20android.app.Notification%29)
since a foreground service is supposed to be something the user cares about so it does not make semantic sense to mark its notification as minimum importance. If you do this as of Android version [Build.VERSION_CODES.O](/reference/android/os/Build.VERSION%5FCODES#O)
, the system will show a higher-priority notification about your app running in the background.
Constant Value: 1 (0x00000001)
IMPORTANCE_NONE
public static final int IMPORTANCE_NONE
A notification with no importance: does not show in the shade.
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)
INTERRUPTION_FILTER_ALARMS
public static final int INTERRUPTION_FILTER_ALARMS
[Interruption filter](/reference/android/app/NotificationManager#getCurrentInterruptionFilter%28%29)
constant - Alarms only interruption filter - all notifications except those of category[Notification#CATEGORY_ALARM](/reference/android/app/Notification#CATEGORY%5FALARM)
are suppressed. Some audio streams are muted.
Constant Value: 4 (0x00000004)
INTERRUPTION_FILTER_ALL
public static final int INTERRUPTION_FILTER_ALL
[Interruption filter](/reference/android/app/NotificationManager#getCurrentInterruptionFilter%28%29)
constant - Normal interruption filter - no notifications are suppressed.
Constant Value: 1 (0x00000001)
INTERRUPTION_FILTER_NONE
public static final int INTERRUPTION_FILTER_NONE
[Interruption filter](/reference/android/app/NotificationManager#getCurrentInterruptionFilter%28%29)
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_UNKNOWN
public static final int INTERRUPTION_FILTER_UNKNOWN
[Interruption filter](/reference/android/app/NotificationManager#getCurrentInterruptionFilter%28%29)
constant - returned when the value is unavailable for any reason.
Constant Value: 0 (0x00000000)
META_DATA_AUTOMATIC_RULE_TYPE
public static final String META_DATA_AUTOMATIC_RULE_TYPE
A required meta-data
tag for activities that handle[ACTION_AUTOMATIC_ZEN_RULE](/reference/android/app/NotificationManager#ACTION%5FAUTOMATIC%5FZEN%5FRULE)
. This tag should contain a localized name of the type of the zen rule provided by the activity.
Constant Value: "android.service.zen.automatic.ruleType"
META_DATA_RULE_INSTANCE_LIMIT
public static final String META_DATA_RULE_INSTANCE_LIMIT
An optional meta-data
tag for activities that handle[ACTION_AUTOMATIC_ZEN_RULE](/reference/android/app/NotificationManager#ACTION%5FAUTOMATIC%5FZEN%5FRULE)
. This tag should contain the maximum number of rule instances that can be created for this rule type. Omit or enter a value <= 0 to allow unlimited instances.
Constant Value: "android.service.zen.automatic.ruleInstanceLimit"
Public methods
addAutomaticZenRule
public String addAutomaticZenRule (AutomaticZenRule automaticZenRule)
Creates the given zen rule.
Throws a SecurityException if policy access is not granted to this package. See [isNotificationPolicyAccessGranted()](/reference/android/app/NotificationManager#isNotificationPolicyAccessGranted%28%29)
.
Parameters | |
---|---|
automaticZenRule | AutomaticZenRule: the rule to create. |
Returns | |
---|---|
String | The id of the newly created rule; null if the rule could not be created. |
areBubblesAllowed
public boolean areBubblesAllowed ()
This method was deprecated in API level 31.
use [getBubblePreference()](/reference/android/app/NotificationManager#getBubblePreference%28%29)
instead.
Gets whether all notifications posted by this app can appear outside of the notification shade, floating over other apps' content.
This value will be ignored for notifications that are posted to channels that do not allow bubbles ([NotificationChannel#canBubble()](/reference/android/app/NotificationChannel#canBubble%28%29)
).
Returns |
---|
boolean |
areBubblesEnabled
public boolean areBubblesEnabled ()
Returns whether bubbles are enabled at the feature level for the current user. When enabled, notifications able to bubble will display an affordance allowing the user to bubble them.
Returns |
---|
boolean |
areNotificationsEnabled
public boolean areNotificationsEnabled ()
Returns whether notifications from the calling package are enabled.
Returns |
---|
boolean |
areNotificationsPaused
public boolean areNotificationsPaused ()
Returns whether notifications from this package are temporarily hidden. This could be done because the package was marked as distracting to the user viaPackageManager#setDistractingPackageRestrictions(String[], int)
or because the package is PackageManager#setPackagesSuspended(String[], boolean, PersistableBundle, PersistableBundle, SuspendDialogInfo) suspended
.
Returns |
---|
boolean |
cancel
public void cancel (int id)
Cancels a previously posted notification.
If the notification does not currently represent a[foreground service](/reference/android/app/Service#startForeground%28int,%20android.app.Notification%29)
or a[user-initiated job](/reference/android/app/job/JobInfo.Builder#setUserInitiated%28boolean%29)
, it will be removed from the UI and live[notification listeners](/reference/android/service/notification/NotificationListenerService)
will be informed so they can remove the notification from their UIs.
Parameters | |
---|---|
id | int |
cancel
public void cancel (String tag, int id)
Cancels a previously posted notification.
If the notification does not currently represent a[foreground service](/reference/android/app/Service#startForeground%28int,%20android.app.Notification%29)
or a[user-initiated job](/reference/android/app/job/JobInfo.Builder#setUserInitiated%28boolean%29)
, it will be removed from the UI and live[notification listeners](/reference/android/service/notification/NotificationListenerService)
will be informed so they can remove the notification from their UIs.
Parameters | |
---|---|
tag | String: This value may be null. |
id | int |
cancelAll
public void cancelAll ()
Cancel all previously shown notifications. See [cancel(int)](/reference/android/app/NotificationManager#cancel%28int%29)
for the detailed behavior.
cancelAsPackage
public void cancelAsPackage (String targetPackage, String tag, int id)
Cancels a previously posted notification.
If the notification does not currently represent a[foreground service](/reference/android/app/Service#startForeground%28int,%20android.app.Notification%29)
or a[user-initiated job](/reference/android/app/job/JobInfo.Builder#setUserInitiated%28boolean%29)
, it will be removed from the UI and live[notification listeners](/reference/android/service/notification/NotificationListenerService)
will be informed so they can remove the notification from their UIs.
This method may be used by [a notification delegate](/reference/android/app/NotificationManager#getNotificationDelegate%28%29)
to cancel notifications that they have posted via [notifyAsPackage(java.lang.String, java.lang.String, int, android.app.Notification)](/reference/android/app/NotificationManager#notifyAsPackage%28java.lang.String,%20java.lang.String,%20int,%20android.app.Notification%29)
.
Parameters | |
---|---|
targetPackage | String: The package to cancel the notification as. If this package is not your package, you can only cancel notifications you posted with {@link #notifyAsPackage(java.lang.String,java.lang.String,int,android.app.Notification). |
tag | String: A string identifier for this notification. May be null. |
id | int: An identifier for this notification. |
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 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 'Messages' and the user changes their locale, this channel's name should be updated with the translation of 'Messages' in the new locale.
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.
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 cannot be null. |
createNotificationChannelGroup
public void createNotificationChannelGroup (NotificationChannelGroup group)
Creates a group container for [NotificationChannel](/reference/android/app/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 cannot be null. |
createNotificationChannelGroups
public void createNotificationChannelGroups (List<NotificationChannelGroup> groups)
Creates multiple notification channel groups.
Parameters | |
---|---|
groups | List: The list of groups to create This value cannot be null. |
deleteNotificationChannel
public void deleteNotificationChannel (String channelId)
Deletes the given notification channel.
If you [create](/reference/android/app/NotificationManager#createNotificationChannel%28android.app.NotificationChannel%29)
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
public void deleteNotificationChannelGroup (String groupId)
Deletes the given notification channel group, and all notification channels that belong to it.
Parameters | |
---|---|
groupId | String |
getNotificationChannelGroup
public NotificationChannelGroup getNotificationChannelGroup (String channelGroupId)
Returns the notification channel group settings for a given channel group id. The channel group must belong to your package, or null will be returned.
Parameters | |
---|---|
channelGroupId | String |
Returns |
---|
NotificationChannelGroup |
getNotificationDelegate
public String getNotificationDelegate ()
Returns the [delegate](/reference/android/app/NotificationManager#setNotificationDelegate%28java.lang.String%29)
that can post notifications on your behalf, if there currently is one.
Returns | |
---|---|
String | This value may be null. |
isNotificationListenerAccessGranted
public boolean isNotificationListenerAccessGranted (ComponentName listener)
Checks whether the user has approved a given[NotificationListenerService](/reference/android/service/notification/NotificationListenerService)
.
The listener service must belong to the calling app.
Apps can request notification listener access by sending the user to the activity that matches the system intent action[Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS](/reference/android/provider/Settings#ACTION%5FNOTIFICATION%5FLISTENER%5FSETTINGS)
.
Parameters | |
---|---|
listener | ComponentName |
Returns |
---|
boolean |
isNotificationPolicyAccessGranted
public boolean isNotificationPolicyAccessGranted ()
Checks the ability to modify notification do not disturb policy for the calling package.
Returns true if the calling package can modify notification policy.
Apps can request policy access by sending the user to the activity that matches the system intent action [Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS](/reference/android/provider/Settings#ACTION%5FNOTIFICATION%5FPOLICY%5FACCESS%5FSETTINGS)
.
Use [ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED](/reference/android/app/NotificationManager#ACTION%5FNOTIFICATION%5FPOLICY%5FACCESS%5FGRANTED%5FCHANGED)
to listen for user grant or denial of this access.
Returns |
---|
boolean |
matchesCallFilter
public boolean matchesCallFilter (Uri uri)
Returns whether a call from the provided URI is permitted to notify the user.
A true return value indicates one of the following: Do Not Disturb is not currently active; or the caller is a repeat caller and the current policy allows interruptions from repeat callers; or the caller is in the user's set of contacts whose calls are allowed to interrupt Do Not Disturb.
If Do Not Disturb is enabled and either no interruptions or only alarms are allowed, this method will return false regardless of input.
The provided URI should be a tel:
or mailto:
schema URI indicating the source of the call. For an accurate answer regarding whether the caller matches the user's permitted contacts, the path part of the URI must match an entry the Contacts database in the appropriate column.
Passing in a [ContactsContract.Contacts.CONTENT_LOOKUP_URI](/reference/android/provider/ContactsContract.Contacts#CONTENT%5FLOOKUP%5FURI)
is also permissible, but should only be used for priority contact interruptions and may not provide accurate results in the case of repeat callers.
See also [Person.Builder#setUri](/reference/android/app/Person.Builder#setUri%28java.lang.String%29)
and[ContactsContract.Contacts.CONTENT_LOOKUP_URI](/reference/android/provider/ContactsContract.Contacts#CONTENT%5FLOOKUP%5FURI)
for more information.
Callers of this method must have notification listener access, permission to read contacts, or have system permissions.
NOTE: This method calls into Contacts, which may take some time, and should not be called on the main thread.
This method may take several seconds to complete, so it should only be called from a worker thread.
Parameters | |
---|---|
uri | Uri: A URI representing a caller. Must not be null. |
Returns | |
---|---|
boolean | A boolean indicating whether a call from the URI provided would be allowed to interrupt the user given the current filter. |
notify
public 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
public void notify (String tag, int id, Notification notification)
Posts 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. All [listener services](/reference/android/service/notification/NotificationListenerService)
will be granted [Intent#FLAG_GRANT_READ_URI_PERMISSION](/reference/android/content/Intent#FLAG%5FGRANT%5FREAD%5FURI%5FPERMISSION)
access to any [uris](/reference/android/net/Uri)
provided on this notification or the[NotificationChannel](/reference/android/app/NotificationChannel)
this notification is posted to using[Context#grantUriPermission(String, Uri, int)](/reference/android/content/Context#grantUriPermission%28java.lang.String,%20android.net.Uri,%20int%29)
. Permission will be revoked when the notification is canceled, or you can revoke permissions with[Context#revokeUriPermission(Uri, int)](/reference/android/content/Context#revokeUriPermission%28android.net.Uri,%20int%29)
.
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. |
notifyAsPackage
public void notifyAsPackage (String targetPackage, String tag, int id, Notification notification)
Posts a notification as a specified package to be shown in the status bar. If a notification with the same tag and id has already been posted for that package and has not yet been canceled, it will be replaced by the updated information. All [listener services](/reference/android/service/notification/NotificationListenerService)
will be granted [Intent#FLAG_GRANT_READ_URI_PERMISSION](/reference/android/content/Intent#FLAG%5FGRANT%5FREAD%5FURI%5FPERMISSION)
access to any [uris](/reference/android/net/Uri)
provided on this notification or the[NotificationChannel](/reference/android/app/NotificationChannel)
this notification is posted to using[Context#grantUriPermission(String, Uri, int)](/reference/android/content/Context#grantUriPermission%28java.lang.String,%20android.net.Uri,%20int%29)
. Permission will be revoked when the notification is canceled, or you can revoke permissions with[Context#revokeUriPermission(Uri, int)](/reference/android/content/Context#revokeUriPermission%28android.net.Uri,%20int%29)
.
Parameters | |
---|---|
targetPackage | String: The package to post the notification as. The package must have granted you access to post notifications on their behalf withsetNotificationDelegate(java.lang.String). This value cannot be null. |
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
public boolean removeAutomaticZenRule (String id)
Deletes the automatic zen rule with the given id.
Throws a SecurityException if policy access is not granted to this package. See [isNotificationPolicyAccessGranted()](/reference/android/app/NotificationManager#isNotificationPolicyAccessGranted%28%29)
.
Callers can only delete rules that they own. See [AutomaticZenRule#getOwner](/reference/android/app/AutomaticZenRule#getOwner%28%29)
.
Parameters | |
---|---|
id | String: the id of the rule to delete. |
Returns | |
---|---|
boolean | Whether the rule was successfully deleted. |
setAutomaticZenRuleState
public void setAutomaticZenRuleState (String id, Condition condition)
Informs the notification manager that the state of an [AutomaticZenRule](/reference/android/app/AutomaticZenRule)
has changed. Use this method to put the system into Do Not Disturb mode or request that it exits Do Not Disturb mode. The calling app must own the provided [AutomaticZenRule](/reference/android/app/AutomaticZenRule)
.
This method can be used in conjunction with or as a replacement to[ConditionProviderService.notifyCondition(Condition)](/reference/android/service/notification/ConditionProviderService#notifyCondition%28android.service.notification.Condition%29)
.
Parameters | |
---|---|
id | String: The id of the rule whose state should change This value cannot be null. |
condition | Condition: The new state of this rule This value cannot be null. |
setNotificationDelegate
public void setNotificationDelegate (String delegate)
Allows a package to post notifications on your behalf using[notifyAsPackage(java.lang.String, java.lang.String, int, android.app.Notification)](/reference/android/app/NotificationManager#notifyAsPackage%28java.lang.String,%20java.lang.String,%20int,%20android.app.Notification%29)
. This can be used to allow persistent processes to post notifications based on messages received on your behalf from the cloud, without your process having to wake up. You can check if you have an allowed delegate with [getNotificationDelegate()](/reference/android/app/NotificationManager#getNotificationDelegate%28%29)
and revoke your delegate by passing null to this method.
Parameters | |
---|---|
delegate | String: Package name of the app which can send notifications on your behalf. This value may be null. |
shouldHideSilentStatusBarIcons
public boolean shouldHideSilentStatusBarIcons ()
Returns whether the user wants silent notifications (see [IMPORTANCE_LOW](/reference/android/app/NotificationManager#IMPORTANCE%5FLOW)
to appear in the status bar.
Only available for [notification listeners](/reference/android/app/NotificationManager#isNotificationListenerAccessGranted%28android.content.ComponentName%29)
.
Returns |
---|
boolean |