NotificationChannel


public final class NotificationChannel
extends Object implements Parcelable

java.lang.Object
   ↳ android.app.NotificationChannel


A representation of settings that apply to a collection of similarly themed notifications.

Summary

Constants

String DEFAULT_CHANNEL_ID

The id of the default channel for an app.

String EDIT_CONVERSATION

Extra value for Settings#EXTRA_CHANNEL_FILTER_LIST.

String EDIT_IMPORTANCE

Extra value for Settings#EXTRA_CHANNEL_FILTER_LIST.

String EDIT_LAUNCHER

Extra value for Settings#EXTRA_CHANNEL_FILTER_LIST.

String EDIT_LOCKED_DEVICE

Extra value for Settings#EXTRA_CHANNEL_FILTER_LIST.

String EDIT_SOUND

Extra value for Settings#EXTRA_CHANNEL_FILTER_LIST.

String EDIT_VIBRATION

Extra value for Settings#EXTRA_CHANNEL_FILTER_LIST.

String EDIT_ZEN

Extra value for Settings#EXTRA_CHANNEL_FILTER_LIST.

Inherited constants

Fields

public static final Creator<NotificationChannel> CREATOR

Public constructors

NotificationChannel(String id, CharSequence name, int importance)

Creates a notification channel.

Public methods

boolean canBubble()

Returns whether notifications posted to this channel are allowed to display outside of the notification shade, in a floating window on top of other apps.

boolean canBypassDnd()

Whether or not notifications posted to this channel can bypass the Do Not Disturb NotificationManager#INTERRUPTION_FILTER_PRIORITY mode when the active policy allows priority channels to bypass notification filtering.

boolean canShowBadge()

Returns whether notifications posted to this channel can appear as badges in a Launcher application.

int describeContents()

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

void enableLights(boolean lights)

Sets whether notifications posted to this channel should display notification lights, on devices that support that feature.

void enableVibration(boolean vibration)

Sets whether notification posted to this channel should vibrate.

boolean equals(Object o)

Indicates whether some other object is "equal to" this one.

AudioAttributes getAudioAttributes()

Returns the audio attributes for sound played by notifications posted to this channel.

String getConversationId()

Returns the id of the conversation backing this channel, if it's associated with a conversation.

String getDescription()

Returns the user visible description of this channel.

String getGroup()

Returns what group this channel belongs to.

String getId()

Returns the id of this channel.

int getImportance()

Returns the user specified importance e.g. NotificationManager#IMPORTANCE_LOW for notifications posted to this channel.

int getLightColor()

Returns the notification light color for notifications posted to this channel.

int getLockscreenVisibility()

Returns whether or not notifications posted to this channel are shown on the lockscreen in full or redacted form.

CharSequence getName()

Returns the user visible name of this channel.

String getParentChannelId()

Returns the id of the parent notification channel to this channel, if it's a conversation related channel.

Uri getSound()

Returns the notification sound for this channel.

VibrationEffect getVibrationEffect()

Returns the VibrationEffect for notifications posted to this channel.

long[] getVibrationPattern()

Returns the vibration pattern for notifications posted to this channel.

boolean hasUserSetImportance()

Returns whether the user has chosen the importance of this channel, either to affirm the initial selection from the app, or changed it to be higher or lower.

boolean hasUserSetSound()

Returns whether the user has chosen the sound of this channel.

int hashCode()

Returns a hash code value for the object.

boolean isBlockable()

Returns whether this channel is always blockable, even if the app is 'fixed' as non-blockable.

boolean isConversation()

Whether or not this channel represents a conversation.

boolean isDemoted()

Returns whether the user has decided that this channel does not represent a conversation.

boolean isImportantConversation()

Whether or not notifications in this conversation are considered important.

void setAllowBubbles(boolean allowBubbles)

As of Android 11 this value is no longer respected.

void setBlockable(boolean blockable)

Allows users to block notifications sent through this channel, if this channel belongs to a package that otherwise would have notifications "fixed" as enabled.

void setBypassDnd(boolean bypassDnd)

Sets whether or not notifications posted to this channel can interrupt the user in NotificationManager.INTERRUPTION_FILTER_PRIORITY mode.

void setConversationId(String parentChannelId, String conversationId)

Sets this channel as being converastion-centric.

void setDescription(String description)

Sets the user visible description of this channel.

void setGroup(String groupId)

Sets what group this channel belongs to.

void setImportance(int importance)

Sets the level of interruption of this notification channel.

void setLightColor(int argb)

Sets the notification light color for notifications posted to this channel, if lights are enabled on this channel and the device supports that feature.

void setLockscreenVisibility(int lockscreenVisibility)

Sets whether notifications posted to this channel appear on the lockscreen or not, and if so, whether they appear in a redacted form.

void setName(CharSequence name)

Sets the user visible name of this channel.

void setShowBadge(boolean showBadge)

Sets whether notifications posted to this channel can appear as application icon badges in a Launcher.

void setSound(Uri sound, AudioAttributes audioAttributes)

Sets the sound that should be played for notifications posted to this channel and its audio attributes.

void setVibrationEffect(VibrationEffect effect)

Sets a VibrationEffect for notifications posted to this channel.

void setVibrationPattern(long[] vibrationPattern)

Sets the vibration pattern for notifications posted to this channel.

boolean shouldShowLights()

Returns whether notifications posted to this channel trigger notification lights.

boolean shouldVibrate()

Returns whether notifications posted to this channel always vibrate.

String toString()

Returns a string representation of the object.

void writeToParcel(Parcel dest, int flags)

Flatten this object in to a Parcel.

Inherited methods

Constants

DEFAULT_CHANNEL_ID

Added in API level 26
public static final String DEFAULT_CHANNEL_ID

The id of the default channel for an app. This id is reserved by the system. All notifications posted from apps targeting Build.VERSION_CODES.N_MR1 or earlier without a notification channel specified are posted to this channel.

Constant Value: "miscellaneous"

EDIT_CONVERSATION

Added in API level 31
public static final String EDIT_CONVERSATION

Extra value for Settings#EXTRA_CHANNEL_FILTER_LIST. Include to show fields that have to do with editing conversation settings (demoting or restoring a channel to be a Conversation, changing bubble behavior, or setting the priority of a conversation).

Constant Value: "conversation"

EDIT_IMPORTANCE

Added in API level 31
public static final String EDIT_IMPORTANCE

Extra value for Settings#EXTRA_CHANNEL_FILTER_LIST. Include to show fields that have to do with editing importance (setImportance(int)) and/or conversation priority.

Constant Value: "importance"

EDIT_LAUNCHER

Added in API level 31
public static final String EDIT_LAUNCHER

Extra value for Settings#EXTRA_CHANNEL_FILTER_LIST. Include to show fields that have to do with editing launcher behavior (showing badges)}.

Constant Value: "launcher"

EDIT_LOCKED_DEVICE

Added in API level 31
public static final String EDIT_LOCKED_DEVICE

Extra value for Settings#EXTRA_CHANNEL_FILTER_LIST. Include to show fields that have to do with editing behavior on devices that are locked or have a turned off display (setLockscreenVisibility(int), enableLights(boolean), setLightColor(int)).

Constant Value: "locked"

EDIT_SOUND

Added in API level 31
public static final String EDIT_SOUND

Extra value for Settings#EXTRA_CHANNEL_FILTER_LIST. Include to show fields that have to do with editing sound, like a tone picker (setSound(android.net.Uri, android.media.AudioAttributes)).

Constant Value: "sound"

EDIT_VIBRATION

Added in API level 31
public static final String EDIT_VIBRATION

Extra value for Settings#EXTRA_CHANNEL_FILTER_LIST. Include to show fields that have to do with editing vibration (enableVibration(boolean), setVibrationPattern(long[])).

Constant Value: "vibration"

EDIT_ZEN

Added in API level 31
public static final String EDIT_ZEN

Extra value for Settings#EXTRA_CHANNEL_FILTER_LIST. Include to show fields that have to do with editing do not disturb bypass {(@link #setBypassDnd(boolean)}) .

Constant Value: "zen"

Fields

CREATOR

Added in API level 26
public static final Creator<NotificationChannel> CREATOR

Public constructors

NotificationChannel

Added in API level 26
public NotificationChannel (String id, 
                CharSequence name, 
                int importance)

Creates a notification channel.

Parameters
id String: The id of the channel. Must be unique per package. The value may be truncated if it is too long.

name CharSequence: The user visible name of the channel. You can rename this channel when the system locale changes by listening for the Intent#ACTION_LOCALE_CHANGED broadcast. The recommended maximum length is 40 characters; the value may be truncated if it is too long.

importance int: The importance of the channel. This controls how interruptive notifications posted to this channel are. Value is NotificationManager.IMPORTANCE_UNSPECIFIED, NotificationManager.IMPORTANCE_NONE, NotificationManager.IMPORTANCE_MIN, NotificationManager.IMPORTANCE_LOW, NotificationManager.IMPORTANCE_DEFAULT, or NotificationManager.IMPORTANCE_HIGH

Public methods

canBubble

Added in API level 29
public boolean canBubble ()

Returns whether notifications posted to this channel are allowed to display outside of the notification shade, in a floating window on top of other apps.

Returns
boolean

canBypassDnd

Added in API level 26
public boolean canBypassDnd ()

Whether or not notifications posted to this channel can bypass the Do Not Disturb NotificationManager#INTERRUPTION_FILTER_PRIORITY mode when the active policy allows priority channels to bypass notification filtering.

Returns
boolean

canShowBadge

Added in API level 26
public boolean canShowBadge ()

Returns whether notifications posted to this channel can appear as badges in a Launcher application. Note that badging may be disabled for other reasons.

Returns
boolean

describeContents

Added in API level 26
public int describeContents ()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of writeToParcel(android.os.Parcel, int), the return value of this method must include the CONTENTS_FILE_DESCRIPTOR bit.

Returns
int a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or CONTENTS_FILE_DESCRIPTOR

enableLights

Added in API level 26
public void enableLights (boolean lights)

Sets whether notifications posted to this channel should display notification lights, on devices that support that feature. Only modifiable before the channel is submitted to NotificationManager#createNotificationChannel(NotificationChannel).

Parameters
lights boolean

enableVibration

Added in API level 26
public void enableVibration (boolean vibration)

Sets whether notification posted to this channel should vibrate. The vibration pattern can be set with setVibrationPattern(long[]). Only modifiable before the channel is submitted to NotificationManager#createNotificationChannel(NotificationChannel).

Parameters
vibration boolean

equals

Added in API level 26
public boolean equals (Object o)

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o Object: This value may be null.

Returns
boolean true if this object is the same as the obj argument; false otherwise.

getAudioAttributes

Added in API level 26
public AudioAttributes getAudioAttributes ()

Returns the audio attributes for sound played by notifications posted to this channel.

Returns
AudioAttributes

getConversationId

Added in API level 30
public String getConversationId ()

Returns the id of the conversation backing this channel, if it's associated with a conversation. See setConversationId(java.lang.String, java.lang.String).

Returns
String This value may be null.

getDescription

Added in API level 26
public String getDescription ()

Returns the user visible description of this channel.

Returns
String

getGroup

Added in API level 26
public String getGroup ()

Returns what group this channel belongs to. This is used only for visually grouping channels in the UI.

Returns
String

getId

Added in API level 26
public String getId ()

Returns the id of this channel.

Returns
String

getImportance

Added in API level 26
public int getImportance ()

Returns the user specified importance e.g. NotificationManager#IMPORTANCE_LOW for notifications posted to this channel. Note: This value might be > NotificationManager#IMPORTANCE_NONE, but notifications posted to this channel will not be shown to the user if the parent NotificationChannelGroup or app is blocked. See NotificationChannelGroup#isBlocked() and NotificationManager#areNotificationsEnabled().

Returns
int

getLightColor

Added in API level 26
public int getLightColor ()

Returns the notification light color for notifications posted to this channel. Irrelevant unless shouldShowLights().

Returns
int

getLockscreenVisibility

Added in API level 26
public int getLockscreenVisibility ()

Returns whether or not notifications posted to this channel are shown on the lockscreen in full or redacted form.

Returns
int

getName

Added in API level 26
public CharSequence getName ()

Returns the user visible name of this channel.

Returns
CharSequence

getParentChannelId

Added in API level 30
public String getParentChannelId ()

Returns the id of the parent notification channel to this channel, if it's a conversation related channel. See setConversationId(java.lang.String, java.lang.String).

Returns
String This value may be null.

getSound

Added in API level 26
public Uri getSound ()

Returns the notification sound for this channel.

Returns
Uri

getVibrationEffect

Added in API level 35
public VibrationEffect getVibrationEffect ()

Returns the VibrationEffect for notifications posted to this channel. The returned effect is derived from either the effect provided in the setVibrationEffect(android.os.VibrationEffect) method, or the equivalent vibration effect of the pattern set via the setVibrationPattern(long[]) method, based on setter method that was called last. The returned effect will be ignored in one of the following cases:

  • vibration is not enabled for the channel (i.e. shouldVibrate() returns false).
  • the effect is not supported/playable by the device. In this case, if vibration is enabled for the channel, the default channel vibration will be used instead.

Returns
VibrationEffect the VibrationEffect set via setVibrationEffect(android.os.VibrationEffect), or the equivalent of the vibration set via setVibrationPattern(long[]). This value may be null.

getVibrationPattern

Added in API level 26
public long[] getVibrationPattern ()

Returns the vibration pattern for notifications posted to this channel. Will be ignored if vibration is not enabled (shouldVibrate()).

Returns
long[]

hasUserSetImportance

Added in API level 29
public boolean hasUserSetImportance ()

Returns whether the user has chosen the importance of this channel, either to affirm the initial selection from the app, or changed it to be higher or lower.

Returns
boolean

See also:

hasUserSetSound

Added in API level 30
public boolean hasUserSetSound ()

Returns whether the user has chosen the sound of this channel.

Returns
boolean

See also:

hashCode

Added in API level 26
public int hashCode ()

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

Returns
int a hash code value for this object.

isBlockable

Added in API level 33
public boolean isBlockable ()

Returns whether this channel is always blockable, even if the app is 'fixed' as non-blockable.

Returns
boolean

isConversation

Added in API level 31
public boolean isConversation ()

Whether or not this channel represents a conversation.

Returns
boolean

isDemoted

Added in API level 31
public boolean isDemoted ()

Returns whether the user has decided that this channel does not represent a conversation. The value will always be false for channels that never claimed to be conversations - that is, for channels where getConversationId() and getParentChannelId() are empty.

Returns
boolean

isImportantConversation

Added in API level 30
public boolean isImportantConversation ()

Whether or not notifications in this conversation are considered important.

Important conversations may get special visual treatment, and might be able to bypass DND.

This is only valid for channels that represent conversations, that is, where isConversation() is true.

Returns
boolean

setAllowBubbles

Added in API level 29
public void setAllowBubbles (boolean allowBubbles)

As of Android 11 this value is no longer respected.

Parameters
allowBubbles boolean

setBlockable

Added in API level 33
public void setBlockable (boolean blockable)

Allows users to block notifications sent through this channel, if this channel belongs to a package that otherwise would have notifications "fixed" as enabled. If the channel does not belong to a package that has a fixed notification permission, this method does nothing, since such channels are blockable by default and cannot be set to be unblockable.

Parameters
blockable boolean: if true, allows users to block notifications on this channel.

setBypassDnd

Added in API level 26
public void setBypassDnd (boolean bypassDnd)

Sets whether or not notifications posted to this channel can interrupt the user in NotificationManager.INTERRUPTION_FILTER_PRIORITY mode. Only modifiable by the system and notification ranker.

Parameters
bypassDnd boolean

setConversationId

Added in API level 30
public void setConversationId (String parentChannelId, 
                String conversationId)

Sets this channel as being converastion-centric. Different settings and functionality may be exposed for conversation-centric channels.

Parameters
parentChannelId String: The getId() id} of the generic channel that notifications of this type would be posted to in absence of a specific conversation id. For example, if this channel represents 'Messages from Person A', the parent channel would be 'Messages.' This value cannot be null.

conversationId String: The ShortcutInfo#getId() of the shortcut representing this channel's conversation. This value cannot be null.

setDescription

Added in API level 26
public void setDescription (String description)

Sets the user visible description of this channel.

The recommended maximum length is 300 characters; the value may be truncated if it is too long.

Parameters
description String

setGroup

Added in API level 26
public void setGroup (String groupId)

Sets what group this channel belongs to. Group information is only used for presentation, not for behavior. Only modifiable before the channel is submitted to NotificationManager#createNotificationChannel(NotificationChannel), unless the channel is not currently part of a group.

Parameters
groupId String: the id of a group created by NotificationManager#createNotificationChannelGroup(NotificationChannelGroup).

setImportance

Added in API level 26
public void setImportance (int importance)

Sets the level of interruption of this notification channel. Only modifiable before the channel is submitted to NotificationManager#createNotificationChannel(NotificationChannel).

Parameters
importance int: the amount the user should be interrupted by notifications from this channel. Value is NotificationManager.IMPORTANCE_UNSPECIFIED, NotificationManager.IMPORTANCE_NONE, NotificationManager.IMPORTANCE_MIN, NotificationManager.IMPORTANCE_LOW, NotificationManager.IMPORTANCE_DEFAULT, or NotificationManager.IMPORTANCE_HIGH

setLightColor

Added in API level 26
public void setLightColor (int argb)

Sets the notification light color for notifications posted to this channel, if lights are enabled on this channel and the device supports that feature. Only modifiable before the channel is submitted to NotificationManager#createNotificationChannel(NotificationChannel).

Parameters
argb int

setLockscreenVisibility

Added in API level 26
public void setLockscreenVisibility (int lockscreenVisibility)

Sets whether notifications posted to this channel appear on the lockscreen or not, and if so, whether they appear in a redacted form. See e.g. Notification#VISIBILITY_SECRET. Only modifiable by the system and notification ranker.

Parameters
lockscreenVisibility int

setName

Added in API level 26
public void setName (CharSequence name)

Sets the user visible name of this channel.

The recommended maximum length is 40 characters; the value may be truncated if it is too long.

Parameters
name CharSequence

setShowBadge

Added in API level 26
public void setShowBadge (boolean showBadge)

Sets whether notifications posted to this channel can appear as application icon badges in a Launcher. Only modifiable before the channel is submitted to NotificationManager#createNotificationChannel(NotificationChannel).

Parameters
showBadge boolean: true if badges should be allowed to be shown.

setSound

Added in API level 26
public void setSound (Uri sound, 
                AudioAttributes audioAttributes)

Sets the sound that should be played for notifications posted to this channel and its audio attributes. Notification channels with an importance of at least NotificationManager#IMPORTANCE_DEFAULT should have a sound. Note: An app-specific sound can be provided in the Uri parameter, but because channels are persistent for the duration of the app install, and are backed up and restored, the Uri should be stable. For this reason it is not recommended to use a ContentResolver#SCHEME_ANDROID_RESOURCE uri, as resource ids can change on app upgrade. Only modifiable before the channel is submitted to NotificationManager#createNotificationChannel(NotificationChannel).

Parameters
sound Uri

audioAttributes AudioAttributes

setVibrationEffect

Added in API level 35
public void setVibrationEffect (VibrationEffect effect)

Sets a VibrationEffect for notifications posted to this channel. If the provided effect is non-null, will enable vibration on this channel (equivalent to calling enableVibration(boolean) with true). Otherwise vibration will be disabled unless enableVibration(boolean) is used with true, in which case the default vibration will be used.

The effect passed here will be returned from getVibrationEffect(). If the provided VibrationEffect is an equivalent to a wave-form vibration pattern, the equivalent wave-form pattern will be returned from getVibrationPattern().

Note that some VibrationEffects may not be playable on some devices. In cases where such an effect is passed here, vibration will still be enabled for the channel, but the default vibration will be used. Nonetheless, the provided effect will be stored and be returned from getVibrationEffect() calls, and could be used by the same channel on a different device, for example, in cases the user backs up and restores to a device that does have the ability to play the effect, where that effect will be used instead of the default. To avoid such issues that could make the vibration behavior of your notification channel differ among different devices, it's recommended that you avoid vibration effect primitives, as the support for them differs widely among devices (read VibrationEffect.Composition for more on vibration primitives).

Only modifiable before the channel is submitted to NotificationManager#createNotificationChannel(NotificationChannel).

Parameters
effect VibrationEffect: This value may be null.

setVibrationPattern

Added in API level 26
public void setVibrationPattern (long[] vibrationPattern)

Sets the vibration pattern for notifications posted to this channel. If the provided pattern is valid (non-null, non-empty with at least 1 non-zero value), will enable vibration on this channel (equivalent to calling enableVibration(boolean) with true). Otherwise, vibration will be disabled unless enableVibration(boolean) is used with true, in which case the default vibration will be used. Only modifiable before the channel is submitted to NotificationManager#createNotificationChannel(NotificationChannel).

Parameters
vibrationPattern long

shouldShowLights

Added in API level 26
public boolean shouldShowLights ()

Returns whether notifications posted to this channel trigger notification lights.

Returns
boolean

shouldVibrate

Added in API level 26
public boolean shouldVibrate ()

Returns whether notifications posted to this channel always vibrate.

Returns
boolean

toString

Added in API level 26
public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.

writeToParcel

Added in API level 26
public void writeToParcel (Parcel dest, 
                int flags)

Flatten this object in to a Parcel.

Parameters
dest Parcel: The Parcel in which the object should be written. This value cannot be null.

flags int: Additional flags about how the object should be written. May be 0 or Parcelable.PARCELABLE_WRITE_RETURN_VALUE. Value is either 0 or a combination of Parcelable.PARCELABLE_WRITE_RETURN_VALUE, and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES