public static class NotificationCompat.Builder extends Object
NotificationCompat
objects. Allows easier control over
all the flags, as well as help constructing the typical notification layouts.
On platform versions that don't offer expanded notifications, methods that depend on expanded notifications have no effect.
For example, action buttons won't appear on platforms prior to Android 4.1. Action buttons depend on expanded notifications, which are only available in Android 4.1 and later.
For this reason, you should always ensure that UI controls in a notification are also
available in an Activity
in your app, and you should always start that
Activity
when users click the notification. To do this, use the
setContentIntent()
method.
Modifier and Type | Field and Description |
---|---|
ArrayList<NotificationCompat.Action> |
mActions |
CharSequence |
mContentInfo |
CharSequence |
mContentText |
CharSequence |
mContentTitle |
Context |
mContext |
Bitmap |
mLargeIcon |
Notification |
mNotification |
int |
mNumber |
ArrayList<String> |
mPeople |
CharSequence[] |
mRemoteInputHistory |
NotificationCompat.Style |
mStyle |
CharSequence |
mSubText |
boolean |
mUseChronometer |
Modifier and Type | Method and Description |
---|---|
NotificationCompat.Builder |
addAction(int icon,
CharSequence title,
PendingIntent intent)
Add an action to this notification.
|
NotificationCompat.Builder |
addAction(NotificationCompat.Action action)
Add an action to this notification.
|
NotificationCompat.Builder |
addExtras(Bundle extras)
Merge additional metadata into this notification.
|
NotificationCompat.Builder |
addPerson(String uri)
Add a person that is relevant to this notification.
|
Notification |
build()
Combine all of the options that have been set and return a new
Notification
object. |
NotificationCompat.Builder |
extend(NotificationCompat.Extender extender)
Apply an extender to this notification builder.
|
RemoteViews |
getBigContentView() |
int |
getColor() |
RemoteViews |
getContentView() |
protected NotificationCompat.BuilderExtender |
getExtender() |
Bundle |
getExtras()
Get the current metadata Bundle used by this notification Builder.
|
RemoteViews |
getHeadsUpContentView() |
Notification |
getNotification()
Deprecated.
Use
build() instead. |
int |
getPriority() |
long |
getWhenIfShowing()
return when if it is showing or 0 otherwise
|
protected static CharSequence |
limitCharSequenceLength(CharSequence cs) |
protected CharSequence |
resolveText() |
protected CharSequence |
resolveTitle() |
NotificationCompat.Builder |
setAutoCancel(boolean autoCancel)
Setting this flag will make it so the notification is automatically
canceled when the user clicks it in the panel.
|
NotificationCompat.Builder |
setCategory(String category)
Set the notification category.
|
NotificationCompat.Builder |
setColor(int argb)
Sets
Notification.color . |
NotificationCompat.Builder |
setContent(RemoteViews views)
Supply a custom RemoteViews to use instead of the standard one.
|
NotificationCompat.Builder |
setContentInfo(CharSequence info)
Set the large text at the right-hand side of the notification.
|
NotificationCompat.Builder |
setContentIntent(PendingIntent intent)
Supply a
PendingIntent to send when the notification is clicked. |
NotificationCompat.Builder |
setContentText(CharSequence text)
Set the text (second row) of the notification, in a standard notification.
|
NotificationCompat.Builder |
setContentTitle(CharSequence title)
Set the title (first row) of the notification, in a standard notification.
|
NotificationCompat.Builder |
setCustomBigContentView(RemoteViews contentView)
Supply custom RemoteViews to use instead of the platform template in the expanded form.
|
NotificationCompat.Builder |
setCustomContentView(RemoteViews contentView)
Supply custom RemoteViews to use instead of the platform template.
|
NotificationCompat.Builder |
setCustomHeadsUpContentView(RemoteViews contentView)
Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
|
NotificationCompat.Builder |
setDefaults(int defaults)
Set the default notification options that will be used.
|
NotificationCompat.Builder |
setDeleteIntent(PendingIntent intent)
Supply a
PendingIntent to send when the notification is cleared by the user
directly from the notification panel. |
NotificationCompat.Builder |
setExtras(Bundle extras)
Set metadata for this notification.
|
NotificationCompat.Builder |
setFullScreenIntent(PendingIntent intent,
boolean highPriority)
An intent to launch instead of posting the notification to the status bar.
|
NotificationCompat.Builder |
setGroup(String groupKey)
Set this notification to be part of a group of notifications sharing the same key.
|
NotificationCompat.Builder |
setGroupSummary(boolean isGroupSummary)
Set this notification to be the group summary for a group of notifications.
|
NotificationCompat.Builder |
setLargeIcon(Bitmap icon)
Set the large icon that is shown in the ticker and notification.
|
NotificationCompat.Builder |
setLights(int argb,
int onMs,
int offMs)
Set the argb value that you would like the LED on the device to blink, as well as the
rate.
|
NotificationCompat.Builder |
setLocalOnly(boolean b)
Set whether or not this notification is only relevant to the current device.
|
NotificationCompat.Builder |
setNumber(int number)
Set the large number at the right-hand side of the notification.
|
NotificationCompat.Builder |
setOngoing(boolean ongoing)
Set whether this is an ongoing notification.
|
NotificationCompat.Builder |
setOnlyAlertOnce(boolean onlyAlertOnce)
Set this flag if you would only like the sound, vibrate
and ticker to be played if the notification is not already showing.
|
NotificationCompat.Builder |
setPriority(int pri)
Set the relative priority for this notification.
|
NotificationCompat.Builder |
setProgress(int max,
int progress,
boolean indeterminate)
Set the progress this notification represents, which may be
represented as a
ProgressBar . |
NotificationCompat.Builder |
setPublicVersion(Notification n)
Supply a replacement Notification whose contents should be shown in insecure contexts
(i.e. atop the secure lockscreen).
|
NotificationCompat.Builder |
setRemoteInputHistory(CharSequence[] text)
Set the remote input history.
|
NotificationCompat.Builder |
setShowWhen(boolean show)
Control whether the timestamp set with
setWhen is shown
in the content view. |
NotificationCompat.Builder |
setSmallIcon(int icon)
Set the small icon to use in the notification layouts.
|
NotificationCompat.Builder |
setSmallIcon(int icon,
int level)
A variant of
setSmallIcon(int) that takes an additional
level parameter for when the icon is a LevelListDrawable . |
NotificationCompat.Builder |
setSortKey(String sortKey)
Set a sort key that orders this notification among other notifications from the
same package.
|
NotificationCompat.Builder |
setSound(Uri sound)
Set the sound to play.
|
NotificationCompat.Builder |
setSound(Uri sound,
int streamType)
Set the sound to play.
|
NotificationCompat.Builder |
setStyle(NotificationCompat.Style style)
Add a rich notification style to be applied at build time.
|
NotificationCompat.Builder |
setSubText(CharSequence text)
Set the third line of text in the platform notification template.
|
NotificationCompat.Builder |
setTicker(CharSequence tickerText)
Set the text that is displayed in the status bar when the notification first
arrives.
|
NotificationCompat.Builder |
setTicker(CharSequence tickerText,
RemoteViews views)
Set the text that is displayed in the status bar when the notification first
arrives, and also a RemoteViews object that may be displayed instead on some
devices.
|
NotificationCompat.Builder |
setUsesChronometer(boolean b)
Show the
Notification.when field as a stopwatch. |
NotificationCompat.Builder |
setVibrate(long[] pattern)
Set the vibration pattern to use.
|
NotificationCompat.Builder |
setVisibility(int visibility)
Sets
Notification.visibility . |
NotificationCompat.Builder |
setWhen(long when)
Set the time that the event occurred.
|
public Context mContext
public CharSequence mContentTitle
public CharSequence mContentText
public Bitmap mLargeIcon
public CharSequence mContentInfo
public int mNumber
public boolean mUseChronometer
public NotificationCompat.Style mStyle
public CharSequence mSubText
public CharSequence[] mRemoteInputHistory
public ArrayList<NotificationCompat.Action> mActions
public Notification mNotification
public Builder(Context context)
System.currentTimeMillis()
and the audio stream to the
Notification.STREAM_DEFAULT
.context
- A Context
that will be used to construct the
RemoteViews. The Context will not be held past the lifetime of this
Builder object.public NotificationCompat.Builder setWhen(long when)
public NotificationCompat.Builder setShowWhen(boolean show)
setWhen
is shown
in the content view.public NotificationCompat.Builder setUsesChronometer(boolean b)
Notification.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).Chronometer
,
Notification.when
public NotificationCompat.Builder setSmallIcon(int icon)
icon
- A resource ID in the application's package of the drawble to use.public NotificationCompat.Builder setSmallIcon(int icon, int level)
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 drawble to use.level
- The level to use for the icon.LevelListDrawable
public NotificationCompat.Builder setContentTitle(CharSequence title)
public NotificationCompat.Builder setContentText(CharSequence text)
public NotificationCompat.Builder setSubText(CharSequence text)
setProgress(int, int, boolean)
;
they occupy the same location in the standard template.
public NotificationCompat.Builder setRemoteInputHistory(CharSequence[] text)
RemoteInput
of this Notification and cleared once the it is no
longer relevant (e.g. for chat notifications once the other party has responded).
The most recent input must be stored at the 0 index, the second most recent at the
1 index, etc. Note that the system will limit both how far back the inputs will be shown
and how much of each individual input is shown.
Note: The reply text will only be shown on notifications that have least one action
with a RemoteInput
.
public NotificationCompat.Builder setNumber(int number)
public NotificationCompat.Builder setContentInfo(CharSequence info)
public NotificationCompat.Builder setProgress(int max, int progress, boolean indeterminate)
ProgressBar
.public NotificationCompat.Builder setContent(RemoteViews views)
public NotificationCompat.Builder setContentIntent(PendingIntent intent)
PendingIntent
to send when the notification is clicked.
If you do not supply an intent, you can now add PendingIntents to individual
views to be launched when clicked by calling RemoteViews.setOnClickPendingIntent(int,PendingIntent)
. Be sure to
read Notification.contentIntent
for
how to correctly use this.public NotificationCompat.Builder setDeleteIntent(PendingIntent intent)
PendingIntent
to send when the notification is cleared by the user
directly from the notification panel. For example, this intent is sent when the user
clicks the "Clear all" button, or the individual "X" buttons on notifications. This
intent is not sent when the application calls
NotificationManager.cancel(int)
.public NotificationCompat.Builder setFullScreenIntent(PendingIntent intent, boolean highPriority)
On some platforms, 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.public NotificationCompat.Builder setTicker(CharSequence tickerText)
public NotificationCompat.Builder setTicker(CharSequence tickerText, RemoteViews views)
public NotificationCompat.Builder setLargeIcon(Bitmap icon)
public NotificationCompat.Builder setSound(Uri sound)
On some platforms, a notification that is noisy is more likely to be presented as a heads-up notification.
public NotificationCompat.Builder setSound(Uri sound, int streamType)
On some platforms, a notification that is noisy is more likely to be presented as a heads-up notification.
public NotificationCompat.Builder setVibrate(long[] pattern)
On some platforms, a notification that vibrates is more likely to be presented as a heads-up notification.
public NotificationCompat.Builder setLights(int argb, int onMs, int offMs)
public NotificationCompat.Builder setOngoing(boolean ongoing)
Ongoing notifications differ from regular notifications in the following ways:
public NotificationCompat.Builder setOnlyAlertOnce(boolean onlyAlertOnce)
public NotificationCompat.Builder setAutoCancel(boolean autoCancel)
setDeleteIntent(android.app.PendingIntent)
will be broadcast when the notification
is canceled.public NotificationCompat.Builder setLocalOnly(boolean b)
Some notifications can be bridged to other devices for remote display. This hint can be set to recommend this notification not be bridged.
public NotificationCompat.Builder setCategory(String category)
Must be one of the predefined notification categories (see the CATEGORY_*
constants in Notification
) that best describes this notification.
May be used by the system for ranking and filtering.
public NotificationCompat.Builder setDefaults(int defaults)
The value should be one or more of the following fields combined with
bitwise-or:
Notification.DEFAULT_SOUND
, Notification.DEFAULT_VIBRATE
,
Notification.DEFAULT_LIGHTS
.
For all default values, use Notification.DEFAULT_ALL
.
public NotificationCompat.Builder setPriority(int pri)
pri
- Relative priority for this notification. Must be one of
the priority constants defined by NotificationCompat
.
Acceptable values range from NotificationCompat.PRIORITY_MIN
(-2) to NotificationCompat.PRIORITY_MAX
(2).public NotificationCompat.Builder addPerson(String uri)
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
ContactsContract.Contacts.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.Notification.EXTRA_PEOPLE
public NotificationCompat.Builder setGroup(String groupKey)
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(java.lang.String)
.
groupKey
- The group key of the group.public NotificationCompat.Builder setGroupSummary(boolean isGroupSummary)
setGroup(java.lang.String)
.isGroupSummary
- Whether this notification should be a group summary.public NotificationCompat.Builder setSortKey(String sortKey)
This sort key can also be used to order members of a notification group. See
setGroup(java.lang.String)
.
String.compareTo(String)
public NotificationCompat.Builder addExtras(Bundle extras)
Values within the Bundle will replace existing extras values in this Builder.
Notification.extras
public NotificationCompat.Builder setExtras(Bundle extras)
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(android.os.Bundle)
to merge in metadata instead.
Notification.extras
public Bundle getExtras()
The returned Bundle is shared with this Builder.
The current contents of this Bundle are copied into the Notification each time
build()
is called.
Notification.extras
public NotificationCompat.Builder addAction(int icon, CharSequence title, PendingIntent intent)
Activity
that starts when a user
clicks the notification (see setContentIntent()
), and then
enhance the notification by implementing the same functionality with
addAction()
.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.public NotificationCompat.Builder addAction(NotificationCompat.Action action)
Activity
that starts when a user
clicks the notification (see setContentIntent()
), and then
enhance the notification by implementing the same functionality with
addAction()
.action
- The action to add.public NotificationCompat.Builder setStyle(NotificationCompat.Style style)
style
- Object responsible for modifying the notification style.public NotificationCompat.Builder setColor(int argb)
Notification.color
.argb
- The accent color to usepublic NotificationCompat.Builder setVisibility(int visibility)
Notification.visibility
.visibility
- One of Notification.VISIBILITY_PRIVATE
(the default),
Notification.VISIBILITY_PUBLIC
, or
Notification.VISIBILITY_SECRET
.public NotificationCompat.Builder setPublicVersion(Notification n)
Notification.visibility
and
NotificationCompat.VISIBILITY_PUBLIC
.n
- A replacement notification, presumably with some or all info redacted.public NotificationCompat.Builder setCustomContentView(RemoteViews contentView)
public NotificationCompat.Builder setCustomBigContentView(RemoteViews contentView)
Build.VERSION_CODES.JELLY_BEAN
.public NotificationCompat.Builder setCustomHeadsUpContentView(RemoteViews contentView)
Build.VERSION_CODES.LOLLIPOP
.public NotificationCompat.Builder extend(NotificationCompat.Extender extender)
@Deprecated public Notification getNotification()
build()
instead.public Notification build()
Notification
object.protected NotificationCompat.BuilderExtender getExtender()
protected static CharSequence limitCharSequenceLength(CharSequence cs)
public RemoteViews getContentView()
public RemoteViews getBigContentView()
public RemoteViews getHeadsUpContentView()
public long getWhenIfShowing()
public int getPriority()
public int getColor()
protected CharSequence resolveText()
protected CharSequence resolveTitle()