Notification.MessagingStyle  |  API reference  |  Android Developers (original) (raw)


public static class Notification.MessagingStyle
` extends [Notification.Style](/reference/android/app/Notification.Style) `


Helper class for generating large-format notifications that include multiple back-and-forth messages of varying types between any number of people.

If the platform does not provide large-format notifications, this method has no effect. The user will always see the normal notification view.

If the app is targeting Android [Build.VERSION_CODES.P](/reference/android/os/Build.VERSION%5FCODES#P) and above, it is required to use the [Person](/reference/android/app/Person) class in order to get an optimal rendering of the notification and its avatars. For conversations involving multiple people, the app should also make sure that it marks the conversation as a group with[setGroupConversation(boolean)](/reference/android/app/Notification.MessagingStyle#setGroupConversation%28boolean%29).

From Android [Build.VERSION_CODES.UPSIDE_DOWN_CAKE](/reference/android/os/Build.VERSION%5FCODES#UPSIDE%5FDOWN%5FCAKE), messaging style notifications that are associated with a valid conversation shortcut (via [Notification.Builder.setShortcutId(String)](/reference/android/app/Notification.Builder#setShortcutId%28java.lang.String%29)) are displayed in a dedicated conversation section in the shade above non-conversation alerting and silence notifications. To be a valid conversation shortcut, the shortcut must be a[ShortcutInfo.Builder.setLongLived(boolean)](/reference/android/content/pm/ShortcutInfo.Builder#setLongLived%28boolean%29) dynamic or cached sharing shortcut.

This class is a "rebuilder": It attaches to a Builder object and modifies its behavior. Here's an example of how this may be used:

Person user = new Person.Builder().setIcon(userIcon).setName(userName).build(); MessagingStyle style = new MessagingStyle(user) .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson()) .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson()) .setGroupConversation(hasMultiplePeople());

Notification notif = new Notification.Builder() .setContentTitle("2 new messages with " + sender.toString()) .setContentText(subject) .setSmallIcon(R.drawable.new_message) .setStyle(style) .build();

Note that this style doesn't display the large icon set via[Builder.setLargeIcon(Icon)](/reference/android/app/Notification.Builder#setLargeIcon%28android.graphics.drawable.Icon%29), except as a fallback group icon if the shortcut doesn't include one. It can, however, display images set via [MessagingStyle.Message.setData](/reference/android/app/Notification.MessagingStyle.Message#setData%28java.lang.String,%20android.net.Uri%29).

Summary

Nested classes
class Notification.MessagingStyle.Message An object representing a simple message or piece of media within a mixed-media message.
Constants
int MAXIMUM_RETAINED_MESSAGES The maximum number of messages that will be retained in the Notification itself (the number displayed is up to the platform).
Inherited fields
From classandroid.app.Notification.Style protectedNotification.Builder mBuilder
Public constructors
MessagingStyle(Person user)
MessagingStyle(CharSequence userDisplayName) This constructor is deprecated. use MessagingStyle(Person)
Public methods
Notification.MessagingStyle addHistoricMessage(Notification.MessagingStyle.Message message) Adds a Message for historic context in this notification.
Notification.MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) This method was deprecated in API level 28. use addMessage(CharSequence,long,Person)
Notification.MessagingStyle addMessage(Notification.MessagingStyle.Message message) Adds a Message for display in this notification.
Notification.MessagingStyle addMessage(CharSequence text, long timestamp, Person sender) Adds a message for display by this notification.
CharSequence getConversationTitle() Return the title to be displayed on this conversation.
List<Notification.MessagingStyle.Message> getHistoricMessages() Gets the list of historic Messages in the notification.
List<Notification.MessagingStyle.Message> getMessages() Gets the list of Message objects that represent the notification.
Person getUser()
CharSequence getUserDisplayName() This method was deprecated in API level 28. use getUser() instead
boolean isGroupConversation() Returns true if this notification represents a group conversation, otherwisefalse.
Notification.MessagingStyle setConversationTitle(CharSequence conversationTitle) Sets the title to be displayed on this conversation.
Notification.MessagingStyle setGroupConversation(boolean isGroupConversation) Sets whether this conversation notification represents a group.
Inherited methods
From class android.app.Notification.Style Notification build() Calls Notification.Builder.build() on the Builder this Style is attached to. void checkBuilder() RemoteViews getStandardView(int layoutId) void internalSetBigContentTitle(CharSequence title) Overrides ContentTitle in the expanded form of the template. void internalSetSummaryText(CharSequence cs) Set the first line of text after the detail section in the expanded form of the template. void setBuilder(Notification.Builder builder)
From class java.lang.Object Object clone() Creates and returns a copy of this object. boolean equals(Object obj) Indicates whether some other object is "equal to" this one. void finalize() Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. finalClass<?> getClass() Returns the runtime class of this Object. int hashCode() Returns a hash code value for the object. final void notify() Wakes up a single thread that is waiting on this object's monitor. final void notifyAll() Wakes up all threads that are waiting on this object's monitor. String toString() Returns a string representation of the object. final void wait(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. final void wait(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. final void wait() Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Constants

MAXIMUM_RETAINED_MESSAGES

public static final int MAXIMUM_RETAINED_MESSAGES

The maximum number of messages that will be retained in the Notification itself (the number displayed is up to the platform).

Constant Value: 25 (0x00000019)

Public constructors

MessagingStyle

public MessagingStyle (Person user)

Parameters
user Person: Required - The person displayed for any messages that are sent by the user. Any messages added with addMessage(Notification.MessagingStyle.Message) who don't have a Person associated with it will be displayed as if they were sent by this user. The user also needs to have a valid name associated with it, which is enforced starting in Android Build.VERSION_CODES.P. This value cannot be null.

MessagingStyle

public MessagingStyle (CharSequence userDisplayName)

This constructor is deprecated.
use MessagingStyle(Person)

Parameters
userDisplayName CharSequence: Required - the name to be displayed for any replies sent by the user before the posting app reposts the notification with those messages after they've been actually sent and in previous messages sent by the user added inaddMessage(Notification.MessagingStyle.Message) This value cannot be null.

Public methods

addHistoricMessage

public Notification.MessagingStyle addHistoricMessage (Notification.MessagingStyle.Message message)

Adds a [Message](/reference/android/app/Notification.MessagingStyle.Message) for historic context in this notification.

Messages should be added as historic if they are not the main subject of the notification but may give context to a conversation. The system may choose to present them only when relevant, e.g. when replying to a message through a [RemoteInput](/reference/android/app/RemoteInput).

The messages should be added in chronologic order, i.e. the oldest first, the newest last.

Parameters
message Notification.MessagingStyle.Message: The historic Message to be added
Returns
Notification.MessagingStyle this object for method chaining

addMessage

public Notification.MessagingStyle addMessage (Notification.MessagingStyle.Message message)

Adds a [Message](/reference/android/app/Notification.MessagingStyle.Message) for display in this notification.

The messages should be added in chronologic order, i.e. the oldest first, the newest last.

Multiple Messages in a row with the same timestamp and sender may be grouped as a single message. This means an app should represent a message that has both an image and text as two Message objects, one with the image (and fallback text), and the other with the message text. For consistency, a text message (if any) should be provided after Uri content.

Parameters
message Notification.MessagingStyle.Message: The Message to be displayed
Returns
Notification.MessagingStyle this object for method chaining

getConversationTitle

public CharSequence getConversationTitle ()

Return the title to be displayed on this conversation. May return null.

Returns
CharSequence

getUser

public Person getUser ()

Returns
Person the user to be displayed for any replies sent by the user. This value cannot be null.

getUserDisplayName

public CharSequence getUserDisplayName ()

This method was deprecated in API level 28.
use [getUser()](/reference/android/app/Notification.MessagingStyle#getUser%28%29) instead

Returns the name to be displayed for any replies sent by the user

Returns
CharSequence

isGroupConversation

public boolean isGroupConversation ()

Returns true if this notification represents a group conversation, otherwisefalse.

If the application that generated this [MessagingStyle](/reference/android/app/Notification.MessagingStyle) targets an SDK version less than [Build.VERSION_CODES.P](/reference/android/os/Build.VERSION%5FCODES#P), this method becomes dependent on whether or not the conversation title is set; returning true if the conversation title is a non-null value, or false otherwise. From [Build.VERSION_CODES.P](/reference/android/os/Build.VERSION%5FCODES#P) forward, this method returns what's set by [setGroupConversation(boolean)](/reference/android/app/Notification.MessagingStyle#setGroupConversation%28boolean%29) allowing for named, non-group conversations.

Returns
boolean

setGroupConversation

public Notification.MessagingStyle setGroupConversation (boolean isGroupConversation)

Sets whether this conversation notification represents a group. If the app is targeting Android P, this is required if the app wants to display the largeIcon set with[Notification.Builder.setLargeIcon(Bitmap)](/reference/android/app/Notification.Builder#setLargeIcon%28android.graphics.Bitmap%29), otherwise it will be hidden.

Parameters
isGroupConversation boolean: true if the conversation represents a group,false otherwise.
Returns
Notification.MessagingStyle this object for method chaining