Notification.Builder
public
static
class
Notification.Builder
extends Object
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();
Summary
Public constructors | |
---|---|
Builder(Context context)
This constructor is deprecated.
use |
|
Builder(Context context, String channelId)
Constructs a new Builder with the defaults: |
Public methods | |
---|---|
Notification.Builder
|
addAction(int icon, CharSequence title, PendingIntent intent)
This method was deprecated
in API level 23.
Use |
Notification.Builder
|
addAction(Notification.Action action)
Add an action to this notification. |
Notification.Builder
|
addExtras(Bundle extras)
Merge additional metadata into this notification. |
Notification.Builder
|
addPerson(String uri)
This method was deprecated
in API level 28.
use |
Notification.Builder
|
addPerson(Person person)
Add a person that is relevant to this notification. |
Notification
|
build()
Combine all of the options that have been set and return a new |
RemoteViews
|
createBigContentView()
This method was deprecated in API level 35. For performance and system health reasons, this API is no longer required to be used directly by the System UI when rendering Notifications to the user. While the UI returned by this method will still represent the content of the Notification being built, it may differ from the visual style of the system. NOTE: this API has always had severe limitations; for example it does not support any interactivity, it ignores the app theme, it hard-codes the colors from the system theme at the time it is called, and it does Bitmap decoding on the main thread which can cause UI jank. |
RemoteViews
|
createContentView()
This method was deprecated in API level 35. For performance and system health reasons, this API is no longer required to be used directly by the System UI when rendering Notifications to the user. While the UI returned by this method will still represent the content of the Notification being built, it may differ from the visual style of the system. NOTE: this API has always had severe limitations; for example it does not support any interactivity, it ignores the app theme, it hard-codes the colors from the system theme at the time it is called, and it does Bitmap decoding on the main thread which can cause UI jank. |
RemoteViews
|
createHeadsUpContentView()
This method was deprecated in API level 35. For performance and system health reasons, this API is no longer required to be used directly by the System UI when rendering Notifications to the user. While the UI returned by this method will still represent the content of the Notification being built, it may differ from the visual style of the system. NOTE: this API has always had severe limitations; for example it does not support any interactivity, it ignores the app theme, it hard-codes the colors from the system theme at the time it is called, and it does Bitmap decoding on the main thread which can cause UI jank. |
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 |
Notification.Style
|
getStyle()
Returns the style set by |
static
Notification.Builder
|
recoverBuilder(Context context, Notification n)
Creates a Builder from an existing notification so further changes can be made. |
Notification.Builder
|
setActions(Action... actions)
Alter the complete list of actions attached to this notification. |
Notification.Builder
|
setAllowSystemGeneratedContextualActions(boolean allowed)
Determines whether the platform can generate contextual actions for a notification. |
Notification.Builder
|
setAutoCancel(boolean autoCancel)
Make this notification automatically dismissed when the user touches it. |
Notification.Builder
|
setBadgeIconType(int icon)
Sets which icon to display as a badge for this notification. |
Notification.Builder
|
setBubbleMetadata(Notification.BubbleMetadata data)
Sets the |
Notification.Builder
|
setCategory(String category)
Set the notification category. |
Notification.Builder
|
setChannelId(String channelId)
Specifies the channel the notification should be delivered on. |
Notification.Builder
|
setChronometerCountDown(boolean countDown)
Sets the Chronometer to count down instead of counting up. |
Notification.Builder
|
setColor(int argb)
Sets |
Notification.Builder
|
setColorized(boolean colorize)
Set whether this notification should be colorized. |
Notification.Builder
|
setContent(RemoteViews views)
Supply a custom RemoteViews to use instead of the platform template. |
Notification.Builder
|
setContentInfo(CharSequence info)
This method was deprecated
in API level 24.
use |
Notification.Builder
|
setContentIntent(PendingIntent intent)
Supply a |
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
|
setCustomBigContentView(RemoteViews contentView)
Supply custom RemoteViews to use instead of the platform template in the expanded form. |
Notification.Builder
|
setCustomContentView(RemoteViews contentView)
Supply custom RemoteViews to use instead of the platform template. |
Notification.Builder
|
setCustomHeadsUpContentView(RemoteViews contentView)
Supply custom RemoteViews to use instead of the platform template in the heads up dialog. |
Notification.Builder
|
setDefaults(int defaults)
This method was deprecated
in API level 26.
use |
Notification.Builder
|
setDeleteIntent(PendingIntent intent)
Supply a |
Notification.Builder
|
setExtras(Bundle extras)
Set metadata for this notification. |
Notification.Builder
|
setFlag(int mask, boolean value)
Set the value for a notification flag |
Notification.Builder
|
setForegroundServiceBehavior(int behavior)
Specify a desired visibility policy for a Notification associated with a foreground service. |
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
|
setGroupAlertBehavior(int groupAlertBehavior)
Sets the group alert behavior for this notification. |
Notification.Builder
|
setGroupSummary(boolean isGroupSummary)
Set this notification to be the group summary for a group of notifications. |
Notification.Builder
|
setLargeIcon(Bitmap b)
Add a large icon to the notification content view. |
Notification.Builder
|
setLargeIcon(Icon icon)
Add a large icon to the notification content view. |
Notification.Builder
|
setLights(int argb, int onMs, int offMs)
This method was deprecated
in API level 26.
use |
Notification.Builder
|
setLocalOnly(boolean localOnly)
Set whether or not this notification should not bridge to other devices. |
Notification.Builder
|
setLocusId(LocusId locusId)
Sets the |
Notification.Builder
|
setNumber(int number)
Sets the number of items this notification represents. |
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)
This method was deprecated
in API level 26.
use |
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
|
setRemoteInputHistory(CharSequence[] text)
Set the remote input history. |
Notification.Builder
|
setSettingsText(CharSequence text)
Provides text that will appear as a link to your application's settings. |
Notification.Builder
|
setShortcutId(String shortcutId)
From Android 11, messaging notifications (those that use |
Notification.Builder
|
setShowWhen(boolean show)
Control whether the timestamp set with |
Notification.Builder
|
setSmallIcon(int icon, int level)
A variant of |
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 overridden there by a
|
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)
This method was deprecated
in API level 26.
use |
Notification.Builder
|
setSound(Uri sound)
This method was deprecated
in API level 26.
use |
Notification.Builder
|
setSound(Uri sound, int streamType)
This method was deprecated
in API level 21.
use |
Notification.Builder
|
setStyle(Notification.Style style)
Add a rich notification style to be applied at build time. |
Notification.Builder
|
setSubText(CharSequence text)
This provides some additional information that is displayed in the notification. |
Notification.Builder
|
setTicker(CharSequence tickerText, RemoteViews views)
Obsolete version of |
Notification.Builder
|
setTicker(CharSequence tickerText)
Set the "ticker" text which is sent to accessibility services. |
Notification.Builder
|
setTimeoutAfter(long durationMs)
Specifies a duration in milliseconds after which this notification should be canceled, if it is not already canceled. |
Notification.Builder
|
setUsesChronometer(boolean b)
Show the |
Notification.Builder
|
setVibrate(long[] pattern)
This method was deprecated
in API level 26.
use |
Notification.Builder
|
setVisibility(int visibility)
Specify the value of |
Notification.Builder
|
setWhen(long when)
Add a timestamp pertaining to the notification (usually the time the event occurred). |
Inherited methods | |
---|---|
Public constructors
Builder
public Builder (Context context)
This constructor is deprecated.
use Builder(android.content.Context, java.lang.String)
instead. All posted Notifications must specify a NotificationChannel Id.
Parameters | |
---|---|
context |
Context |
Builder
public Builder (Context context, String channelId)
Constructs a new Builder with the defaults:
Parameters | |
---|---|
context |
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. |
channelId |
String : The constructed Notification will be posted on this
NotificationChannel . To use a NotificationChannel, it must first be
created using NotificationManager.createNotificationChannel . |
Public methods
addAction
public Notification.Builder addAction (int icon, CharSequence title, PendingIntent intent)
This method was deprecated
in API level 23.
Use addAction(android.app.Notification.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 Notification.contentIntent
).
As of Android Build.VERSION_CODES.S
, apps targeting API level
Build.VERSION_CODES.S
or higher won't be able to start activities
while processing broadcast receivers or services in response to notification action
clicks. To launch an activity in those cases, provide a PendingIntent
to the
activity itself.
As of Android Build.VERSION_CODES.N
,
action button icons will not be displayed on action buttons, but are still required
and are available to
notification listeners
,
which may display them in other contexts, for example on a wearable device.
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 | |
---|---|
Notification.Builder |
addAction
public Notification.Builder addAction (Notification.Action action)
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 Notification.contentIntent
).
Parameters | |
---|---|
action |
Notification.Action : The action to add. |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
addExtras
public Notification.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 | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
addPerson
public Notification.Builder addPerson (String uri)
This method was deprecated
in API level 28.
use addPerson(android.app.Person)
Add a person that is relevant to this notification.
Depending on user preferences, this annotation may allow the notification to pass
through interruption filters, if this notification is of category Notification.CATEGORY_CALL
or Notification.CATEGORY_MESSAGE
. The addition of people may also cause this notification 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
.
It is also possible to provide a URI with the schema name:
in order to uniquely
identify a person without an entry in the contacts database.
Parameters | |
---|---|
uri |
String : A URI for the person. |
Returns | |
---|---|
Notification.Builder |
See also:
addPerson
public Notification.Builder addPerson (Person person)
Add a person that is relevant to this notification.
Depending on user preferences, this annotation may allow the notification to pass
through interruption filters, if this notification is of category Notification.CATEGORY_CALL
or Notification.CATEGORY_MESSAGE
. The addition of people may also cause this notification to
appear more prominently in the user interface.
A person should usually contain a uri in order to benefit from the ranking boost. However, even if no uri is provided, it's beneficial to provide other people in the notification, such that listeners and voice only devices can announce and handle them properly.
Parameters | |
---|---|
person |
Person : the person to add. |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
build
public Notification build ()
Combine all of the options that have been set and return a new Notification
object.
If this notification has BubbleMetadata
attached that was created with
a shortcutId a check will be performed to ensure the shortcutId supplied to bubble
metadata matches the shortcutId set on the notification builder, if one was set.
If the shortcutId's were specified but do not match, an exception is thrown here.
Returns | |
---|---|
Notification |
This value cannot be null . |
createBigContentView
public RemoteViews createBigContentView ()
This method was deprecated
in API level 35.
For performance and system health reasons, this API is no longer required to
be used directly by the System UI when rendering Notifications to the user. While the UI
returned by this method will still represent the content of the Notification being
built, it may differ from the visual style of the system.
NOTE: this API has always had severe limitations; for example it does not support any
interactivity, it ignores the app theme, it hard-codes the colors from the system theme
at the time it is called, and it does Bitmap decoding on the main thread which can cause
UI jank.
Construct a RemoteViews representing the expanded notification layout.
Returns | |
---|---|
RemoteViews |
createContentView
public RemoteViews createContentView ()
This method was deprecated
in API level 35.
For performance and system health reasons, this API is no longer required to
be used directly by the System UI when rendering Notifications to the user. While the UI
returned by this method will still represent the content of the Notification being
built, it may differ from the visual style of the system.
NOTE: this API has always had severe limitations; for example it does not support any
interactivity, it ignores the app theme, it hard-codes the colors from the system theme
at the time it is called, and it does Bitmap decoding on the main thread which can cause
UI jank.
Construct a RemoteViews representing the standard notification layout.
Returns | |
---|---|
RemoteViews |
createHeadsUpContentView
public RemoteViews createHeadsUpContentView ()
This method was deprecated
in API level 35.
For performance and system health reasons, this API is no longer required to
be used directly by the System UI when rendering Notifications to the user. While the UI
returned by this method will still represent the content of the Notification being
built, it may differ from the visual style of the system.
NOTE: this API has always had severe limitations; for example it does not support any
interactivity, it ignores the app theme, it hard-codes the colors from the system theme
at the time it is called, and it does Bitmap decoding on the main thread which can cause
UI jank.
Construct a RemoteViews representing the heads up notification layout.
Returns | |
---|---|
RemoteViews |
extend
public Notification.Builder extend (Notification.Extender extender)
Apply an extender to this notification builder. Extenders may be used to add metadata or change options on this builder.
Parameters | |
---|---|
extender |
Notification.Extender |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
getExtras
public 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.
Returns | |
---|---|
Bundle |
See also:
getNotification
public Notification getNotification ()
This method was deprecated
in API level 16.
Use build()
instead.
Returns | |
---|---|
Notification |
getStyle
public Notification.Style getStyle ()
Returns the style set by setStyle(android.app.Notification.Style)
.
Returns | |
---|---|
Notification.Style |
recoverBuilder
public static Notification.Builder recoverBuilder (Context context, Notification n)
Creates a Builder from an existing notification so further changes can be made.
Parameters | |
---|---|
context |
Context : The context for your application / activity. |
n |
Notification : The notification to create a Builder from. |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setActions
public Notification.Builder setActions (Action... actions)
Alter the complete list of actions attached to this notification.
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setAllowSystemGeneratedContextualActions
public Notification.Builder setAllowSystemGeneratedContextualActions (boolean allowed)
Determines whether the platform can generate contextual actions for a notification. By default this is true.
Parameters | |
---|---|
allowed |
boolean |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setAutoCancel
public Notification.Builder setAutoCancel (boolean autoCancel)
Make this notification automatically dismissed when the user touches it.
Parameters | |
---|---|
autoCancel |
boolean |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setBadgeIconType
public Notification.Builder setBadgeIconType (int icon)
Sets which icon to display as a badge for this notification.
Must be one of Notification.BADGE_ICON_NONE
, Notification.BADGE_ICON_SMALL
,
Notification.BADGE_ICON_LARGE
.
Note: This value might be ignored, for launchers that don't support badge icons.
Parameters | |
---|---|
icon |
int |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setBubbleMetadata
public Notification.Builder setBubbleMetadata (Notification.BubbleMetadata data)
Sets the BubbleMetadata
that will be used to display app content in a floating
window over the existing foreground activity.
This data will be ignored unless the notification is posted to a channel that
allows bubbles
.
Notifications allowed to bubble that have valid bubble metadata will display in collapsed state outside of the notification shade on unlocked devices. When a user interacts with the collapsed state, the bubble intent will be invoked and displayed.
Parameters | |
---|---|
data |
Notification.BubbleMetadata : This value may be null . |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setCategory
public Notification.Builder setCategory (String category)
Set the notification category.
Parameters | |
---|---|
category |
String |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setChannelId
public Notification.Builder setChannelId (String channelId)
Specifies the channel the notification should be delivered on.
Parameters | |
---|---|
channelId |
String |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setChronometerCountDown
public Notification.Builder setChronometerCountDown (boolean countDown)
Sets the Chronometer to count down instead of counting up.
This is only relevant if setUsesChronometer(boolean)
has been set to true.
If it isn't set the chronometer will count up.
Parameters | |
---|---|
countDown |
boolean |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setColor
public Notification.Builder setColor (int argb)
Sets Notification.color
.
Parameters | |
---|---|
argb |
int : The accent color to use |
Returns | |
---|---|
Notification.Builder |
The same Builder.
This value cannot be null . |
setColorized
public Notification.Builder setColorized (boolean colorize)
Set whether this notification should be colorized. When set, the color set with
setColor(int)
will be used as the background color of this notification.
This should only be used for high priority ongoing tasks like navigation, an ongoing call, or other similarly high-priority events for the user.
For most styles, the coloring will only be applied if the notification is for a
foreground service notification.
However, for MediaStyle
and DecoratedMediaCustomViewStyle
notifications
that have a media session attached there is no such requirement.
Parameters | |
---|---|
colorize |
boolean |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setContent
public Notification.Builder setContent (RemoteViews views)
Supply a custom RemoteViews to use instead of the platform template.
Use setCustomContentView(android.widget.RemoteViews)
instead.
Parameters | |
---|---|
views |
RemoteViews |
Returns | |
---|---|
Notification.Builder |
setContentInfo
public Notification.Builder setContentInfo (CharSequence info)
This method was deprecated
in API level 24.
use setSubText(java.lang.CharSequence)
instead to set a text in the header.
For legacy apps targeting a version below Build.VERSION_CODES.N
this
field will still show up, but the subtext will take precedence.
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).
Parameters | |
---|---|
info |
CharSequence |
Returns | |
---|---|
Notification.Builder |
setContentIntent
public Notification.Builder setContentIntent (PendingIntent intent)
Supply a PendingIntent
to be sent when the notification is clicked.
As of Android Build.VERSION_CODES.S
, apps targeting API level
Build.VERSION_CODES.S
or higher won't be able to start activities
while processing broadcast receivers or services in response to notification clicks. To
launch an activity in those cases, provide a PendingIntent
for the activity
itself.
As of Build.VERSION_CODES.HONEYCOMB
, if this field is unset and you
have specified a custom RemoteViews with setContent(android.widget.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).
Parameters | |
---|---|
intent |
PendingIntent |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setContentText
public Notification.Builder setContentText (CharSequence text)
Set the second line of text in the platform notification template.
Parameters | |
---|---|
text |
CharSequence |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setContentTitle
public Notification.Builder setContentTitle (CharSequence title)
Set the first line of text in the platform notification template.
Parameters | |
---|---|
title |
CharSequence |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setCustomBigContentView
public Notification.Builder setCustomBigContentView (RemoteViews contentView)
Supply custom RemoteViews to use instead of the platform template in the expanded form. This will override the expanded layout that would otherwise be constructed by this Builder object.
Parameters | |
---|---|
contentView |
RemoteViews |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setCustomContentView
public Notification.Builder setCustomContentView (RemoteViews contentView)
Supply custom RemoteViews to use instead of the platform template. This will override the layout that would otherwise be constructed by this Builder object.
Parameters | |
---|---|
contentView |
RemoteViews |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setCustomHeadsUpContentView
public Notification.Builder setCustomHeadsUpContentView (RemoteViews contentView)
Supply custom RemoteViews to use instead of the platform template in the heads up dialog. This will override the heads-up layout that would otherwise be constructed by this Builder object.
Parameters | |
---|---|
contentView |
RemoteViews |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setDefaults
public Notification.Builder setDefaults (int defaults)
This method was deprecated
in API level 26.
use NotificationChannel.enableVibration(boolean)
and
NotificationChannel.enableLights(boolean)
and
NotificationChannel.setSound(Uri, AudioAttributes)
instead.
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:
Notification.DEFAULT_SOUND
, Notification.DEFAULT_VIBRATE
, Notification.DEFAULT_LIGHTS
.
For all default values, use Notification.DEFAULT_ALL
.
Parameters | |
---|---|
defaults |
int |
Returns | |
---|---|
Notification.Builder |
setDeleteIntent
public Notification.Builder setDeleteIntent (PendingIntent intent)
Supply a PendingIntent
to send when the notification is cleared explicitly by the user.
Parameters | |
---|---|
intent |
PendingIntent |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setExtras
public Notification.Builder setExtras (Bundle extras)
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.
Parameters | |
---|---|
extras |
Bundle |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setFlag
public Notification.Builder setFlag (int mask, boolean value)
Set the value for a notification flag
Parameters | |
---|---|
mask |
int : Bit mask of the flag
Value is either 0 or a combination of Notification.FLAG_SHOW_LIGHTS , Notification.FLAG_ONGOING_EVENT , Notification.FLAG_INSISTENT , Notification.FLAG_ONLY_ALERT_ONCE , Notification.FLAG_AUTO_CANCEL , Notification.FLAG_NO_CLEAR , Notification.FLAG_FOREGROUND_SERVICE , Notification.FLAG_HIGH_PRIORITY , Notification.FLAG_LOCAL_ONLY , Notification.FLAG_GROUP_SUMMARY , android.app.Notification.FLAG_AUTOGROUP_SUMMARY, android.app.Notification.FLAG_CAN_COLORIZE, Notification.FLAG_BUBBLE , android.app.Notification.FLAG_NO_DISMISS, android.app.Notification.FLAG_FSI_REQUESTED_BUT_DENIED, android.app.Notification.FLAG_USER_INITIATED_JOB, and android.app.Notification.FLAG_SILENT |
value |
boolean : Status (on/off) of the flag |
Returns | |
---|---|
Notification.Builder |
The same Builder.
This value cannot be null . |
setForegroundServiceBehavior
public Notification.Builder setForegroundServiceBehavior (int behavior)
Specify a desired visibility policy for a Notification associated with a
foreground service. By default, the system can choose to defer
visibility of the notification for a short time after the service is
started. Pass
FOREGROUND_SERVICE_IMMEDIATE
to this method in order to guarantee that visibility is never deferred. Pass
FOREGROUND_SERVICE_DEFERRED
to request that visibility is deferred whenever possible.
Note that deferred visibility is not guaranteed. There may be some circumstances under which the system will show the foreground service's associated Notification immediately even when the app has used this method to explicitly request deferred display.
Parameters | |
---|---|
behavior |
int : One of
FOREGROUND_SERVICE_DEFAULT ,
FOREGROUND_SERVICE_IMMEDIATE ,
or FOREGROUND_SERVICE_DEFERRED
Value is Notification.FOREGROUND_SERVICE_DEFAULT , Notification.FOREGROUND_SERVICE_IMMEDIATE , or Notification.FOREGROUND_SERVICE_DEFERRED |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setFullScreenIntent
public Notification.Builder setFullScreenIntent (PendingIntent intent, boolean highPriority)
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.
Apps targeting Build.VERSION_CODES.Q
and above will have to request
a permission (Manifest.permission.USE_FULL_SCREEN_INTENT
) in order to
use full screen intents.
Prior to Build.VERSION_CODES.TIRAMISU
, the system may display a
heads up notification (which may display on screen longer than other heads up
notifications), instead of launching the intent, while the user is using the device.
From Build.VERSION_CODES.TIRAMISU
,
the system UI will display a heads up notification, instead of launching this intent,
while the user is using the device. This notification will display with emphasized
action buttons. If the posting app holds
Manifest.permission.USE_FULL_SCREEN_INTENT
, then the heads
up notification will appear persistently until the user dismisses or snoozes it, or
the app cancels it. If the posting app does not hold
Manifest.permission.USE_FULL_SCREEN_INTENT
, then the notification will
appear as heads up notification even when the screen is locked or turned off, and this
notification will only be persistent for 60 seconds.
To be launched as a full screen intent, the notification must also be posted to a channel with importance level set to IMPORTANCE_HIGH or higher.
Requires
Manifest.permission.USE_FULL_SCREEN_INTENT
Parameters | |
---|---|
intent |
PendingIntent : The pending intent to launch. |
highPriority |
boolean : Passing true will cause this notification to be sent
even if other notifications are suppressed. |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setGroup
public Notification.Builder setGroup (String groupKey)
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 |
String : The group key of the group. |
Returns | |
---|---|
Notification.Builder |
this object for method chaining
This value cannot be null . |
setGroupAlertBehavior
public Notification.Builder setGroupAlertBehavior (int groupAlertBehavior)
Sets the group alert behavior for this notification. Use this method to mute this
notification if alerts for this notification's group should be handled by a different
notification. This is only applicable for notifications that belong to a
group
. This must be called on all notifications you want to
mute. For example, if you want only the summary of your group to make noise and/or peek
on screen, all children in the group should have the group alert behavior
Notification.GROUP_ALERT_SUMMARY
.
The default value is Notification.GROUP_ALERT_ALL
.
Parameters | |
---|---|
groupAlertBehavior |
int : Value is Notification.GROUP_ALERT_ALL , Notification.GROUP_ALERT_CHILDREN , or Notification.GROUP_ALERT_SUMMARY |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setGroupSummary
public Notification.Builder setGroupSummary (boolean isGroupSummary)
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. If thereRequires a group key also be set using setGroup(String)
.
The group summary may be suppressed if too few notifications are included in the group.
Parameters | |
---|---|
isGroupSummary |
boolean : Whether this notification should be a group summary. |
Returns | |
---|---|
Notification.Builder |
this object for method chaining
This value cannot be null . |
setLargeIcon
public Notification.Builder setLargeIcon (Bitmap b)
Add a large icon to the notification content view.
In the platform template, this image will be shown either on the right of the
notification, with an aspect ratio of up to 16:9, or (when the notification is grouped)
on the left in place of the small icon
.
Parameters | |
---|---|
b |
Bitmap |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setLargeIcon
public Notification.Builder setLargeIcon (Icon icon)
Add a large icon to the notification content view.
In the platform template, this image will be shown either on the right of the
notification, with an aspect ratio of up to 16:9, or (when the notification is grouped)
on the left in place of the small icon
.
Parameters | |
---|---|
icon |
Icon |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setLights
public Notification.Builder setLights (int argb, int onMs, int offMs)
This method was deprecated
in API level 26.
use NotificationChannel.enableLights(boolean)
instead.
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.
Parameters | |
---|---|
argb |
int |
onMs |
int |
offMs |
int |
Returns | |
---|---|
Notification.Builder |
setLocalOnly
public Notification.Builder setLocalOnly (boolean localOnly)
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.
Parameters | |
---|---|
localOnly |
boolean |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setLocusId
public Notification.Builder setLocusId (LocusId locusId)
Sets the LocusId
associated with this notification.
This method should be called when the LocusId
is used in other places (such
as ShortcutInfo
and ContentCaptureContext
) so the device's intelligence
services can correlate them.
Parameters | |
---|---|
locusId |
LocusId : This value may be null . |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setNumber
public Notification.Builder setNumber (int number)
Sets the number of items this notification represents. May be displayed as a badge count for Launchers that support badging.
Parameters | |
---|---|
number |
int |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setOngoing
public Notification.Builder setOngoing (boolean ongoing)
Set whether this is an "ongoing" notification. Ongoing notifications cannot be dismissed by the user on locked devices, or by notification listeners, and some notifications (call, device management, media) cannot be dismissed on unlocked devices, 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).
Parameters | |
---|---|
ongoing |
boolean |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setOnlyAlertOnce
public 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. Note that using this flag will stop any ongoing alerting behaviour such as sound, vibration or blinking notification LED.
Parameters | |
---|---|
onlyAlertOnce |
boolean |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setPriority
public Notification.Builder setPriority (int pri)
This method was deprecated
in API level 26.
use NotificationChannel.setImportance(int)
instead.
Set the priority of this notification.
Parameters | |
---|---|
pri |
int : Value is Notification.PRIORITY_DEFAULT , Notification.PRIORITY_LOW , Notification.PRIORITY_MIN , Notification.PRIORITY_HIGH , or Notification.PRIORITY_MAX |
Returns | |
---|---|
Notification.Builder |
See also:
setProgress
public Notification.Builder setProgress (int max, int progress, boolean indeterminate)
Set the progress this notification represents.
The platform template will represent this using a ProgressBar
.
Parameters | |
---|---|
max |
int |
progress |
int |
indeterminate |
boolean |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setPublicVersion
public Notification.Builder setPublicVersion (Notification n)
Supply a replacement Notification whose contents should be shown in insecure contexts
(i.e. atop the secure lockscreen). See Notification.visibility
and Notification.VISIBILITY_PUBLIC
.
Parameters | |
---|---|
n |
Notification : A replacement notification, presumably with some or all info redacted. |
Returns | |
---|---|
Notification.Builder |
The same Builder.
This value cannot be null . |
setRemoteInputHistory
public Notification.Builder setRemoteInputHistory (CharSequence[] text)
Set the remote input history.
This should be set to the most recent inputs that have been sent
through a 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
.
Parameters | |
---|---|
text |
CharSequence |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setSettingsText
public Notification.Builder setSettingsText (CharSequence text)
Provides text that will appear as a link to your application's settings.
This text does not appear within notification templates
but may
appear when the user uses an affordance to learn more about the notification.
Additionally, this text will not appear unless you provide a valid link target by
handling Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES
.
This text is meant to be concise description about what the user can customize when they click on this link. The recommended maximum length is 40 characters.
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setShortcutId
public Notification.Builder setShortcutId (String shortcutId)
From Android 11, messaging notifications (those that use MessagingStyle
) that
use this method to link to a published long-lived sharing shortcut may appear in a
dedicated Conversation section of the shade and may show configuration options that
are unique to conversations. This behavior should be reserved for person to person(s)
conversations where there is a likely social obligation for an individual to respond.
For example, the following are some examples of notifications that belong in the conversation space:
- 1:1 conversations between two individuals
- Group conversations between individuals where everyone can contribute
- Advertisements from a bot (even if personal and contextualized)
- Engagement notifications from a bot
- Directional conversations where there is an active speaker and many passive individuals
- Stream / posting updates from other individuals
- Email, document comments, or other conversation types that are not real-time
Additionally, this method can be used for all types of notifications to mark this notification as duplicative of a Launcher shortcut. Launchers that show badges or notification content may then suppress the shortcut in favor of the content of this notification.
If this notification has BubbleMetadata
attached that was created with
a shortcutId a check will be performed to ensure the shortcutId supplied to bubble
metadata matches the shortcutId set here, if one was set. If the shortcutId's were
specified but do not match, an exception is thrown.
Parameters | |
---|---|
shortcutId |
String : the id of the shortcut this notification
is linked to |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setShowWhen
public Notification.Builder setShowWhen (boolean show)
Control whether the timestamp set with setWhen
is shown
in the content view.
Parameters | |
---|---|
show |
boolean |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setSmallIcon
public 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
.
Parameters | |
---|---|
icon |
int : A resource ID in the application's package of the drawable to use. |
level |
int : The level to use for the icon. |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setSmallIcon
public Notification.Builder setSmallIcon (int 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.
Parameters | |
---|---|
icon |
int : A resource ID in the application's package of the drawable to use. |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setSmallIcon
public 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 overridden there by a
large icon
).
Parameters | |
---|---|
icon |
Icon : An Icon object to use. |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setSortKey
public Notification.Builder setSortKey (String sortKey)
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)
.
Parameters | |
---|---|
sortKey |
String |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setSound
public Notification.Builder setSound (Uri sound, AudioAttributes audioAttributes)
This method was deprecated
in API level 26.
use NotificationChannel.setSound(Uri, AudioAttributes)
instead.
Set the sound to play, along with specific audio attributes
to
use during playback.
Parameters | |
---|---|
sound |
Uri |
audioAttributes |
AudioAttributes |
Returns | |
---|---|
Notification.Builder |
See also:
setSound
public Notification.Builder setSound (Uri sound)
This method was deprecated
in API level 26.
use NotificationChannel.setSound(Uri, AudioAttributes)
instead.
Set the sound to play.
It will be played using the default audio attributes
for notifications.
Parameters | |
---|---|
sound |
Uri |
Returns | |
---|---|
Notification.Builder |
setSound
public Notification.Builder setSound (Uri sound, int streamType)
This method was deprecated
in API level 21.
use NotificationChannel.setSound(Uri, AudioAttributes)
.
Set the sound to play, along with a specific stream on which to play it.
See AudioManager
for the STREAM_
constants.
Parameters | |
---|---|
sound |
Uri |
streamType |
int |
Returns | |
---|---|
Notification.Builder |
setStyle
public Notification.Builder setStyle (Notification.Style style)
Add a rich notification style to be applied at build time.
Parameters | |
---|---|
style |
Notification.Style : Object responsible for modifying the notification style. |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setSubText
public Notification.Builder setSubText (CharSequence text)
This provides some additional information that is displayed in the notification. No guarantees are given where exactly it is displayed.
This information should only be provided if it provides an essential benefit to the understanding of the notification. The more text you provide the less readable it becomes. For example, an email client should only provide the account name here if more than one email account has been added.
As of Build.VERSION_CODES.N
this information is displayed in the
notification header area.
On Android versions before Build.VERSION_CODES.N
this will be shown in the third line of text in the platform notification template.
You should not be using setProgress(int, int, boolean)
at the
same time on those versions; they occupy the same place.
Parameters | |
---|---|
text |
CharSequence |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setTicker
public Notification.Builder setTicker (CharSequence tickerText, RemoteViews views)
Obsolete version of setTicker(java.lang.CharSequence)
.
Parameters | |
---|---|
tickerText |
CharSequence |
views |
RemoteViews |
Returns | |
---|---|
Notification.Builder |
setTicker
public Notification.Builder setTicker (CharSequence tickerText)
Set the "ticker" text which is sent to accessibility services.
Parameters | |
---|---|
tickerText |
CharSequence |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also:
setTimeoutAfter
public Notification.Builder setTimeoutAfter (long durationMs)
Specifies a duration in milliseconds after which this notification should be canceled, if it is not already canceled.
Parameters | |
---|---|
durationMs |
long |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setUsesChronometer
public Notification.Builder setUsesChronometer (boolean b)
Show the 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).
The counter can also be set to count down to when
when using
setChronometerCountDown(boolean)
.
Parameters | |
---|---|
b |
boolean |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
setVibrate
public Notification.Builder setVibrate (long[] pattern)
This method was deprecated
in API level 26.
use NotificationChannel.setVibrationPattern(long[])
instead.
Set the vibration pattern to use.
See Vibrator.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.
Parameters | |
---|---|
pattern |
long |
Returns | |
---|---|
Notification.Builder |
See also:
setVisibility
public Notification.Builder setVisibility (int visibility)
Specify the value of Notification.visibility
.
Parameters | |
---|---|
visibility |
int : Value is Notification.VISIBILITY_PUBLIC , Notification.VISIBILITY_PRIVATE , or Notification.VISIBILITY_SECRET |
Returns | |
---|---|
Notification.Builder |
The same Builder.
This value cannot be null . |
setWhen
public Notification.Builder setWhen (long when)
Add a timestamp pertaining to the notification (usually the time the event occurred).
Parameters | |
---|---|
when |
long |
Returns | |
---|---|
Notification.Builder |
This value cannot be null . |
See also: