java.lang.Object | |
↳ | android.app.Notification.Builder |
Builder class for Notification
objects.
Provides a convenient way to set the various fields of a Notification
and generate
content views using the platform's notification layout template. If your app supports
versions of Android as old as API level 4, you can instead use
NotificationCompat.Builder
,
available in the Android Support
library.
Example:
Notification noti = new Notification.Builder(mContext) .setContentTitle("New mail from " + sender.toString()) .setContentText(subject) .setSmallIcon(R.drawable.new_mail) .setLargeIcon(aBitmap) .build();
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new Builder with the defaults:
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Add an action to this notification.
| |||||||||||
Add an action to this notification.
| |||||||||||
Merge additional metadata into this notification.
| |||||||||||
Add a person that is relevant to this notification.
| |||||||||||
Combine all of the options that have been set and return a new
Notification
object.
| |||||||||||
Apply an extender to this notification builder.
| |||||||||||
Get the current metadata Bundle used by this notification Builder.
| |||||||||||
This method was deprecated
in API level 16.
Use
build() instead.
| |||||||||||
Make this notification automatically dismissed when the user touches it.
| |||||||||||
Set the notification category.
| |||||||||||
Sets
color .
| |||||||||||
Supply a custom RemoteViews to use instead of the platform template.
| |||||||||||
A small piece of additional information pertaining to this notification.
| |||||||||||
Supply a
PendingIntent to be sent when the notification is clicked.
| |||||||||||
Set the second line of text in the platform notification template.
| |||||||||||
Set the first line of text in the platform notification template.
| |||||||||||
Set which notification properties will be inherited from system defaults.
| |||||||||||
Supply a
PendingIntent to send when the notification is cleared explicitly by the user.
| |||||||||||
Set metadata for this notification.
| |||||||||||
An intent to launch instead of posting the notification to the status bar.
| |||||||||||
Set this notification to be part of a group of notifications sharing the same key.
| |||||||||||
Set this notification to be the group summary for a group of notifications.
| |||||||||||
Add a large icon to the notification (and the ticker on some devices).
| |||||||||||
Set the desired color for the indicator LED on the device, as well as the
blink duty cycle (specified in milliseconds).
| |||||||||||
Set whether or not this notification should not bridge to other devices.
| |||||||||||
Set the large number at the right-hand side of the notification.
| |||||||||||
Set whether this is an "ongoing" notification.
| |||||||||||
Set this flag if you would only like the sound, vibrate
and ticker to be played if the notification is not already showing.
| |||||||||||
Set the priority of this notification.
| |||||||||||
Set the progress this notification represents.
| |||||||||||
Supply a replacement Notification whose contents should be shown in insecure contexts
(i.e.
| |||||||||||
Control whether the timestamp set with
setWhen is shown
in the content view.
| |||||||||||
A variant of
setSmallIcon(int) that takes an additional
level parameter for when the icon is a LevelListDrawable .
| |||||||||||
Set the small icon resource, which will be used to represent the notification in the
status bar.
| |||||||||||
Set a sort key that orders this notification among other notifications from the
same package.
| |||||||||||
Set the sound to play, along with specific
audio attributes to
use during playback.
| |||||||||||
Set the sound to play.
| |||||||||||
This method was deprecated
in API level 21.
use
setSound(Uri, AudioAttributes) instead.
| |||||||||||
Add a rich notification style to be applied at build time.
| |||||||||||
Set the third line of text in the platform notification template.
| |||||||||||
Obsolete version of
setTicker(CharSequence) .
| |||||||||||
Set the "ticker" text which is sent to accessibility services.
| |||||||||||
Show the
when field as a stopwatch.
| |||||||||||
Set the vibration pattern to use.
| |||||||||||
Specify the value of
visibility .
| |||||||||||
Add a timestamp pertaining to the notification (usually the time the event occurred).
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Constructs a new Builder with the defaults:
priority | PRIORITY_DEFAULT |
---|---|
when | now (currentTimeMillis() ) |
audio stream | STREAM_DEFAULT |
context | A Context that will be used by the Builder to construct the
RemoteViews. The Context will not be held past the lifetime of this Builder
object.
|
---|
Add an action to this notification. Actions are typically displayed by the system as a button adjacent to the notification content.
Every action must have an icon (32dp square and matching the
Holo
Dark action bar visual style), a textual label, and a PendingIntent
.
A notification in its expanded form can display up to 3 actions, from left to right in
the order they were added. Actions will not be displayed when the notification is
collapsed, however, so be sure that any essential functions may be accessed by the user
in some other way (for example, in the Activity pointed to by contentIntent
).
action | The action to add. |
---|
Add an action to this notification. Actions are typically displayed by the system as a button adjacent to the notification content.
Every action must have an icon (32dp square and matching the
Holo
Dark action bar visual style), a textual label, and a PendingIntent
.
A notification in its expanded form can display up to 3 actions, from left to right in
the order they were added. Actions will not be displayed when the notification is
collapsed, however, so be sure that any essential functions may be accessed by the user
in some other way (for example, in the Activity pointed to by contentIntent
).
icon | Resource ID of a drawable that represents the action. |
---|---|
title | Text describing the action. |
intent | PendingIntent to be fired when the action is invoked. |
Merge additional metadata into this notification.
Values within the Bundle will replace existing extras values in this Builder.
Add a person that is relevant to this notification.
Depending on user preferences, this annotation may allow the notification to pass through interruption filters, and to appear more prominently in the user interface.
The person should be specified by the String
representation of a
CONTENT_LOOKUP_URI
.
The system will also attempt to resolve mailto:
and tel:
schema
URIs. The path part of these URIs must exist in the contacts database, in the
appropriate column, or the reference will be discarded as invalid. Telephone schema
URIs will be resolved by ContactsContract.PhoneLookup
.
uri | A URI for the person. |
---|
Combine all of the options that have been set and return a new Notification
object.
Apply an extender to this notification builder. Extenders may be used to add metadata or change options on this builder.
Make this notification automatically dismissed when the user touches it. The
PendingIntent set with setDeleteIntent(PendingIntent)
will be sent when this happens.
Set the notification category.
Supply a custom RemoteViews to use instead of the platform template.
A small piece of additional information pertaining to this notification. The platform template will draw this on the last line of the notification, at the far right (to the right of a smallIcon if it has been placed there).
Supply a PendingIntent
to be sent when the notification is clicked.
As of HONEYCOMB
, if this field is unset and you
have specified a custom RemoteViews with setContent(RemoteViews)
, you can use
RemoteViews.setOnClickPendingIntent(int,PendingIntent)
to assign PendingIntents to individual views in that custom layout (i.e., to create
clickable buttons inside the notification view).
Set the second line of text in the platform notification template.
Set the first line of text in the platform notification template.
Set which notification properties will be inherited from system defaults.
The value should be one or more of the following fields combined with
bitwise-or:
DEFAULT_SOUND
, DEFAULT_VIBRATE
, DEFAULT_LIGHTS
.
For all default values, use DEFAULT_ALL
.
Supply a PendingIntent
to send when the notification is cleared explicitly by the user.
Set metadata for this notification.
A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
current contents are copied into the Notification each time build()
is
called.
Replaces any existing extras values with those from the provided Bundle.
Use addExtras(Bundle)
to merge in metadata instead.
An intent to launch instead of posting the notification to the status bar. Only for use with extremely high-priority notifications demanding the user's immediate attention, such as an incoming phone call or alarm clock that the user has explicitly set to a particular time. If this facility is used for something else, please give the user an option to turn it off and use a normal notification, as this can be extremely disruptive.
The system UI may choose to display a heads-up notification, instead of launching this intent, while the user is using the device.
intent | The pending intent to launch. |
---|---|
highPriority | Passing true will cause this notification to be sent even if other notifications are suppressed. |
Set this notification to be part of a group of notifications sharing the same key. Grouped notifications may display in a cluster or stack on devices which support such rendering.
To make this notification the summary for its group, also call
setGroupSummary(boolean)
. A sort order can be specified for group members by using
setSortKey(String)
.
groupKey | The group key of the group. |
---|
Set this notification to be the group summary for a group of notifications.
Grouped notifications may display in a cluster or stack on devices which
support such rendering. Requires a group key also be set using setGroup(String)
.
isGroupSummary | Whether this notification should be a group summary. |
---|
Add a large icon to the notification (and the ticker on some devices).
In the platform template, this image will be shown on the left of the notification view
in place of the small icon
(which will move to the right side).
Set whether or not this notification should not bridge to other devices.
Some notifications can be bridged to other devices for remote display. This hint can be set to recommend this notification not be bridged.
Set the large number at the right-hand side of the notification. This is equivalent to setContentInfo, although it might show the number in a different font size for readability.
Set whether this is an "ongoing" notification. Ongoing notifications cannot be dismissed by the user, so your application or service must take care of canceling them. They are typically used to indicate a background task that the user is actively engaged with (e.g., playing music) or is pending in some way and therefore occupying the device (e.g., a file download, sync operation, active network connection).
Set this flag if you would only like the sound, vibrate and ticker to be played if the notification is not already showing.
Set the priority of this notification.
Set the progress this notification represents.
The platform template will represent this using a ProgressBar
.
Supply a replacement Notification whose contents should be shown in insecure contexts
(i.e. atop the secure lockscreen). See visibility
and VISIBILITY_PUBLIC
.
n | A replacement notification, presumably with some or all info redacted. |
---|
Control whether the timestamp set with setWhen
is shown
in the content view.
A variant of setSmallIcon(int)
that takes an additional
level parameter for when the icon is a LevelListDrawable
.
icon | A resource ID in the application's package of the drawable to use. |
---|---|
level | The level to use for the icon. |
Set the small icon resource, which will be used to represent the notification in the
status bar.
The platform template for the expanded view will draw this icon in the left, unless a
large icon
has also been specified, in which case the small
icon will be moved to the right-hand side.
icon | A resource ID in the application's package of the drawable to use. |
---|
Set a sort key that orders this notification among other notifications from the same package. This can be useful if an external sort was already applied and an app would like to preserve this. Notifications will be sorted lexicographically using this value, although providing different priorities in addition to providing sort key may cause this value to be ignored.
This sort key can also be used to order members of a notification group. See
setGroup(String)
.
Set the sound to play, along with specific audio attributes
to
use during playback.
A notification that is noisy is more likely to be presented as a heads-up notification.
Set the sound to play.
It will be played using the default audio attributes
for notifications.
A notification that is noisy is more likely to be presented as a heads-up notification.
This method was deprecated
in API level 21.
use setSound(Uri, AudioAttributes)
instead.
Set the sound to play, along with a specific stream on which to play it.
See AudioManager
for the STREAM_
constants.
A notification that is noisy is more likely to be presented as a heads-up notification.
Add a rich notification style to be applied at build time.
style | Object responsible for modifying the notification style. |
---|
Set the third line of text in the platform notification template.
Don't use if you're also using setProgress(int, int, boolean)
; they occupy the
same location in the standard template.
Obsolete version of setTicker(CharSequence)
.
Set the "ticker" text which is sent to accessibility services.
Show the when
field as a stopwatch.
Instead of presenting when
as a timestamp, the notification will show an
automatically updating display of the minutes and seconds since when
.
Useful when showing an elapsed time (like an ongoing phone call).
Set the vibration pattern to use.
See vibrate(long[], int)
for a discussion of the
pattern
parameter.
A notification that vibrates is more likely to be presented as a heads-up notification.
Specify the value of visibility
.
visibility | One of VISIBILITY_PRIVATE (the default),
VISIBILITY_SECRET , or VISIBILITY_PUBLIC . |
---|
Add a timestamp pertaining to the notification (usually the time the event occurred).
It will be shown in the notification content view by default; use
setShowWhen
to control this.