MessagingStyle
open class MessagingStyle : NotificationCompat.Style
kotlin.Any | ||
↳ | 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 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
Nested classes | |
---|---|
Constants | |
---|---|
static Int |
The maximum number of messages that will be retained in the Notification itself (the number displayed is up to the platform). |
Public constructors | |
---|---|
<init>(@NonNull userDisplayName: CharSequence) |
|
Creates a new |
Public methods | |
---|---|
open Unit |
addCompatExtras(@NonNull extras: Bundle) |
open NotificationCompat.MessagingStyle |
addHistoricMessage(@Nullable message: NotificationCompat.MessagingStyle.Message?) Adds a |
open NotificationCompat.MessagingStyle |
addMessage(@Nullable text: CharSequence?, timestamp: Long, @Nullable sender: CharSequence?) Adds a message for display by this notification. |
open NotificationCompat.MessagingStyle |
addMessage(@Nullable text: CharSequence?, timestamp: Long, @Nullable person: Person?) Adds a message for display by this notification. |
open NotificationCompat.MessagingStyle |
addMessage(@Nullable message: NotificationCompat.MessagingStyle.Message?) Adds a |
open static NotificationCompat.MessagingStyle? |
extractMessagingStyleFromNotification(@NonNull notification: Notification) Retrieves a |
open CharSequence? |
Return the title to be displayed on this conversation. |
open MutableList<NotificationCompat.MessagingStyle.Message!> |
Gets the list of historic |
open MutableList<NotificationCompat.MessagingStyle.Message!> |
Gets the list of |
open Person |
getUser() Returns the person to be used for any replies sent by the user. |
open CharSequence? |
Returns the name to be displayed for any replies sent by the user. |
open Boolean |
Returns |
open NotificationCompat.MessagingStyle |
setConversationTitle(@Nullable conversationTitle: CharSequence?) Sets the title to be displayed on this conversation. |
open NotificationCompat.MessagingStyle |
setGroupConversation(isGroupConversation: Boolean) Sets whether this conversation notification represents a group. |
Inherited functions | |
---|---|
Constants
MAXIMUM_RETAINED_MESSAGES
static val MAXIMUM_RETAINED_MESSAGES: Int
The maximum number of messages that will be retained in the Notification itself (the number displayed is up to the platform).
Value: 25
Public constructors
<init>
MessagingStyle(@NonNull userDisplayName: CharSequence)
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) |
<init>
MessagingStyle(@NonNull user: Person)
Creates a new 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
addHistoricMessage
@NonNull open fun addHistoricMessage(@Nullable message: NotificationCompat.MessagingStyle.Message?): NotificationCompat.MessagingStyle
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 | |
---|---|
message |
NotificationCompat.MessagingStyle.Message?: The historic Message to be added |
Return | |
---|---|
NotificationCompat.MessagingStyle |
this object for method chaining |
addMessage
@NonNull open funaddMessage(
@Nullable text: CharSequence?,
timestamp: Long,
@Nullable sender: CharSequence?
): NotificationCompat.MessagingStyle
Deprecated: Use addMessage(CharSequence, long, Person)
or addMessage(Message)
Adds a message for display by this notification. Convenience call for a simple 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. |
Return | |
---|---|
NotificationCompat.MessagingStyle |
this object for method chaining |
addMessage
@NonNull open fun addMessage(
@Nullable text: CharSequence?,
timestamp: Long,
@Nullable person: Person?
): NotificationCompat.MessagingStyle
Adds a message for display by this notification. Convenience call for addMessage(Message)
.
Return | |
---|---|
NotificationCompat.MessagingStyle |
this for method chaining |
addMessage
@NonNull open fun addMessage(@Nullable message: NotificationCompat.MessagingStyle.Message?): NotificationCompat.MessagingStyle
Adds a Message
for display in this notification.
Parameters | |
---|---|
message |
NotificationCompat.MessagingStyle.Message?: The Message to be displayed |
Return | |
---|---|
NotificationCompat.MessagingStyle |
this object for method chaining |
extractMessagingStyleFromNotification
@Nullable open static fun extractMessagingStyleFromNotification(@NonNull notification: Notification): NotificationCompat.MessagingStyle?
Retrieves a MessagingStyle
from a Notification
, enabling an application that has set a MessagingStyle
using NotificationCompat
or android.app.Notification.Builder
to send messaging information to another application using NotificationCompat
, regardless of the API level of the system.
Return | |
---|---|
NotificationCompat.MessagingStyle? |
null if there is no MessagingStyle set, or if the SDK version is < 16 (JellyBean). |