Skip to content

Most visited

Recently visited


Create an Expandable Notification

A basic notification usually includes a title, a line of text, and one or more actions the user can perform in response. To provide even more information, you can also create large, expandable notifications by applying one of several notification templates as described on this page.

To start, build a notification with all the basic content as described in Create a Notification. Then, call setStyle() with a style object and supply information corresponding to each template, as shown below.

Add a large image

To add an image in your notification, pass an instance of NotificationCompat.BigPictureStyle to setStyle().

Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_ID)
        .setStyle(new NotificationCompat.BigPictureStyle()

To make the image appear as a thumbnail only while the notification is collapsed (as shown in figure 1), call setLargeIcon() and pass it the image, but also call BigPictureStyle.bigLargeIcon() and pass it null so the large icon goes away when the notification is expanded:

Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_ID)
        .setStyle(new NotificationCompat.BigPictureStyle()

Figure 1. A notification using NotificationCompat.BigPictureStyle

Add a large block of text

Apply NotificationCompat.BigTextStyle to display text in the expanded content area of the notification:

Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_ID)
        .setStyle(new NotificationCompat.BigTextStyle()

Figure 2. A notification using NotificationCompat.BigTextStyle

Tip: To add formatting in your text (bold, italic, line breaks, and so on), you can add styling with HTML markup.

Create an inbox-style notification

Apply NotificationCompat.InboxStyle to a notification if you want to add multiple short summary lines, such as snippets from incoming emails. This allows you to add multiple pieces of content text that are each truncated to one line, instead of one continuous line of text provided by NotificationCompat.BigTextStyle.

To add a new line, call addLine() up to 6 times. If you add more than 6 lines, only the first 6 are visible.

Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_ID)
        .setContentTitle("5 New mails from " + sender.toString())
        .setStyle(new NotificationCompat.InboxStyle()

Tip: You can distinguish the message's subject and message in each line by adding styling with HTML markup (such as bolding the subject).

Show a conversation in a notification

Apply NotificationCompat.MessagingStyle to display sequential messages between any number of people. This is ideal for messaging apps because it provides a consistent layout for each message by handling the sender name and message text separately, and each message can be multiple lines long.

To add a new message, call addMessage(), passing the message text, the time received, and the sender name. You can also pass this information as a NotificationCompat.MessagingStyle.Message object.

NotificationCompat.MessagingStyle.Message message1 =
        new NotificationCompat.MessagingStyle.Message(messages[0].getText(),
NotificationCompat.MessagingStyle.Message message2 =
        new NotificationCompat.MessagingStyle.Message(messages[1].getText(),

Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_ID)
        .setStyle(new NotificationCompat.MessagingStyle(resources.getString(R.string.reply_name))

Figure 3. A notification using NotificationCompat.MessagingStyle

Optionally, you can call setConversationTitle() to add a title that appears above the conversation. This might 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, because the system uses the existence of this field as a hint that the conversation is a group.

This style applies only on devices running Android 7.0 (API level 24) and up. When using the compatibility library (NotificationCompat) as demonstrated above, notifications with MessagingStyle will fallback automatically to a supported expanded notification style.

When building a notification like this for a chat conversation, you should also add a direct reply action.

Create a notification with media controls

Apply NotificationCompat.MediaStyle to display media playback controls and track information.

Call addAction() up to five times to display up to five separate icon buttons. And call setLargeIcon() to set the album artwork.

Unlike the other notification styles, MediaStyle allows you to also modify the collapsed-size content view by specifying three action buttons that should also appear in the collapsed view. To do so, provide the action button indices to setShowActionsInCompactView().

If the notification represents an active media session, also attach a MediaSession.Token to the notification using setMediaSession(). Android then identifies this as a notification representing an active media session and respond accordingly (by showing album artwork in the lock screen, for example).

Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
        // Show controls on lock screen even when user hides sensitive content.
        // Add media control buttons that invoke intents in your media service
        .addAction(R.drawable.ic_prev, "Previous", prevPendingIntent) // #0
        .addAction(R.drawable.ic_pause, "Pause", pausePendingIntent)  // #1
        .addAction(R.drawable.ic_next, "Next", nextPendingIntent)     // #2
        // Apply the media style template
        .setStyle(new NotificationCompat.MediaStyle()
                .setShowActionsInCompactView(1 /* #1: pause button */)
        .setContentTitle("Wonderful music")
        .setContentText("My Awesome Band")

Figure 4. A notification using NotificationCompat.MediaStyle

For more information, also read Using MediaStyle notifications with a foreground service.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields


Ikuti Google Developers di WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)