public class NotificationManager extends Object
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
Context.getSystemService(java.lang.String)
.
For a guide to creating notifications, read the Status Bar Notifications developer guide.
Modifier and Type | Class and Description |
---|---|
static interface |
NotificationManager.Importance |
static interface |
NotificationManager.InterruptionFilter |
static class |
NotificationManager.Policy
Notification policy configuration.
|
Modifier and Type | Field and Description |
---|---|
static String |
ACTION_EFFECTS_SUPPRESSOR_CHANGED
Intent that is broadcast when the state of
getEffectsSuppressor() changes. |
static String |
ACTION_INTERRUPTION_FILTER_CHANGED
Intent that is broadcast when the state of getCurrentInterruptionFilter() changes.
|
static String |
ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL
Intent that is broadcast when the state of getCurrentInterruptionFilter() changes.
|
static String |
ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED
Intent that is broadcast when the state of
isNotificationPolicyAccessGranted()
changes. |
static String |
ACTION_NOTIFICATION_POLICY_CHANGED
Intent that is broadcast when the state of getNotificationPolicy() changes.
|
static int |
IMPORTANCE_DEFAULT
Default notification importance: shows everywhere, allowed to makes noise,
but does not visually intrude.
|
static int |
IMPORTANCE_HIGH
Higher notification importance: shows everywhere, allowed to makes noise and peek.
|
static int |
IMPORTANCE_LOW
Low notification importance: shows everywhere, but is not intrusive.
|
static int |
IMPORTANCE_MAX
Highest notification importance: shows everywhere, allowed to makes noise, peek, and
use full screen intents.
|
static int |
IMPORTANCE_MIN
Min notification importance: only shows in the shade, below the fold.
|
static int |
IMPORTANCE_NONE
A notification with no importance: shows nowhere, is blocked.
|
static int |
IMPORTANCE_UNSPECIFIED
Value signifying that the user has not expressed an importance.
|
static int |
INTERRUPTION_FILTER_ALARMS
Interruption filter constant -
Alarms only interruption filter - all notifications except those of category
Notification.CATEGORY_ALARM are suppressed. |
static int |
INTERRUPTION_FILTER_ALL
Interruption filter constant -
Normal interruption filter - no notifications are suppressed. |
static 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. |
static int |
INTERRUPTION_FILTER_PRIORITY
Interruption filter constant -
Priority interruption filter - all notifications are suppressed except those that match
the priority criteria. |
static int |
INTERRUPTION_FILTER_UNKNOWN
Interruption filter constant - returned when
the value is unavailable for any reason. |
static int |
VISIBILITY_NO_OVERRIDE
Value signifying that the user has not expressed a per-app visibility override value.
|
Modifier and Type | Method and Description |
---|---|
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 |
cancelAsUser(String tag,
int id,
UserHandle user) |
static NotificationManager |
from(Context context) |
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<String,AutomaticZenRule> |
getAutomaticZenRules()
Returns AutomaticZenRules owned by the caller.
|
int |
getCurrentInterruptionFilter()
Gets the current notification interruption filter.
|
ComponentName |
getEffectsSuppressor() |
int |
getImportance()
Returns the user specified importance for notifications from the calling package.
|
NotificationManager.Policy |
getNotificationPolicy()
Gets the current notification policy.
|
ArraySet<String> |
getPackagesRequestingNotificationPolicyAccess() |
int |
getRuleInstanceCount(ComponentName owner) |
static INotificationManager |
getService() |
int |
getZenMode() |
ZenModeConfig |
getZenModeConfig() |
boolean |
isNotificationPolicyAccessGranted()
Checks the ability to read/modify notification policy for the calling package.
|
boolean |
isNotificationPolicyAccessGrantedForPackage(String pkg) |
boolean |
isSystemConditionProviderEnabled(String path) |
boolean |
matchesCallFilter(Bundle extras) |
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.
|
void |
notifyAsUser(String tag,
int id,
Notification notification,
UserHandle user) |
boolean |
removeAutomaticZenRule(String id)
Deletes the automatic zen rule with the given id.
|
boolean |
removeAutomaticZenRules(String packageName)
Deletes all automatic zen rules owned by the given package.
|
void |
setInterruptionFilter(int interruptionFilter)
Sets the current notification interruption filter.
|
void |
setNotificationPolicy(NotificationManager.Policy policy)
Sets the current notification policy.
|
void |
setNotificationPolicyAccessGranted(String pkg,
boolean granted) |
void |
setZenMode(int mode,
Uri conditionId,
String reason) |
boolean |
updateAutomaticZenRule(String id,
AutomaticZenRule automaticZenRule)
Updates the given zen rule.
|
static int |
zenModeFromInterruptionFilter(int interruptionFilter,
int defValue) |
static int |
zenModeToInterruptionFilter(int zen) |
public static final String ACTION_EFFECTS_SUPPRESSOR_CHANGED
getEffectsSuppressor()
changes.
This broadcast is only sent to registered receivers.public static final String ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED
isNotificationPolicyAccessGranted()
changes.
This broadcast is only sent to registered receivers, and only to the apps that have changed.public static final String ACTION_NOTIFICATION_POLICY_CHANGED
public static final String ACTION_INTERRUPTION_FILTER_CHANGED
public static final String ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL
public static final int INTERRUPTION_FILTER_ALL
Interruption filter
constant -
Normal interruption filter - no notifications are suppressed.public static final 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
NotificationManager.Policy.priorityCallSenders
, NotificationManager.Policy.priorityCategories
,
NotificationManager.Policy.priorityMessageSenders
to define or query this criteria. Users can
additionally specify packages that can bypass this interruption filter.public static final 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.public static final int INTERRUPTION_FILTER_ALARMS
Interruption filter
constant -
Alarms only interruption filter - all notifications except those of category
Notification.CATEGORY_ALARM
are suppressed. Some audio streams are muted.public static final int INTERRUPTION_FILTER_UNKNOWN
Interruption filter
constant - returned when
the value is unavailable for any reason.public static final int VISIBILITY_NO_OVERRIDE
public static final int IMPORTANCE_UNSPECIFIED
public static final int IMPORTANCE_NONE
public static final int IMPORTANCE_MIN
public static final int IMPORTANCE_LOW
public static final int IMPORTANCE_DEFAULT
public static final int IMPORTANCE_HIGH
public static final int IMPORTANCE_MAX
public static INotificationManager getService()
public static NotificationManager from(Context context)
public void notify(int id, Notification notification)
id
- An identifier for this notification unique within your
application.notification
- A Notification
object describing what to show the user. Must not
be null.public void notify(String tag, int id, Notification notification)
tag
- A string identifier for this notification. May be null
.id
- An identifier for this notification. The pair (tag, id) must be unique
within your application.notification
- A Notification
object describing what to
show the user. Must not be null.public void notifyAsUser(String tag, int id, Notification notification, UserHandle user)
public void cancel(int id)
public void cancel(String tag, int id)
public void cancelAsUser(String tag, int id, UserHandle user)
public void cancelAll()
cancel(int)
for the
detailed behavior.public ComponentName getEffectsSuppressor()
public boolean matchesCallFilter(Bundle extras)
public boolean isSystemConditionProviderEnabled(String path)
public int getZenMode()
public ZenModeConfig getZenModeConfig()
public int getRuleInstanceCount(ComponentName owner)
public Map<String,AutomaticZenRule> getAutomaticZenRules()
Throws a SecurityException if policy access is granted to this package.
See isNotificationPolicyAccessGranted()
.
public AutomaticZenRule getAutomaticZenRule(String id)
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 AutomaticZenRule.getOwner()
.
public String addAutomaticZenRule(AutomaticZenRule automaticZenRule)
Throws a SecurityException if policy access is granted to this package.
See isNotificationPolicyAccessGranted()
.
automaticZenRule
- the rule to create.public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule)
Throws a SecurityException if policy access is granted to this package.
See isNotificationPolicyAccessGranted()
.
Callers can only update rules that they own. See AutomaticZenRule.getOwner()
.
id
- The id of the rule to updateautomaticZenRule
- the rule to update.public boolean removeAutomaticZenRule(String id)
Throws a SecurityException if policy access is granted to this package.
See isNotificationPolicyAccessGranted()
.
Callers can only delete rules that they own. See AutomaticZenRule.getOwner()
.
id
- the id of the rule to delete.public boolean removeAutomaticZenRules(String packageName)
public int getImportance()
IMPORTANCE_DEFAULT
.public boolean areNotificationsEnabled()
public boolean isNotificationPolicyAccessGranted()
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 Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS
.
Use ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED
to listen for
user grant or denial of this access.
public boolean isNotificationPolicyAccessGrantedForPackage(String pkg)
public NotificationManager.Policy getNotificationPolicy()
Only available if policy access is granted to this package.
See isNotificationPolicyAccessGranted()
.
public void setNotificationPolicy(NotificationManager.Policy policy)
Only available if policy access is granted to this package.
See isNotificationPolicyAccessGranted()
.
policy
- The new desired policy.public void setNotificationPolicyAccessGranted(String pkg, boolean granted)
public ArraySet<String> getPackagesRequestingNotificationPolicyAccess()
public StatusBarNotification[] getActiveNotifications()
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 StatusBarNotification.getNotification()
).StatusBarNotification
.public final int getCurrentInterruptionFilter()
The interruption filter defines which notifications are allowed to interrupt the user (e.g. via sound & vibration) and is applied globally.
public final void setInterruptionFilter(int interruptionFilter)
The interruption filter defines which notifications are allowed to interrupt the user (e.g. via sound & vibration) and is applied globally.
public static int zenModeToInterruptionFilter(int zen)
public static int zenModeFromInterruptionFilter(int interruptionFilter, int defValue)