public class

Notification

extends Object
implements Parcelable
java.lang.Object
   ↳ android.app.Notification

Class Overview

A class that represents how a persistent notification is to be presented to the user using the NotificationManager.

The Notification.Builder has been added to make it easier to construct Notifications.

Developer Guides

For a guide to creating notifications, read the Status Bar Notifications developer guide.

Summary

Nested Classes
class Notification.Builder Builder class for Notification objects. 
Constants
int DEFAULT_ALL Use all default values (where applicable).
int DEFAULT_LIGHTS Use the default notification lights.
int DEFAULT_SOUND Use the default notification sound.
int DEFAULT_VIBRATE Use the default notification vibrate.
int FLAG_AUTO_CANCEL Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user.
int FLAG_FOREGROUND_SERVICE Bit to be bitwise-ored into the flags field that should be set if this notification represents a currently running service.
int FLAG_HIGH_PRIORITY Bit to be bitwise-ored into the flags field that should be set if this notification represents a high-priority event that may be shown to the user even if notifications are otherwise unavailable (that is, when the status bar is hidden).
int FLAG_INSISTENT Bit to be bitwise-ored into the flags field that if set, the audio will be repeated until the notification is cancelled or the notification window is opened.
int FLAG_NO_CLEAR Bit to be bitwise-ored into the flags field that should be set if the notification should not be canceled when the user clicks the Clear all button.
int FLAG_ONGOING_EVENT Bit to be bitwise-ored into the flags field that should be set if this notification is in reference to something that is ongoing, like a phone call.
int FLAG_ONLY_ALERT_ONCE Bit to be bitwise-ored into the flags field that should be set if you want the sound and/or vibration play each time the notification is sent, even if it has not been canceled before that.
int FLAG_SHOW_LIGHTS Bit to be bitwise-ored into the flags field that should be set if you want the LED on for this notification.
int STREAM_DEFAULT Use this constant as the value for audioStreamType to request that the default stream type for notifications be used.
[Expand]
Inherited Constants
From interface android.os.Parcelable
Fields
public static final Creator<Notification> CREATOR Parcelable.Creator that instantiates Notification objects
public int audioStreamType The audio stream type to use when playing the sound.
public PendingIntent contentIntent The intent to execute when the expanded status entry is clicked.
public RemoteViews contentView The view that will represent this notification in the expanded status bar.
public int defaults Specifies which values should be taken from the defaults.
public PendingIntent deleteIntent The intent to execute when the status entry is deleted by the user with the "Clear All Notifications" button.
public int flags
public PendingIntent fullScreenIntent An intent to launch instead of posting the notification to the status bar.
public int icon The resource id of a drawable to use as the icon in the status bar.
public int iconLevel If the icon in the status bar is to have more than one level, you can set this.
public Bitmap largeIcon The bitmap that may escape the bounds of the panel and bar.
public int ledARGB The color of the led.
public int ledOffMS The number of milliseconds for the LED to be off while it's flashing.
public int ledOnMS The number of milliseconds for the LED to be on while it's flashing.
public int number The number of events that this notification represents.
public Uri sound The sound to play.
public CharSequence tickerText Text to scroll across the screen when this item is added to the status bar on large and smaller devices.
public RemoteViews tickerView The view to show as the ticker in the status bar when the notification is posted.
public long[] vibrate The pattern with which to vibrate.
public long when The timestamp for the notification.
Public Constructors
Notification()
Constructs a Notification object with everything set to 0.
Notification(int icon, CharSequence tickerText, long when)
This constructor is deprecated. Use Notification.Builder instead.
Notification(Parcel parcel)
Unflatten the notification from a parcel.
Public Methods
Notification clone()
Creates and returns a copy of this Object.
int describeContents()
Describe the kinds of special objects contained in this Parcelable's marshalled representation.
void setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)
This method is deprecated. Use Notification.Builder instead.
String toString()
Returns a string containing a concise, human-readable description of this object.
void writeToParcel(Parcel parcel, int flags)
Flatten this notification from a parcel.
[Expand]
Inherited Methods
From class java.lang.Object
From interface android.os.Parcelable

Constants

public static final int DEFAULT_ALL

Since: API Level 1

Use all default values (where applicable).

Constant Value: -1 (0xffffffff)

public static final int DEFAULT_LIGHTS

Since: API Level 1

Use the default notification lights. This will ignore the FLAG_SHOW_LIGHTS bit, and ledARGB, ledOffMS, or ledOnMS.

See Also
Constant Value: 4 (0x00000004)

public static final int DEFAULT_SOUND

Since: API Level 1

Use the default notification sound. This will ignore any given sound.

See Also
Constant Value: 1 (0x00000001)

public static final int DEFAULT_VIBRATE

Since: API Level 1

Use the default notification vibrate. This will ignore any given vibrate. Using phone vibration requires the VIBRATE permission.

See Also
Constant Value: 2 (0x00000002)

public static final int FLAG_AUTO_CANCEL

Since: API Level 1

Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user. On tablets, the

Constant Value: 16 (0x00000010)

public static final int FLAG_FOREGROUND_SERVICE

Since: API Level 5

Bit to be bitwise-ored into the flags field that should be set if this notification represents a currently running service. This will normally be set for you by startForeground(int, Notification).

Constant Value: 64 (0x00000040)

public static final int FLAG_HIGH_PRIORITY

Since: API Level 11

Bit to be bitwise-ored into the flags field that should be set if this notification represents a high-priority event that may be shown to the user even if notifications are otherwise unavailable (that is, when the status bar is hidden). This flag is ideally used in conjunction with fullScreenIntent.

Constant Value: 128 (0x00000080)

public static final int FLAG_INSISTENT

Since: API Level 1

Bit to be bitwise-ored into the flags field that if set, the audio will be repeated until the notification is cancelled or the notification window is opened.

Constant Value: 4 (0x00000004)

public static final int FLAG_NO_CLEAR

Since: API Level 1

Bit to be bitwise-ored into the flags field that should be set if the notification should not be canceled when the user clicks the Clear all button.

Constant Value: 32 (0x00000020)

public static final int FLAG_ONGOING_EVENT

Since: API Level 1

Bit to be bitwise-ored into the flags field that should be set if this notification is in reference to something that is ongoing, like a phone call. It should not be set if this notification is in reference to something that happened at a particular point in time, like a missed phone call.

Constant Value: 2 (0x00000002)

public static final int FLAG_ONLY_ALERT_ONCE

Since: API Level 1

Bit to be bitwise-ored into the flags field that should be set if you want the sound and/or vibration play each time the notification is sent, even if it has not been canceled before that.

Constant Value: 8 (0x00000008)

public static final int FLAG_SHOW_LIGHTS

Since: API Level 1

Bit to be bitwise-ored into the flags field that should be set if you want the LED on for this notification.

  • To turn the LED off, pass 0 in the alpha channel for colorARGB or 0 for both ledOnMS and ledOffMS.
  • To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.
  • To flash the LED, pass the number of milliseconds that it should be on and off to ledOnMS and ledOffMS.

Since hardware varies, you are not guaranteed that any of the values you pass are honored exactly. Use the system defaults (TODO) if possible because they will be set to values that work on any given hardware.

The alpha channel must be set for forward compatibility.

Constant Value: 1 (0x00000001)

public static final int STREAM_DEFAULT

Since: API Level 1

Use this constant as the value for audioStreamType to request that the default stream type for notifications be used. Currently the default stream type is STREAM_RING.

Constant Value: -1 (0xffffffff)

Fields

public static final Creator<Notification> CREATOR

Since: API Level 1

Parcelable.Creator that instantiates Notification objects

public int audioStreamType

Since: API Level 1

The audio stream type to use when playing the sound. Should be one of the STREAM_ constants from AudioManager.

public PendingIntent contentIntent

Since: API Level 1

The intent to execute when the expanded status entry is clicked. If this is an activity, it must include the FLAG_ACTIVITY_NEW_TASK flag, which requires that you take care of task management as described in the Tasks and Back Stack document. In particular, make sure to read the notification section Handling Notifications for the correct ways to launch an application from a notification.

public RemoteViews contentView

Since: API Level 1

The view that will represent this notification in the expanded status bar.

public int defaults

Since: API Level 1

Specifies which values should be taken from the defaults.

To set, OR the desired from DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS. For all default values, use DEFAULT_ALL.

public PendingIntent deleteIntent

Since: API Level 1

The intent to execute when the status entry is deleted by the user with the "Clear All Notifications" button. This probably shouldn't be launching an activity since several of those will be sent at the same time.

public int flags

Since: API Level 1

public PendingIntent fullScreenIntent

Since: API Level 9

An intent to launch instead of posting the notification to the status bar.

public int icon

Since: API Level 1

The resource id of a drawable to use as the icon in the status bar. This is required; notifications with an invalid icon resource will not be shown.

public int iconLevel

Since: API Level 1

If the icon in the status bar is to have more than one level, you can set this. Otherwise, leave it at its default value of 0.

public Bitmap largeIcon

Since: API Level 11

The bitmap that may escape the bounds of the panel and bar.

public int ledARGB

Since: API Level 1

The color of the led. The hardware will do its best approximation.

public int ledOffMS

Since: API Level 1

The number of milliseconds for the LED to be off while it's flashing. The hardware will do its best approximation.

public int ledOnMS

Since: API Level 1

The number of milliseconds for the LED to be on while it's flashing. The hardware will do its best approximation.

public int number

Since: API Level 1

The number of events that this notification represents. For example, in a new mail notification, this could be the number of unread messages. This number is superimposed over the icon in the status bar. If the number is 0 or negative, it is not shown in the status bar.

public Uri sound

Since: API Level 1

The sound to play.

To play the default notification sound, see defaults.

public CharSequence tickerText

Since: API Level 1

Text to scroll across the screen when this item is added to the status bar on large and smaller devices.

This field is provided separately from the other ticker fields both for compatibility and to allow an application to choose different text for when the text scrolls in and when it is displayed all at once in conjunction with one or more icons.

See Also

public RemoteViews tickerView

Since: API Level 11

The view to show as the ticker in the status bar when the notification is posted.

public long[] vibrate

Since: API Level 1

The pattern with which to vibrate.

To vibrate the default pattern, see defaults.

public long when

Since: API Level 1

The timestamp for the notification. The icons and expanded views are sorted by this key.

Public Constructors

public Notification ()

Since: API Level 1

Constructs a Notification object with everything set to 0. You might want to consider using Notification.Builder instead.

public Notification (int icon, CharSequence tickerText, long when)

Since: API Level 1

This constructor is deprecated.
Use Notification.Builder instead.

Constructs a Notification object with the information needed to have a status bar icon without the standard expanded view.

Parameters
icon The resource id of the icon to put in the status bar.
tickerText The text that flows by in the status bar when the notification first activates.
when The time to show in the time field. In the System.currentTimeMillis timebase.

public Notification (Parcel parcel)

Since: API Level 1

Unflatten the notification from a parcel.

Public Methods

public Notification clone ()

Since: API Level 1

Creates and returns a copy of this Object. The default implementation returns a so-called "shallow" copy: It creates a new instance of the same class and then copies the field values (including object references) from this instance to the new instance. A "deep" copy, in contrast, would also recursively clone nested objects. A subclass that needs to implement this kind of cloning should call super.clone() to create the new instance and then create deep copies of the nested, mutable objects.

Returns
  • a copy of this object.

public int describeContents ()

Since: API Level 1

Describe the kinds of special objects contained in this Parcelable's marshalled representation.

Returns
  • a bitmask indicating the set of special object types marshalled by the Parcelable.

public void setLatestEventInfo (Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)

Since: API Level 1

This method is deprecated.
Use Notification.Builder instead.

Sets the contentView field to be a view with the standard "Latest Event" layout.

Uses the icon and when fields to set the icon and time fields in the view.

Parameters
context The context for your application / activity.
contentTitle The title that goes in the expanded entry.
contentText The text that goes in the expanded entry.
contentIntent The intent to launch when the user clicks the expanded notification. If this is an activity, it must include the FLAG_ACTIVITY_NEW_TASK flag, which requires that you take care of task management as described in the Tasks and Back Stack document.

public String toString ()

Since: API Level 1

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
  • a printable representation of this object.

public void writeToParcel (Parcel parcel, int flags)

Since: API Level 1

Flatten this notification from a parcel.

Parameters
parcel The Parcel in which the object should be written.
flags Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE.