NotificationCompat.MessagingStyle
public class NotificationCompat.MessagingStyle extends NotificationCompat.Style
java.lang.Object | ||
↳ | androidx.core.app.NotificationCompat.Style | |
↳ | androidx.core.app.NotificationCompat.MessagingStyle |
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 setConversationTitle
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
Nested types |
---|
public final class NotificationCompat.MessagingStyle.Message |
Constants |
|
---|---|
static final int |
The maximum number of messages that will be retained in the Notification itself (the number displayed is up to the platform). |
Public constructors |
---|
This method is deprecated. Use |
MessagingStyle(@NonNull Person user) Creates a new |
Public methods |
|
---|---|
void |
addCompatExtras(@NonNull Bundle extras) This is called with the extras of the framework |
@NonNull NotificationCompat.MessagingStyle |
Adds a |
@NonNull NotificationCompat.MessagingStyle |
Adds a |
@NonNull NotificationCompat.MessagingStyle |
addMessage( Adds a message for display by this notification. |
@NonNull NotificationCompat.MessagingStyle |
This method is deprecated. Use |
static @Nullable NotificationCompat.MessagingStyle |
extractMessagingStyleFromNotification( Retrieves a |
@Nullable CharSequence |
Return the title to be displayed on this conversation. |
@NonNull List<NotificationCompat.MessagingStyle.Message> |
Gets the list of historic |
@NonNull List<NotificationCompat.MessagingStyle.Message> |
Gets the list of |
@NonNull Person |
getUser() Returns the person to be used for any replies sent by the user. |
@Nullable CharSequence |
This method is deprecated. Use |
boolean |
Returns |
@NonNull NotificationCompat.MessagingStyle |
setConversationTitle(@Nullable CharSequence conversationTitle) Sets the title to be displayed on this conversation. |
@NonNull NotificationCompat.MessagingStyle |
setGroupConversation(boolean isGroupConversation) Sets whether this conversation notification represents a group. |
Inherited methods |
||||
---|---|---|---|---|
|
Constants
MAXIMUM_RETAINED_MESSAGES
public static final int MAXIMUM_RETAINED_MESSAGES = 25
The maximum number of messages that will be retained in the Notification itself (the number displayed is up to the platform).
Public constructors
publicMessagingStyle(@NonNull CharSequence userDisplayName)
Parameters | |
---|---|
@NonNull CharSequence 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 |
MessagingStyle
public MessagingStyle(@NonNull Person user)
Creates a new MessagingStyle
object. Note that Person
must have a non-empty name.
Public methods
addCompatExtras
public void addCompatExtras(@NonNull Bundle extras)
This is called with the extras of the framework Notification
during the 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.
addHistoricMessage
public @NonNull NotificationCompat.MessagingStyle addHistoricMessage(
@Nullable NotificationCompat.MessagingStyle.Message message
)
Adds a 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 | |
---|---|
@Nullable NotificationCompat.MessagingStyle.Message message |
The historic |
Returns | |
---|---|
@NonNull NotificationCompat.MessagingStyle |
this object for method chaining |
addMessage
public @NonNull NotificationCompat.MessagingStyle addMessage(@Nullable NotificationCompat.MessagingStyle.Message message)
Adds a Message
for display in this notification.
Parameters | |
---|---|
@Nullable NotificationCompat.MessagingStyle.Message message |
The |
Returns | |
---|---|
@NonNull NotificationCompat.MessagingStyle |
this object for method chaining |
addMessage
public @NonNull NotificationCompat.MessagingStyle addMessage(
@Nullable CharSequence text,
long timestamp,
@Nullable Person person
)
Adds a message for display by this notification. Convenience call for addMessage
.
Returns | |
---|---|
@NonNull NotificationCompat.MessagingStyle |
this for method chaining |
See also | |
---|---|
Message |
public @NonNull NotificationCompat.MessagingStyleaddMessage(
@Nullable CharSequence text,
long timestamp,
@Nullable CharSequence sender
)
Adds a message for display by this notification. Convenience call for a simple Message
in addMessage
Parameters | |
---|---|
@Nullable CharSequence text |
A |
long timestamp |
Time at which the message arrived in ms since Unix epoch |
@Nullable CharSequence sender |
A |
Returns | |
---|---|
@NonNull NotificationCompat.MessagingStyle |
this object for method chaining |
See also | |
---|---|
Message |
extractMessagingStyleFromNotification
public static @Nullable NotificationCompat.MessagingStyle extractMessagingStyleFromNotification(
@NonNull Notification notification
)
Retrieves a MessagingStyle
from a Notification
, enabling an application that has set a MessagingStyle
using NotificationCompat
or Notification.Builder
to send messaging information to another application using NotificationCompat
, regardless of the API level of the system.
Returns | |
---|---|
@Nullable NotificationCompat.MessagingStyle |
|
getConversationTitle
public @Nullable CharSequence getConversationTitle()
Return the title to be displayed on this conversation. Can be null
.
getHistoricMessages
public @NonNull List<NotificationCompat.MessagingStyle.Message> getHistoricMessages()
Gets the list of historic Message
s in the notification.
getMessages
public @NonNull List<NotificationCompat.MessagingStyle.Message> getMessages()
Gets the list of Message
objects that represent the notification.
getUser
public @NonNull Person getUser()
Returns the person to be used for any replies sent by the user.
public @Nullable CharSequencegetUserDisplayName()
Returns the name to be displayed for any replies sent by the user.
isGroupConversation
public boolean isGroupConversation()
Returns true
if this notification represents a group conversation, otherwise false
.
If the application that generated this MessagingStyle
targets an SDK version less than P
and setGroupConversation
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, setGroupConversation
has precedence over this legacy behavior. From P
forward, setConversationTitle
has no affect on group conversation status.
See also | |
---|---|
setConversationTitle |
setConversationTitle
public @NonNull NotificationCompat.MessagingStyle setConversationTitle(@Nullable 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 P
. If your application's target version is less than P
, setting a conversation title to a non-null value will make isGroupConversation
return true
and passing null
will make it return false
. This behavior can be overridden by calling setGroupConversation
regardless of SDK version. In P
and above, this method does not affect group conversation settings.
Parameters | |
---|---|
@Nullable CharSequence conversationTitle |
Title displayed for this conversation |
Returns | |
---|---|
@NonNull NotificationCompat.MessagingStyle |
this object for method chaining |
setGroupConversation
public @NonNull 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 | |
---|---|
boolean isGroupConversation |
|
Returns | |
---|---|
@NonNull NotificationCompat.MessagingStyle |
this object for method chaining |
See also | |
---|---|
isGroupConversation |