NotificationCompat.MessagingStyle
public
static
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(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
Nested classes | |
---|---|
class |
NotificationCompat.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). |
Public constructors | |
---|---|
MessagingStyle(CharSequence userDisplayName)
This constructor is deprecated.
Use |
|
MessagingStyle(Person user)
Creates a new |
Public methods | |
---|---|
void
|
addCompatExtras(Bundle extras)
This is called with the extras of the framework |
NotificationCompat.MessagingStyle
|
addHistoricMessage(NotificationCompat.MessagingStyle.Message message)
Adds a |
NotificationCompat.MessagingStyle
|
addMessage(CharSequence text, long timestamp, CharSequence sender)
This method is deprecated.
Use |
NotificationCompat.MessagingStyle
|
addMessage(NotificationCompat.MessagingStyle.Message message)
Adds a |
NotificationCompat.MessagingStyle
|
addMessage(CharSequence text, long timestamp, Person person)
Adds a message for display by this notification. |
static
NotificationCompat.MessagingStyle
|
extractMessagingStyleFromNotification(Notification notification)
Retrieves a |
CharSequence
|
getConversationTitle()
Return the title to be displayed on this conversation. |
List<NotificationCompat.MessagingStyle.Message>
|
getHistoricMessages()
Gets the list of historic |
List<NotificationCompat.MessagingStyle.Message>
|
getMessages()
Gets the list of |
Person
|
getUser()
Returns the person to be used for any replies sent by the user. |
CharSequence
|
getUserDisplayName()
This method is deprecated.
Use |
boolean
|
isGroupConversation()
Returns |
NotificationCompat.MessagingStyle
|
setConversationTitle(CharSequence conversationTitle)
Sets the title to be displayed on this conversation. |
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
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 (CharSequence userDisplayName)
This constructor is deprecated.
Use #MessagingStyle(Person)
instead.
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(Message) |
MessagingStyle
public MessagingStyle (Person user)
Creates a new NotificationCompat.MessagingStyle
object. Note that Person
must have a
non-empty name.
Parameters | |
---|---|
user |
Person : 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.
|
Public methods
addCompatExtras
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.
Parameters | |
---|---|
extras |
Bundle |
addHistoricMessage
public NotificationCompat.MessagingStyle addHistoricMessage (NotificationCompat.MessagingStyle.Message message)
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 |
NotificationCompat.MessagingStyle.Message : The historic NotificationCompat.MessagingStyle.Message to be added |
Returns | |
---|---|
NotificationCompat.MessagingStyle |
this object for method chaining |
addMessage
public NotificationCompat.MessagingStyle addMessage (CharSequence text, long timestamp, CharSequence sender)
This method is deprecated.
Use addMessage(CharSequence, long, Person)
or
addMessage(Message)
Adds a message for display by this notification. Convenience call for a simple
NotificationCompat.MessagingStyle.Message
in addMessage(Message)
Parameters | |
---|---|
text |
CharSequence : A CharSequence to be displayed as the message content |
timestamp |
long : Time at which the message arrived in ms since Unix epoch |
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 | |
---|---|
NotificationCompat.MessagingStyle |
this object for method chaining |
addMessage
public NotificationCompat.MessagingStyle addMessage (NotificationCompat.MessagingStyle.Message message)
Adds a NotificationCompat.MessagingStyle.Message
for display in this notification.
Parameters | |
---|---|
message |
NotificationCompat.MessagingStyle.Message : The NotificationCompat.MessagingStyle.Message to be displayed |
Returns | |
---|---|
NotificationCompat.MessagingStyle |
this object for method chaining |
addMessage
public NotificationCompat.MessagingStyle addMessage (CharSequence text, long timestamp, Person person)
Adds a message for display by this notification. Convenience call for
addMessage(Message)
.
Parameters | |
---|---|
text |
CharSequence |
timestamp |
long |
person |
Person |
Returns | |
---|---|
NotificationCompat.MessagingStyle |
this for method chaining |
extractMessagingStyleFromNotification
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
Notification.Builder
to send messaging information to another
application using NotificationCompat
, regardless of the API level of the system.
Parameters | |
---|---|
notification |
Notification |
Returns | |
---|---|
NotificationCompat.MessagingStyle |
null if there is no NotificationCompat.MessagingStyle set, or if the SDK version is
< 16 (JellyBean).
|
getConversationTitle
public CharSequence getConversationTitle ()
Return the title to be displayed on this conversation. Can be null
.
Returns | |
---|---|
CharSequence |
getHistoricMessages
public List<NotificationCompat.MessagingStyle.Message> getHistoricMessages ()
Gets the list of historic Message
s in the notification.
Returns | |
---|---|
List<NotificationCompat.MessagingStyle.Message> |
getMessages
public List<NotificationCompat.MessagingStyle.Message> getMessages ()
Gets the list of Message
objects that represent the notification
Returns | |
---|---|
List<NotificationCompat.MessagingStyle.Message> |
getUser
public Person getUser ()
Returns the person to be used for any replies sent by the user.
Returns | |
---|---|
Person |
getUserDisplayName
public CharSequence getUserDisplayName ()
This method is deprecated.
Use getUser()
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, otherwise
false
.
If the application that generated this NotificationCompat.MessagingStyle
targets an SDK version
less than Build.VERSION_CODES.P
and 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, setGroupConversation(boolean)
has precedence over this legacy behavior. From P
forward, setConversationTitle(CharSequence)
has no affect on group conversation
status.
Returns | |
---|---|
boolean |
See also:
setConversationTitle
public NotificationCompat.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
. This behavior
can be overridden by calling setGroupConversation(boolean)
regardless of SDK
version. In P
and above, this method does not affect group conversation settings.
Parameters | |
---|---|
conversationTitle |
CharSequence : Title displayed for this conversation |
Returns | |
---|---|
NotificationCompat.MessagingStyle |
this object for method chaining |
setGroupConversation
public NotificationCompat.MessagingStyle setGroupConversation (boolean isGroupConversation)
Sets whether this conversation notification represents a group.
Parameters | |
---|---|
isGroupConversation |
boolean : true if the conversation represents a group,
false otherwise. |
Returns | |
---|---|
NotificationCompat.MessagingStyle |
this object for method chaining |
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2021-02-24 UTC.