Android APIs
public static class

Notification.Builder

extends Object
java.lang.Object
   ↳ android.app.Notification.Builder

Class Overview

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();
 

Summary

Public Constructors
Notification.Builder(Context context)
Constructs a new Builder with the defaults:
priority PRIORITY_DEFAULT
when now (currentTimeMillis())
audio stream STREAM_DEFAULT
Public Methods
Notification.Builder addAction(Notification.Action action)
Add an action to this notification.
Notification.Builder addAction(int icon, CharSequence title, PendingIntent intent)
This method was deprecated in API level 23. Use addAction(Action) instead.
Notification.Builder addExtras(Bundle extras)
Merge additional metadata into this notification.
Notification.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.
Notification.Builder extend(Notification.Extender extender)
Apply an extender to this notification builder.
Bundle getExtras()
Get the current metadata Bundle used by this notification Builder.
Notification getNotification()
This method was deprecated in API level 16. Use build() instead.
Notification.Builder setAutoCancel(boolean autoCancel)
Make this notification automatically dismissed when the user touches it.
Notification.Builder setCategory(String category)
Set the notification category.
Notification.Builder setColor(int argb)
Sets color.
Notification.Builder setContent(RemoteViews views)
Supply a custom RemoteViews to use instead of the platform template.
Notification.Builder setContentInfo(CharSequence info)
A small piece of additional information pertaining to this notification.
Notification.Builder setContentIntent(PendingIntent intent)
Supply a PendingIntent to be sent when the notification is clicked.
Notification.Builder setContentText(CharSequence text)
Set the second line of text in the platform notification template.
Notification.Builder setContentTitle(CharSequence title)
Set the first line of text in the platform notification template.
Notification.Builder setDefaults(int defaults)
Set which notification properties will be inherited from system defaults.
Notification.Builder setDeleteIntent(PendingIntent intent)
Supply a PendingIntent to send when the notification is cleared explicitly by the user.
Notification.Builder setExtras(Bundle extras)
Set metadata for this notification.
Notification.Builder setFullScreenIntent(PendingIntent intent, boolean highPriority)
An intent to launch instead of posting the notification to the status bar.
Notification.Builder setGroup(String groupKey)
Set this notification to be part of a group of notifications sharing the same key.
Notification.Builder setGroupSummary(boolean isGroupSummary)
Set this notification to be the group summary for a group of notifications.
Notification.Builder setLargeIcon(Icon icon)
Add a large icon to the notification content view.
Notification.Builder setLargeIcon(Bitmap b)
Add a large icon to the notification content view.
Notification.Builder setLights(int argb, int onMs, int offMs)
Set the desired color for the indicator LED on the device, as well as the blink duty cycle (specified in milliseconds).
Notification.Builder setLocalOnly(boolean localOnly)
Set whether or not this notification should not bridge to other devices.
Notification.Builder setNumber(int number)
Set the large number at the right-hand side of the notification.
Notification.Builder setOngoing(boolean ongoing)
Set whether this is an "ongoing" notification.
Notification.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.
Notification.Builder setPriority(int pri)
Set the priority of this notification.
Notification.Builder setProgress(int max, int progress, boolean indeterminate)
Set the progress this notification represents.
Notification.Builder setPublicVersion(Notification n)
Supply a replacement Notification whose contents should be shown in insecure contexts (i.e.
Notification.Builder setShowWhen(boolean show)
Control whether the timestamp set with setWhen is shown in the content view.
Notification.Builder setSmallIcon(int icon, int level)
A variant of setSmallIcon(int) that takes an additional level parameter for when the icon is a LevelListDrawable.
Notification.Builder setSmallIcon(int icon)
Set the small icon resource, which will be used to represent the notification in the status bar.
Notification.Builder setSmallIcon(Icon icon)
Set the small icon, which will be used to represent the notification in the status bar and content view (unless overriden there by a large icon).
Notification.Builder setSortKey(String sortKey)
Set a sort key that orders this notification among other notifications from the same package.
Notification.Builder setSound(Uri sound, AudioAttributes audioAttributes)
Set the sound to play, along with specific audio attributes to use during playback.
Notification.Builder setSound(Uri sound)
Set the sound to play.
Notification.Builder setSound(Uri sound, int streamType)
This method was deprecated in API level 21. use setSound(Uri, AudioAttributes) instead.
Notification.Builder setStyle(Notification.Style style)
Add a rich notification style to be applied at build time.
Notification.Builder setSubText(CharSequence text)
Set the third line of text in the platform notification template.
Notification.Builder setTicker(CharSequence tickerText, RemoteViews views)
Obsolete version of setTicker(CharSequence).
Notification.Builder setTicker(CharSequence tickerText)
Set the "ticker" text which is sent to accessibility services.
Notification.Builder setUsesChronometer(boolean b)
Show the when field as a stopwatch.
Notification.Builder setVibrate(long[] pattern)
Set the vibration pattern to use.
Notification.Builder setVisibility(int visibility)
Specify the value of visibility.
Notification.Builder setWhen(long when)
Add a timestamp pertaining to the notification (usually the time the event occurred).
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public Notification.Builder (Context context)

Added in API level 11

Constructs a new Builder with the defaults:

priority PRIORITY_DEFAULT
when now (currentTimeMillis())
audio stream STREAM_DEFAULT

Parameters
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.

Public Methods

public Notification.Builder addAction (Notification.Action action)

Added in API level 20

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).

Parameters
action The action to add.

public Notification.Builder addAction (int icon, CharSequence title, PendingIntent intent)

Added in API level 16

This method was deprecated in API level 23.
Use addAction(Action) instead.

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).

Parameters
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 Notification.Builder addExtras (Bundle extras)

Added in API level 20

Merge additional metadata into this notification.

Values within the Bundle will replace existing extras values in this Builder.

See Also

public Notification.Builder addPerson (String uri)

Added in API level 21

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.

Parameters
uri A URI for the person.
See Also

public Notification build ()

Added in API level 16

Combine all of the options that have been set and return a new Notification object.

public Notification.Builder extend (Notification.Extender extender)

Added in API level 20

Apply an extender to this notification builder. Extenders may be used to add metadata or change options on this builder.

public Bundle getExtras ()

Added in API level 20

Get the current metadata Bundle used by this notification Builder.

The returned Bundle is shared with this Builder.

The current contents of this Bundle are copied into the Notification each time build() is called.

See Also

public Notification getNotification ()

Added in API level 11

This method was deprecated in API level 16.
Use build() instead.

public Notification.Builder setAutoCancel (boolean autoCancel)

Added in API level 11

Make this notification automatically dismissed when the user touches it. The PendingIntent set with setDeleteIntent(PendingIntent) will be sent when this happens.

See Also

public Notification.Builder setCategory (String category)

Added in API level 21

Set the notification category.

See Also

public Notification.Builder setColor (int argb)

Added in API level 21

Sets color.

Parameters
argb The accent color to use
Returns
  • The same Builder.

public Notification.Builder setContent (RemoteViews views)

Added in API level 11

Supply a custom RemoteViews to use instead of the platform template.

See Also

public Notification.Builder setContentInfo (CharSequence info)

Added in API level 11

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).

public Notification.Builder setContentIntent (PendingIntent intent)

Added in API level 11

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).

public Notification.Builder setContentText (CharSequence text)

Added in API level 11

Set the second line of text in the platform notification template.

public Notification.Builder setContentTitle (CharSequence title)

Added in API level 11

Set the first line of text in the platform notification template.

public Notification.Builder setDefaults (int defaults)

Added in API level 11

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.

public Notification.Builder setDeleteIntent (PendingIntent intent)

Added in API level 11

Supply a PendingIntent to send when the notification is cleared explicitly by the user.

See Also

public Notification.Builder setExtras (Bundle extras)

Added in API level 19

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.

See Also

public Notification.Builder setFullScreenIntent (PendingIntent intent, boolean highPriority)

Added in API level 11

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.

Parameters
intent The pending intent to launch.
highPriority Passing true will cause this notification to be sent even if other notifications are suppressed.
See Also

public Notification.Builder setGroup (String groupKey)

Added in API level 20

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).

Parameters
groupKey The group key of the group.
Returns
  • this object for method chaining

public Notification.Builder setGroupSummary (boolean isGroupSummary)

Added in API level 20

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).

Parameters
isGroupSummary Whether this notification should be a group summary.
Returns
  • this object for method chaining

public Notification.Builder setLargeIcon (Icon icon)

Added in API level 23

Add a large icon to the notification content view. In the platform template, this image will be shown on the left of the notification view in place of the small icon (which will be placed in a small badge atop the large icon).

public Notification.Builder setLargeIcon (Bitmap b)

Added in API level 11

Add a large icon to the notification content view. In the platform template, this image will be shown on the left of the notification view in place of the small icon (which will be placed in a small badge atop the large icon).

public Notification.Builder setLights (int argb, int onMs, int offMs)

Added in API level 11

Set the desired color for the indicator LED on the device, as well as the blink duty cycle (specified in milliseconds). Not all devices will honor all (or even any) of these values.

public Notification.Builder setLocalOnly (boolean localOnly)

Added in API level 20

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.

public Notification.Builder setNumber (int number)

Added in API level 11

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.

public Notification.Builder setOngoing (boolean ongoing)

Added in API level 11

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).

public Notification.Builder setOnlyAlertOnce (boolean onlyAlertOnce)

Added in API level 11

Set this flag if you would only like the sound, vibrate and ticker to be played if the notification is not already showing.

public Notification.Builder setPriority (int pri)

Added in API level 16

Set the priority of this notification.

See Also

public Notification.Builder setProgress (int max, int progress, boolean indeterminate)

Added in API level 14

Set the progress this notification represents. The platform template will represent this using a ProgressBar.

public Notification.Builder setPublicVersion (Notification n)

Added in API level 21

Supply a replacement Notification whose contents should be shown in insecure contexts (i.e. atop the secure lockscreen). See visibility and VISIBILITY_PUBLIC.

Parameters
n A replacement notification, presumably with some or all info redacted.
Returns
  • The same Builder.

public Notification.Builder setShowWhen (boolean show)

Added in API level 17

Control whether the timestamp set with setWhen is shown in the content view.

public Notification.Builder setSmallIcon (int icon, int level)

Added in API level 11

A variant of setSmallIcon(int) that takes an additional level parameter for when the icon is a LevelListDrawable.

Parameters
icon A resource ID in the application's package of the drawable to use.
level The level to use for the icon.
See Also

public Notification.Builder setSmallIcon (int icon)

Added in API level 11

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.

Parameters
icon A resource ID in the application's package of the drawable to use.
See Also

public Notification.Builder setSmallIcon (Icon icon)

Added in API level 23

Set the small icon, which will be used to represent the notification in the status bar and content view (unless overriden there by a large icon).

Parameters
icon An Icon object to use.
See Also

public Notification.Builder setSortKey (String sortKey)

Added in API level 20

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).

public Notification.Builder setSound (Uri sound, AudioAttributes audioAttributes)

Added in API level 21

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.

See Also

public Notification.Builder setSound (Uri sound)

Added in API level 11

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.

See Also

public Notification.Builder setSound (Uri sound, int streamType)

Added in API level 11

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.

See Also

public Notification.Builder setStyle (Notification.Style style)

Added in API level 16

Add a rich notification style to be applied at build time.

Parameters
style Object responsible for modifying the notification style.

public Notification.Builder setSubText (CharSequence text)

Added in API level 16

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.

public Notification.Builder setTicker (CharSequence tickerText, RemoteViews views)

Added in API level 11

Obsolete version of setTicker(CharSequence).

public Notification.Builder setTicker (CharSequence tickerText)

Added in API level 11

Set the "ticker" text which is sent to accessibility services.

See Also

public Notification.Builder setUsesChronometer (boolean b)

Added in API level 16

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).

See Also

public Notification.Builder setVibrate (long[] pattern)

Added in API level 11

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.

See Also

public Notification.Builder setVisibility (int visibility)

Added in API level 21

Specify the value of visibility.

Parameters
visibility One of VISIBILITY_PRIVATE (the default), VISIBILITY_SECRET, or VISIBILITY_PUBLIC.
Returns
  • The same Builder.

public Notification.Builder setWhen (long when)

Added in API level 11

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.

See Also