Stay organized with collections Save and categorize content based on your preferences.
added in version 22.1.0
belongs to Maven artifact com.android.support:support-compat:28.0.0-alpha1

NotificationCompat.Builder

public static class NotificationCompat.Builder
extends Object

java.lang.Object
   ↳ android.support.v4.app.NotificationCompat.Builder


Builder class for 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.

Summary

Fields

public ArrayList<String> mPeople

This field was deprecated in API level 27.1.0. This field was not meant to be public.

Public constructors

NotificationCompat.Builder(Context context, String channelId)

Constructor.

NotificationCompat.Builder(Context context)

This constructor was deprecated in API level 26.1.0. use NotificationCompat.Builder(Context, String) instead. All posted Notifications must specify a NotificationChannel Id.

Public methods

NotificationCompat.Builder addAction(NotificationCompat.Action action)

Add an action to this notification.

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

Add an action to this notification.

NotificationCompat.Builder addExtras(Bundle extras)

Merge additional metadata into this notification.

NotificationCompat.Builder addInvisibleAction(NotificationCompat.Action action)

Add an invisible action to this notification.

NotificationCompat.Builder addInvisibleAction(int icon, CharSequence title, PendingIntent intent)

Add an invisible action to 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.

Bundle getExtras()

Get the current metadata Bundle used by this notification Builder.

Notification getNotification()

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

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 setBadgeIconType(int icon)

Sets which icon to display as a badge for this notification.

NotificationCompat.Builder setCategory(String category)

Set the notification category.

NotificationCompat.Builder setChannelId(String channelId)

Specifies the channel the notification should be delivered on.

NotificationCompat.Builder setColor(int argb)

Sets color.

NotificationCompat.Builder setColorized(boolean colorize)

Set whether this notification should be colorized.

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 setGroupAlertBehavior(int groupAlertBehavior)

Sets the group alert behavior for this notification.

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.

NotificationCompat.Builder setRemoteInputHistory(CharSequence[] text)

Set the remote input history.

NotificationCompat.Builder setShortcutId(String shortcutId)

If this notification is duplicative of a Launcher shortcut, sets the id of the shortcut, in case the Launcher wants to hide the shortcut.

NotificationCompat.Builder setShowWhen(boolean show)

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

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 setSmallIcon(int icon)

Set the small icon to use in the notification layouts.

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, RemoteViews views)

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

NotificationCompat.Builder setTicker(CharSequence tickerText)

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

NotificationCompat.Builder setTimeoutAfter(long durationMs)

Specifies the time at which this notification should be canceled, if it is not already canceled.

NotificationCompat.Builder setUsesChronometer(boolean b)

Show the when field as a stopwatch.

NotificationCompat.Builder setVibrate(long[] pattern)

Set the vibration pattern to use.

NotificationCompat.Builder setVisibility(int visibility)

Sets visibility.

NotificationCompat.Builder setWhen(long when)

Set the time that the event occurred.

Protected methods

static CharSequence limitCharSequenceLength(CharSequence cs)

Inherited methods

From class java.lang.Object

Fields

mPeople

added in version 22.1.0
ArrayList<String> mPeople

This field was deprecated in API level 27.1.0.
This field was not meant to be public.

Public constructors

NotificationCompat.Builder

added in version 26.1.0
NotificationCompat.Builder (Context context, 
                String channelId)

Constructor. Automatically sets the when field to System.currentTimeMillis() and the audio stream to the STREAM_DEFAULT.

Parameters
context Context: A Context that will be used to construct the RemoteViews. The Context will not be held past the lifetime of this Builder object.

channelId String: The constructed Notification will be posted on this NotificationChannel.

NotificationCompat.Builder

added in version 22.1.0
NotificationCompat.Builder (Context context)

This constructor was deprecated in API level 26.1.0.
use NotificationCompat.Builder(Context, String) instead. All posted Notifications must specify a NotificationChannel Id.

Parameters
context Context

Public methods

addAction

added in version 22.1.0
NotificationCompat.Builder addAction (NotificationCompat.Action action)

Add an action to this notification. Actions are typically displayed by the system as a button adjacent to the notification content.
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. To ensure that an action button's functionality is always available, first implement the functionality in the Activity that starts when a user clicks the notification (see setContentIntent()), and then enhance the notification by implementing the same functionality with addAction().

Parameters
action NotificationCompat.Action: The action to add.

Returns
NotificationCompat.Builder

addAction

added in version 22.1.0
NotificationCompat.Builder addAction (int icon, 
                CharSequence title, 
                PendingIntent intent)

Add an action to this notification. Actions are typically displayed by the system as a button adjacent to the notification content.
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. To ensure that an action button's functionality is always available, first implement the functionality in the Activity that starts when a user clicks the notification (see setContentIntent()), and then enhance the notification by implementing the same functionality with addAction().

Parameters
icon int: Resource ID of a drawable that represents the action.

title CharSequence: Text describing the action.

intent PendingIntent: PendingIntent to be fired when the action is invoked.

Returns
NotificationCompat.Builder

addExtras

added in version 22.1.0
NotificationCompat.Builder addExtras (Bundle extras)

Merge additional metadata into this notification.

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

Parameters
extras Bundle

Returns
NotificationCompat.Builder

See also:

addInvisibleAction

NotificationCompat.Builder addInvisibleAction (NotificationCompat.Action action)

Add an invisible action to this notification. Invisible actions are never displayed by the system, but can be retrieved and used by other application listening to system notifications. Invisible actions are supported from Android 4.4.4 (API 20) and can be retrieved using getInvisibleActions(Notification).

Parameters
action NotificationCompat.Action: The action to add.

Returns
NotificationCompat.Builder

addInvisibleAction

NotificationCompat.Builder addInvisibleAction (int icon, 
                CharSequence title, 
                PendingIntent intent)

Add an invisible action to this notification. Invisible actions are never displayed by the system, but can be retrieved and used by other application listening to system notifications. Invisible actions are supported from Android 4.4.4 (API 20) and can be retrieved using getInvisibleActions(Notification).

Parameters
icon int: Resource ID of a drawable that represents the action.

title CharSequence: Text describing the action.

intent PendingIntent: PendingIntent to be fired when the action is invoked.

Returns
NotificationCompat.Builder

addPerson

added in version 22.1.0
NotificationCompat.Builder addPerson (String uri)

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 String: A URI for the person.

Returns
NotificationCompat.Builder

See also:

build

added in version 22.1.0
Notification build ()

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

Returns
Notification

extend

added in version 22.1.0
NotificationCompat.Builder extend (NotificationCompat.Extender extender)

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

Parameters
extender NotificationCompat.Extender

Returns
NotificationCompat.Builder

getExtras

added in version 22.1.0
Bundle getExtras ()

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.