Notification.MessagingStyle

public static class Notification.MessagingStyle
extends Notification.Style

java.lang.Object
   ↳ android.app.Notification.Style
     ↳ android.app.Notification.MessagingStyle


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 P and above, it is required to use the 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).

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 noti = new Notification.Builder()
     .setContentTitle("2 new messages with " + sender.toString())
     .setContentText(subject)
     .setSmallIcon(R.drawable.new_message)
     .setLargeIcon(aBitmap)
     .setStyle(style)
     .build();
 

Summary

Nested classes

class Notification.MessagingStyle.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

Public constructors

Notification.MessagingStyle(CharSequence userDisplayName)

This constructor was deprecated in API level 28. use MessagingStyle(Person)

Notification.MessagingStyle(Person user)

Public methods

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

Adds a Notification.MessagingStyle.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 Notification.MessagingStyle.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, otherwise false.

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

Constants

MAXIMUM_RETAINED_MESSAGES

added in API level 24
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

Notification.MessagingStyle

added in API level 24
public Notification.MessagingStyle (CharSequence userDisplayName)

This constructor was deprecated in API level 28.
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 in addMessage(Notification.MessagingStyle.Message)

This value must never be null.

Notification.MessagingStyle

added in API level 28
public Notification.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 will be enforced starting in Android P.

This value must never be null.

Public methods

addHistoricMessage

added in API level 26
public Notification.MessagingStyle addHistoricMessage (Notification.MessagingStyle.Message message)

Adds a 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.

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

Parameters
message Notification.MessagingStyle.Message: The historic Notification.MessagingStyle.Message to be added

Returns
Notification.MessagingStyle this object for method chaining

addMessage

added in API level 24
public Notification.MessagingStyle addMessage (CharSequence text, 
                long timestamp, 
                CharSequence sender)

This method was deprecated in API level 28.
use addMessage(CharSequence, long, Person)

Adds a message for display by this notification. Convenience call for a simple Notification.MessagingStyle.Message in addMessage(Notification.MessagingStyle.Message).

Parameters
text CharSequence: A CharSequence to be displayed as the message content

timestamp long: Time at which the message arrived

sender CharSequence: A CharSequence to be used for displaying the name of the sender. Should be null for messages by the current user, in which case the platform will insert getUserDisplayName(). Should be unique amongst all individuals in the conversation, and should be consistent during re-posts of the notification.

Returns
Notification.MessagingStyle this object for method chaining

addMessage

added in API level 24
public Notification.MessagingStyle addMessage (Notification.MessagingStyle.Message message)

Adds a Notification.MessagingStyle.Message for display in this notification.

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

Parameters
message Notification.MessagingStyle.Message: The Notification.MessagingStyle.Message to be displayed

Returns
Notification.MessagingStyle this object for method chaining

addMessage

added in API level 28
public Notification.MessagingStyle addMessage (CharSequence text, 
                long timestamp, 
                Person sender)

Adds a message for display by this notification. Convenience call for a simple Notification.MessagingStyle.Message in addMessage(Notification.MessagingStyle.Message).

Parameters
text CharSequence: A CharSequence to be displayed as the message content

This value must never be null.

timestamp long: Time at which the message arrived

sender Person: The Person who sent the message. Should be null for messages by the current user, in which case the platform will insert the user set in MessagingStyle(Person).

Returns
Notification.MessagingStyle this object for method chaining

getConversationTitle

added in API level 24
public CharSequence getConversationTitle ()

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

Returns
CharSequence

This value may be null.

getHistoricMessages

added in API level 26
public List<Notification.MessagingStyle.Message> getHistoricMessages ()

Gets the list of historic Messages in the notification.

Returns
List<Notification.MessagingStyle.Message>

getMessages

added in API level 24
public List<Notification.MessagingStyle.Message> getMessages ()

Gets the list of Message objects that represent the notification

Returns
List<Notification.MessagingStyle.Message>

getUser

added in API level 28
public Person getUser ()

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

This value will never be null.

getUserDisplayName

added in API level 24
public CharSequence getUserDisplayName ()

This method was deprecated in API level 28.
use getUser() instead

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

Returns
CharSequence

isGroupConversation

added in API level 28
public boolean isGroupConversation ()

Returns true if this notification represents a group conversation, otherwise false.

If the application that generated this Notification.MessagingStyle targets an SDK version less than Build.VERSION_CODES.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 forward, this method returns what's set by setGroupConversation(boolean) allowing for named, non-group conversations.

Returns
boolean

setConversationTitle

added in API level 24
public Notification.MessagingStyle setConversationTitle (CharSequence conversationTitle)

Sets the title to be displayed on this conversation. May be set to null.

This API's behavior was changed in SDK version Build.VERSION_CODES.P. If your application's target version is less than Build.VERSION_CODES.P, setting a conversation title to a non-null value will make isGroupConversation() return true and passing null will make it return false. In Build.VERSION_CODES.P and beyond, use setGroupConversation(boolean) to set group conversation status.

Parameters
conversationTitle CharSequence: Title displayed for this conversation

This value may be null.

Returns
Notification.MessagingStyle this object for method chaining

setGroupConversation

added in API level 28
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), 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