public class

NotificationCompat.MessagingStyle

extends NotificationCompat.Style

 java.lang.Object

androidx.core.app.NotificationCompat.Style

↳androidx.core.app.NotificationCompat.MessagingStyle

Overview

Helper class for generating large-format notifications that include multiple back-and-forth messages of varying types between any number of people.
In order to get a backwards compatible behavior, the app needs to use the v7 version of the notification builder together with this style, otherwise the user will see the normal notification view.
Use NotificationCompat.MessagingStyle.setConversationTitle(CharSequence) to set a conversation title for group chats with more than two people. This could be the user-created name of the group or, if it doesn't have a specific name, a list of the participants in the conversation. Do not set a conversation title for one-on-one chats, since platforms use the existence of this field as a hint that the conversation is a group.
This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like so:


 Notification notification = new Notification.Builder()
     .setContentTitle("2 new messages with " + sender.toString())
     .setContentText(subject)
     .setSmallIcon(R.drawable.new_message)
     .setLargeIcon(aBitmap)
     .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
         .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
         .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
     .build();
 

Summary

Fields
public static final intMAXIMUM_RETAINED_MESSAGES

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

from NotificationCompat.StylemBuilder
Constructors
publicMessagingStyle(java.lang.CharSequence userDisplayName)

publicMessagingStyle(Person user)

Creates a new NotificationCompat.MessagingStyle object.

Methods
public voidaddCompatExtras(Bundle extras)

This is called with the extras of the framework Notification during the NotificationCompat.Builder.build() process, after apply() has been called.

public NotificationCompat.MessagingStyleaddHistoricMessage(NotificationCompat.MessagingStyle.Message message)

Adds a NotificationCompat.MessagingStyle.Message for historic context in this notification.

public NotificationCompat.MessagingStyleaddMessage(java.lang.CharSequence text, long timestamp, java.lang.CharSequence sender)

Adds a message for display by this notification.

public NotificationCompat.MessagingStyleaddMessage(java.lang.CharSequence text, long timestamp, Person person)

Adds a message for display by this notification.

public NotificationCompat.MessagingStyleaddMessage(NotificationCompat.MessagingStyle.Message message)

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

public voidapply(NotificationBuilderWithBuilderAccessor builder)

protected voidclearCompatExtraKeys(Bundle extras)

public static NotificationCompat.MessagingStyleextractMessagingStyleFromNotification(Notification notification)

Retrieves a NotificationCompat.MessagingStyle from a Notification, enabling an application that has set a NotificationCompat.MessagingStyle using NotificationCompat or to send messaging information to another application using NotificationCompat, regardless of the API level of the system.

protected java.lang.StringgetClassName()

public java.lang.CharSequencegetConversationTitle()

Return the title to be displayed on this conversation.

public java.util.List<NotificationCompat.MessagingStyle.Message>getHistoricMessages()

Gets the list of historic Messages in the notification.

public java.util.List<NotificationCompat.MessagingStyle.Message>getMessages()

Gets the list of Message objects that represent the notification.

public PersongetUser()

Returns the person to be used for any replies sent by the user.

public java.lang.CharSequencegetUserDisplayName()

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

public booleanisGroupConversation()

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

protected voidrestoreFromCompatExtras(Bundle extras)

public NotificationCompat.MessagingStylesetConversationTitle(java.lang.CharSequence conversationTitle)

Sets the title to be displayed on this conversation.

public NotificationCompat.MessagingStylesetGroupConversation(boolean isGroupConversation)

Sets whether this conversation notification represents a group.

from NotificationCompat.StyleapplyStandardTemplate, build, buildIntoRemoteViews, createColoredBitmap, displayCustomViewInline, extractStyleFromNotification, makeBigContentView, makeContentView, makeHeadsUpContentView, setBuilder
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

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).

Constructors

public MessagingStyle(java.lang.CharSequence userDisplayName)

Deprecated: Use #MessagingStyle(Person) instead.

Parameters:

userDisplayName: 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 NotificationCompat.MessagingStyle.addMessage(NotificationCompat.MessagingStyle.Message)

public MessagingStyle(Person user)

Creates a new NotificationCompat.MessagingStyle object. Note that Person must have a non-empty name.

Parameters:

user: This Person's name will be shown when this app's notification is being replied to. It's used temporarily so the app has time to process the send request and repost the notification with updates to the conversation.

Methods

public java.lang.CharSequence getUserDisplayName()

Deprecated: Use NotificationCompat.MessagingStyle.getUser() instead.

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

public Person getUser()

Returns the person to be used for any replies sent by the user.

public NotificationCompat.MessagingStyle setConversationTitle(java.lang.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 . If your application's target version is less than , setting a conversation title to a non-null value will make NotificationCompat.MessagingStyle.isGroupConversation() return true and passing null will make it return false. This behavior can be overridden by calling NotificationCompat.MessagingStyle.setGroupConversation(boolean) regardless of SDK version. In and above, this method does not affect group conversation settings.

Parameters:

conversationTitle: Title displayed for this conversation

Returns:

this object for method chaining

public java.lang.CharSequence getConversationTitle()

Return the title to be displayed on this conversation. Can be null.

public NotificationCompat.MessagingStyle addMessage(java.lang.CharSequence text, long timestamp, java.lang.CharSequence sender)

Deprecated: Use NotificationCompat.MessagingStyle.addMessage(CharSequence, long, Person) or NotificationCompat.MessagingStyle.addMessage(NotificationCompat.MessagingStyle.Message)

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

Parameters:

text: A java.lang.CharSequence to be displayed as the message content
timestamp: Time at which the message arrived in ms since Unix epoch
sender: A java.lang.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 NotificationCompat.MessagingStyle.getUserDisplayName(). Should be unique amongst all individuals in the conversation, and should be consistent during re-posts of the notification.

Returns:

this object for method chaining

See also: NotificationCompat.MessagingStyle.Message

public NotificationCompat.MessagingStyle addMessage(java.lang.CharSequence text, long timestamp, Person person)

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

Returns:

this for method chaining

See also: NotificationCompat.MessagingStyle.Message

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

Parameters:

message: The NotificationCompat.MessagingStyle.Message to be displayed

Returns:

this object for method chaining

Adds a NotificationCompat.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: The historic NotificationCompat.MessagingStyle.Message to be added

Returns:

this object for method chaining

public java.util.List<NotificationCompat.MessagingStyle.Message> getMessages()

Gets the list of Message objects that represent the notification.

public java.util.List<NotificationCompat.MessagingStyle.Message> getHistoricMessages()

Gets the list of historic Messages in the notification.

public NotificationCompat.MessagingStyle setGroupConversation(boolean isGroupConversation)

Sets whether this conversation notification represents a group. An app should set isGroupConversation true to mark that the conversation involves multiple people.

Group conversation notifications may display additional group-related context not present in non-group notifications.

Parameters:

isGroupConversation: true if the conversation represents a group, false otherwise.

Returns:

this object for method chaining

See also: NotificationCompat.MessagingStyle.isGroupConversation()

public boolean isGroupConversation()

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

If the application that generated this NotificationCompat.MessagingStyle targets an SDK version less than and NotificationCompat.MessagingStyle.setGroupConversation(boolean) was not called, 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. This is to maintain backwards compatibility. Regardless, NotificationCompat.MessagingStyle.setGroupConversation(boolean) has precedence over this legacy behavior. From forward, NotificationCompat.MessagingStyle.setConversationTitle(CharSequence) has no affect on group conversation status.

See also: NotificationCompat.MessagingStyle.setConversationTitle(CharSequence)

public static NotificationCompat.MessagingStyle extractMessagingStyleFromNotification(Notification notification)

Retrieves a NotificationCompat.MessagingStyle from a Notification, enabling an application that has set a NotificationCompat.MessagingStyle using NotificationCompat or to send messaging information to another application using NotificationCompat, regardless of the API level of the system.

Returns:

null if there is no NotificationCompat.MessagingStyle set, or if the SDK version is < 16 (JellyBean).

protected java.lang.String getClassName()

public void apply(NotificationBuilderWithBuilderAccessor builder)

public void addCompatExtras(Bundle extras)

This is called with the extras of the framework Notification during the NotificationCompat.Builder.build() process, after apply() has been called. This means that you only need to add data which won't be populated by the framework Notification which was built so far. Moreover, recovering builders and styles is only supported at API 19 and above, no implementation is required for current BigTextStyle, BigPictureStyle, or InboxStyle.

protected void restoreFromCompatExtras(Bundle extras)

protected void clearCompatExtraKeys(Bundle extras)