This post is about android notification, how to create a simple notification, notification styles, how to add actions in notifications with the help of simple android application.
Notification
A Notifications provide short, timely information about events happened in the application, even if the application is not running.
Creating a Basic Notification
A notification in its most basic and compact form (or collapsed form) displays an icon, a title, and a small amount of content text.

Setting the notification content
To create a basic notification, we can set the notification content with the following properties :
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher_round) .setContentTitle(title) .setContentText(text) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .build();
setSmallIcon(): Set the small icon in notification. This is the only required property and it appears in the status bar.

setContentTitle(): Set the title of the notification.
setContentText(): Set the text of the notification.
setPriority(): Set the priority of notification. This property will work only on Android 7.1 and lower. (For Android 8.0 and higher, you must instead set the channel importance)
Note: CHANNEL_ID is required for Android 8.0 (API level 26) and higher, but is ignored by older versions.
Creating a notification channel
Starting in Android 8.0 (API level 26) , all notifications must be assigned to a channel or it will not appear. One app can have multiple notification channels.
By categorising notifications into channels, users can disable specific notification channels for your app (instead of disabling all your notifications), and users can control the visual and auditory options for each channel—all from the Android system settings.
On devices running Android 7.1 (API level 25) and lower, each app only has one channel.
To create a notification channel, follow these steps:
- Construct a NotificationChannel object with a unique channel ID, a user-visible name, and an importance level.
- Optionally, specify the description that the user sees in the system settings with setDescription().
- Register the notification channel by passing it to createNotificationChannel(). Use createNotificationChannels() to create multiple notification channels in a single operation.
//Unique channel ID final String CHANNEL_ID = "Important_mail_channel"; private void createNotificationChannel() { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is new and not in the support library if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //Channel name CharSequence name = "Important_mail_channel"; //Channel description String description = "This channel will show notification only to important people"; //The importance level you assign to a channel applies to all notifications that you post to it. int importance = NotificationManager.IMPORTANCE_DEFAULT; //Create the NotificationChannel NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); //Set channel description channel.setDescription(description); // Register the channel with the system; you can't change the importance // or other notification behaviors after this NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } }
Notification settings for NotificationTest app and one of its channel (Important_mail_channel).
Setting the notification’s tap action
To perform some operation on notification’s tap, you must specify a content intent defined with a PendingIntent object and pass it to setContentIntent().
The following snippet shows how to create a basic intent to open an url when the user taps the notification:
//open the url when user taps the notification Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.c1ctech.com/")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher_round) .setContentTitle(title) .setContentText(text) .setPriority(NotificationCompat.PRIORITY_DEFAULT) // Set the intent that will fire when the user taps the notification .setContentIntent(pendingIntent) //removes the notification when the user taps it .setAutoCancel(true) .build();
Show the notification
To make the notification appear, call NotificationManagerCompat.notify(), passing it a unique ID for the notification and the Notification object.
NotificationManagerCompat mNotificationManagerCompat; Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher_round) .setContentTitle(title) .setContentText(text) .setLargeIcon(bitmap) .setPriority(NotificationCompat.PRIORITY_DEFAULT) //Set the intent that will fire when the user taps the notification .setContentIntent(pendingIntent) .setAutoCancel(true) .build(); //notificationId must be unique int for each notification mNotificationManagerCompat.notify(notificationId, notification);
Android Notification Styles
By default, the notification’s text content is truncated to fit one line. If you want your notification to be longer, you can enable an expandable notification by adding a style template with setStyle().
BigText Style
This style should be used when a notification contains a large block of text. It lets the user preview more text when the notification is expanded.
final String CHANNEL_ID = "Important_mail_channel"; private void createBigTextNotification(String title, String text, int notificationId) { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nature_img); NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle().bigText(text + " used for generating large-format notifications that include a lot of text.") //set different title in expanded mode. .setBigContentTitle(null) //needed if an app sends notification from multiple sources(accounts). .setSummaryText("BigTextStyle"); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher_round) .setContentTitle(title) .setContentText(text + " used for generating large-format notifications that include a lot of text.") //set BigTextStyle .setStyle(style) //set the large icon in the notification. .setLargeIcon(bitmap) .build(); mNotificationManagerCompat.notify(notificationId, notification); }
Collapsed Mode:

Expanded Mode:

BigPicture Style
This style should be used when a notification contains a picture. The large icon offers a thumbnail of the picture, and the user can get a bigger preview by expanding the notification.
final String CHANNEL_ID = "Important_mail_channel"; private void createBigPictureNotification(String title, String text, int notificationId) { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nature_img); NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle() //set big picture .bigPicture(bitmap) //set the content text in expanded form. .setSummaryText("BigPicture style is used to show large image.") //set different title in expanded mode. .setBigContentTitle(null) //set different large icon in expanded mode. .bigLargeIcon(null); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher_round) .setContentTitle(title) .setContentText(text) .setLargeIcon(bitmap) //set Big picture template .setStyle(style) .build(); mNotificationManagerCompat.notify(notificationId, notification); }
Collapsed Mode:

Expanded Mode:

Inbox Style
This style should be used when a notification contains a list of strings. It lets the user preview more text (list of strings) when the notification is expanded.
final String CHANNEL_ID = "Important_mail_channel"; private void createInboxNotification(String title, String text, int notificationId) { String line1 = "This is line1 "; String line2 = "This is line2 "; String line3 = "This is line3 "; String line4 = "This is line4 "; String line5 = "This is line5 "; String line6 = "This is line6 "; String line7 = "This is line7 "; NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle() //To add n lines, call it n times .addLine(line1) .addLine(line2) .addLine(line3) .addLine(line4) .addLine(line5) .addLine(line6) .addLine(line7) //needed if an app send notification from multiple sources(accounts). .setSummaryText("InboxStyle") //set different title in expanded form. .setBigContentTitle(null); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher_round) .setContentTitle(title) .setContentText("It is used for notifications includes a list of (up to 5) strings.") //set inbox style in notification .setStyle(style) .build(); mNotificationManagerCompat.notify(notificationId, notification); }
Collapsed Mode:

Expanded Mode:

Add Action Buttons
A notification can offer up to three action buttons that allow the user to respond quickly, such as dismiss a notification or even reply to a text message.
The following code shows how to send a broadcast to a specific receiver (cancels the notification of specific notificationId) on click of action buttons Dismiss and Delete:
final String CHANNEL_ID = "Important_mail_channel"; private void createActionNotification(String title, String text, int notificationId) { Intent intent = new Intent(getApplicationContext(), Receiver.class); intent.setAction("ACTION_CANCEL"); //passing notificationId to receiver class through intent intent.putExtra("id", notificationId); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); //Action fire on click of notification Dismiss action button NotificationCompat.Action actionDismiss = new NotificationCompat.Action.Builder(0, "Dismiss", pendingIntent) .build(); //Action fire on click of notification Delete action button NotificationCompat.Action actionDelete = new NotificationCompat.Action.Builder(0, "Delete", pendingIntent) .build(); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher_round) .setContentTitle(title) .setContentText(text) .setStyle(new NotificationCompat.BigTextStyle().bigText("This is an example of BigTextStyle notification with action.")) //Add actions Dismiss and Delete to this notification. .addAction(actionDismiss) .addAction(actionDelete) .build(); mNotificationManagerCompat.notify(notificationId, notification); }
A BigTextStyle notification with two action buttons.
Collapsed Mode:

Expanded Mode:

Creating New Project
1. Create a new project NotificationTest in Android Studio from File ⇒ New Project, fill the project details and then click on finish.
2. Open activity_main.xml file and add the following code as shown below:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/btn_simple_notification" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="15dp" android:text="@string/simple_notification_title" android:textAllCaps="false" android:textSize="18sp" app:layout_constraintBottom_toTopOf="@+id/btn_bigtextstyle_notification" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btn_bigtextstyle_notification" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="15dp" android:text="@string/bigtext_notification_style_title" android:textAllCaps="false" android:textSize="18sp" app:layout_constraintBottom_toTopOf="@+id/btn_bigpicturestyle_notification" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btn_simple_notification" /> <Button android:id="@+id/btn_bigpicturestyle_notification" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="15dp" android:text="@string/bigPicture_notification_style_title" android:textAllCaps="false" android:textSize="18sp" app:layout_constraintBottom_toTopOf="@+id/btn_inboxstyle_notification" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btn_bigtextstyle_notification" /> <Button android:id="@+id/btn_inboxstyle_notification" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="15dp" android:text="@string/inbox_notification_style_title" android:textAllCaps="false" android:textSize="18sp" app:layout_constraintBottom_toTopOf="@+id/btn_action_notification" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btn_bigpicturestyle_notification" /> <Button android:id="@+id/btn_action_notification" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="15dp" android:text="@string/action_notification_title" android:textAllCaps="false" android:textSize="18sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btn_inboxstyle_notification" /> </androidx.constraintlayout.widget.ConstraintLayout>
The layout of the above activity_main.xml file will look like this:

3. Create a new class Receiver.java inside project package folder. Receiver class onReceive() will call on click of notification action buttons (Delete and Dismiss).
Receiver.java
package com.example.notificationtest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import androidx.core.app.NotificationManagerCompat; public class Receiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent != null) { //get notificationId of notification using intent int id = intent.getIntExtra("id", 0); NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context); //cancel the specific notificationId notification. notificationManagerCompat.cancel(id); } } }
4. To register the user defined broadcast receiver (Receiver.java) inside AndroidManifest.xml file write the below code:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.notificationtest"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".Receiver"> <intent-filter> <category android:name="android.intent.categeory.DEFAULT"/> </intent-filter> </receiver> </application> </manifest>
5. Open MainActivity.java and add the below code:
MainActivity.java
package com.example.notificationtest; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; public class MainActivity extends AppCompatActivity implements View.OnClickListener { final String CHANNEL_ID = "Important_mail_channel"; Button mBtnSimpleNotification, mBtnBigTextNotification, mBtnBigPictureNotification, mBtnInboxNotification, mBtnActionNotification; NotificationManagerCompat mNotificationManagerCompat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); createNotificationChannel(); mBtnSimpleNotification = findViewById(R.id.btn_simple_notification); mBtnBigTextNotification = findViewById(R.id.btn_bigtextstyle_notification); mBtnBigPictureNotification = findViewById(R.id.btn_bigpicturestyle_notification); mBtnInboxNotification = findViewById(R.id.btn_inboxstyle_notification); mBtnActionNotification = findViewById(R.id.btn_action_notification); mBtnSimpleNotification.setOnClickListener(this); mBtnBigTextNotification.setOnClickListener(this); mBtnBigPictureNotification.setOnClickListener(this); mBtnInboxNotification.setOnClickListener(this); mBtnActionNotification.setOnClickListener(this); mNotificationManagerCompat = NotificationManagerCompat.from(MainActivity.this); } @Override public void onClick(View view) { int viewId = view.getId(); switch (viewId) { case (R.id.btn_simple_notification): createSimpleNotification(getString(R.string.simple_notification_title), getString(R.string.simple_notification_text), 1); break; case (R.id.btn_bigtextstyle_notification): createBigTextNotification(getString(R.string.bigtext_notification_title), getString(R.string.bigtext_notification_text), 2); break; case (R.id.btn_bigpicturestyle_notification): createBigPictureNotification(getString(R.string.bigPicture_notification_title), getString(R.string.bigPicture_notification_text), 3); break; case (R.id.btn_inboxstyle_notification): createInboxNotification(getString(R.string.inbox_notification_title), getString(R.string.inbox_notification_text), 4); break; case (R.id.btn_action_notification): createActionNotification(getString(R.string.action_notification_title), getString(R.string.action_notification_text), 5); break; } } private void createSimpleNotification(String title, String text, int notificationId) { //removes all previously shown notifications. mNotificationManagerCompat.cancelAll(); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nature_img); //open the url when user taps the notification Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.c1ctech.com/")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher_round) .setContentTitle(title) .setContentText(text) .setPriority(NotificationCompat.PRIORITY_DEFAULT) //Set the intent that will fire when the user taps the notification .setContentIntent(pendingIntent) .setAutoCancel(true) .build(); // notificationId is a unique int for each notification that you must define mNotificationManagerCompat.notify(notificationId, notification); } private void createBigTextNotification(String title, String text, int notificationId) { //removes all previously shown notifications. mNotificationManagerCompat.cancelAll(); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nature_img); NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle().bigText(text + " used for generating large-format notifications that include a lot of text.") //set different title in expanded mode. .setBigContentTitle(null) //needed if an app sends notification from multiple sources(accounts). .setSummaryText("BigTextStyle"); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher_round) .setContentTitle(title) .setContentText(text + " used for generating large-format notifications that include a lot of text.") //set Big text template .setStyle(style) //Set the large icon in the notification. .setLargeIcon(bitmap) .build(); mNotificationManagerCompat.notify(notificationId, notification); } private void createBigPictureNotification(String title, String text, int notificationId) { //removes all previously shown notifications. mNotificationManagerCompat.cancelAll(); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nature_img); NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle() //set big picture .bigPicture(bitmap) //set the content text in expanded form. .setSummaryText("BigPicture style is used to show large image.") //set different title in expanded mode. .setBigContentTitle(null) //set different large icon in expanded mode. .bigLargeIcon(null); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher_round) .setContentTitle(title) .setContentText(text) //Set the large icon in the notification. .setLargeIcon(bitmap) //set Big picture template .setStyle(style) .build(); mNotificationManagerCompat.notify(notificationId, notification); } private void createInboxNotification(String title, String text, int notificationId) { //removes all previously shown notifications. mNotificationManagerCompat.cancelAll(); String line1 = "This is line1 "; String line2 = "This is line2 "; String line3 = "This is line3 "; String line4 = "This is line4 "; String line5 = "This is line5 "; String line6 = "This is line6 "; String line7 = "This is line7 "; NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle() //To add n lines, call it n times .addLine(line1) .addLine(line2) .addLine(line3) .addLine(line4) .addLine(line5) .addLine(line6) .addLine(line7) //needed if an app sends notification from multiple sources(accounts). .setSummaryText("InboxStyle") //set different title in expanded mode. .setBigContentTitle(null); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher_round) .setContentTitle(title) .setContentText("It is used for notifications includes a list of (up to 5) strings.") //set inbox style in notification .setStyle(style) .build(); mNotificationManagerCompat.notify(notificationId, notification); } private void createActionNotification(String title, String text, int notificationId) { //removes all previously shown notifications. mNotificationManagerCompat.cancelAll(); Intent intent = new Intent(getApplicationContext(), Receiver.class); intent.setAction("ACTION_CANCEL"); //passing notificationId to receiver class through intent intent.putExtra("id", notificationId); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); //action fire on click of notification Dismiss action button. NotificationCompat.Action actionDismiss = new NotificationCompat.Action.Builder(0, "Dismiss", pendingIntent) .build(); //action fire on click of notification Delete action button. NotificationCompat.Action actionDelete = new NotificationCompat.Action.Builder(0, "Delete", pendingIntent) .build(); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher_round) .setContentTitle(title) .setContentText(text) .setStyle(new NotificationCompat.BigTextStyle().bigText("This is an example of BigTextStyle notification with action.")) //Add actions Dismiss and Delete to this notification. .addAction(actionDismiss) .addAction(actionDelete) .build(); mNotificationManagerCompat.notify(notificationId, notification); } private void createNotificationChannel() { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is new and not in the support library if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //Channel name CharSequence name = "Important_mail_channel"; //Channel description String description = "This channel will show notification only to important people"; //The importance level you assign to a channel applies to all notifications that you post to it. int importance = NotificationManager.IMPORTANCE_DEFAULT; //Create the NotificationChannel NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); //Set channel description channel.setDescription(description); // Register the channel with the system; you can't change the importance // or other notification behaviors after this NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } } }