<p>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.</p>



<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<amp-youtube layout="responsive" width="1200" height="675" data-videoid="r0do1KgYNDA" title="Android Notification Example"><a placeholder href="https://youtu.be/r0do1KgYNDA"><img src="https://i.ytimg.com/vi/r0do1KgYNDA/hqdefault.jpg" layout="fill" object-fit="cover" alt="Android Notification Example"></a></amp-youtube>
</div></figure>



<div class="wp-block-buttons aligncenter is-layout-flex wp-block-buttons-is-layout-flex">
<div class="wp-block-button"><a class="wp-block-button__link has-white-color has-text-color has-background" href="https://github.com/arunk7839/AndroidNotificationExample" style="background-color:#520599" target="_blank" rel="noreferrer noopener"><strong>DOWNLOAD CODE</strong></a></div>
</div>



<h3 class="p1 wp-block-heading"><span style="color: #000080;"><strong>Notification</strong></span></h3>



<p class="p1">A Notifications provide short, timely information about events happened in the application, even if the application is not running.</p>



<h3 class="p1 wp-block-heading"><span style="color: #000080;"><strong>Creating a Basic Notification</strong></span></h3>



<p class="p1">A notification in its most basic and compact form (or collapsed form) displays an icon, a title, and a small amount of content text.</p>



<figure class="wp-block-image"><img src="https://c1ctech.com/wp-content/uploads/2020/08/Screen-Shot-2020-08-27-at-4.25.26-PM.png" alt="Screen Shot 2020-08-27 at 4.25.26 PM" class="wp-image-2065"/></figure>



<h4 class="p1 wp-block-heading"><span style="color: #0000ff;"><strong>Setting the notification content</strong></span></h4>



<p>To create a basic notification, we can set the notification content with the following properties :</p>



<pre class="wp-block-preformatted">Notification notification = new NotificationCompat.Builder(this, <span style="color: #0000ff;"><strong>CHANNEL_ID</strong></span>)
 <span style="color: #008000;"><strong> .setSmallIcon(R.mipmap.ic_launcher_round)
 .setContentTitle(title)
 .setContentText(text)
 .setPriority(NotificationCompat.PRIORITY_DEFAULT)</strong></span>
 .build();</pre>



<p class="p1"><span style="color: #0000ff;"><strong>setSmallIcon(): </strong><span style="color: #000000;">S</span><span style="color: #000000;"><i><span style="color: #000000;">et</span> the <strong><span style="color: #008000;">small icon</span></strong> in notification. This is the only required property and it ;appears in the status bar.</i></span></span></p>



<figure class="wp-block-image"><img src="https://c1ctech.com/wp-content/uploads/2020/08/Screen-Shot-2020-08-26-at-9.45.38-PM.png" alt="Screen Shot 2020-08-26 at 9.45.38 PM" class="wp-image-2016"/></figure>



<p class="p1"><span style="color: #0000ff;"><strong>setContentTitle(): </strong><span style="color: #000000;"><i>Set the <strong><span style="color: #008000;">title</span></strong> of the notification.</i></span></span></p>



<p class="p1"><span style="color: #0000ff;"><strong>setContentText(): </strong><span style="color: #000000;"><i>Set the <strong><span style="color: #008000;">text</span></strong> of the notification.</i></span></span></p>



<p class="p1"><span style="color: #0000ff;"><strong>setPriority(): </strong><span style="color: #000000;"><i>Set the <strong><span style="color: #008000;">priority</span></strong> 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)</i></span></span></p>



<p><span style="color: #000080;"><strong>Note:</strong></span> <span style="color: #008000;"><strong>CHANNEL_ID</strong></span> is required for Android 8.0 (API level 26) and higher, but is ignored by older versions.</p>



<h4 class="p1 wp-block-heading"><span style="color: #0000ff;"><strong>Creating a notification channel ;</strong></span></h4>



<p class="p1">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.</p>



<p>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.</p>



<p>On devices running Android 7.1 (API level 25) and lower, each app only has one channel.</p>



<p class="p1">To create a notification channel, follow these steps:</p>



<ol class="ol1 wp-block-list"><li class="li1">Construct a ;<span style="color: #008000;"><strong><span class="s1">NotificationChannel</span></strong></span> ;object with a unique channel ID, a user-visible name, and an importance level.</li><li class="li1">Optionally, specify the description that the user sees in the system settings with ;<span style="color: #008000;"><strong><span class="s1">setDescription()</span></strong></span>.</li><li class="li1">Register the notification channel by passing it to ;<strong><span style="color: #008000;"><a style="color: #008000;" href="https://developer.android.com/reference/android/app/NotificationManager#createNotificationChannel(android.app.NotificationChannel)"><span class="s1">createNotificationChannel()</span></a></span></strong>. Use <span style="color: #008000;"><strong><a style="color: #008000;" href="https://developer.android.com/reference/android/app/NotificationManager#createNotificationChannels(java.util.List&;lt;android.app.NotificationChannel&;gt;)"><span class="s1">createNotificationChannels()</span></a></strong></span> ; to create multiple notification channels in a single operation.</li></ol>



<pre class="wp-block-preformatted"><strong><span style="color: #008000;">//Unique channel ID</span></strong>
final String CHANNEL_ID = "Important_mail_channel";

private void createNotificationChannel() {

 <strong><span style="color: #008000;">// Create the NotificationChannel, but only on API 26+ because</span></strong>
<strong><span style="color: #008000;"> // the NotificationChannel class is new and not in the support library</span></strong>
 if (Build.VERSION.SDK_INT >;= Build.VERSION_CODES.O) {

 <span style="color: #008000;"><strong>//Channel name</strong></span>
 CharSequence name = "Important_mail_channel";

 <span style="color: #008000;"><strong> //Channel description</strong></span>
 String description = "This channel will show notification only to important people";

 <strong><span style="color: #008000;">//The importance level you assign to a channel applies to all notifications that you post to it.</span></strong>
 int importance = NotificationManager.IMPORTANCE_DEFAULT;

 <strong><span style="color: #008000;">//Create the NotificationChannel</span></strong>
 NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);

 <span style="color: #008000;"><strong>//Set channel description</strong></span>
 channel.setDescription(description);

 <strong><span style="color: #008000;">// Register the channel with the system; you can't change the importance</span></strong>
<strong><span style="color: #008000;"> // or other notification behaviors after this</span></strong>
 NotificationManager notificationManager = getSystemService(NotificationManager.class);
 notificationManager.createNotificationChannel(channel);
 }
}</pre>



<p>Notification settings for <strong>NotificationTest</strong> app and one of its channel <strong>(Important_mail_channel).</strong></p>



<h4 class="wp-block-heading"><img class="alignnone wp-image-2026" src="https://c1ctech.com/wp-content/uploads/2020/08/Screenshot_1598511682.png" alt="Screenshot_1598511682" width="323" height="574"> ;  ;  ; <img class="alignnone wp-image-2028" src="https://c1ctech.com/wp-content/uploads/2020/08/Screenshot_1598511690.png" alt="Screenshot_1598511690" width="324" height="575"></h4>



<h4 class="wp-block-heading"> ;</h4>



<h4 class="p1 wp-block-heading"><span style="color: #0000ff;"><strong>Setting the notification&#8217;s tap action</strong></span></h4>



<p class="p1">To perform some operation on notification&#8217;s tap, you must specify a content intent defined with a <span style="color: #008000;"><strong><a style="color: #008000;" href="https://developer.android.com/reference/android/app/PendingIntent"><span class="s1">PendingIntent</span></a></strong></span> ;object and pass it to ;<strong><span style="color: #008000;"><a style="color: #008000;" href="https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setContentIntent(android.app.PendingIntent)"><span class="s1">setContentIntent()</span></a>.</span></strong></p>



<p class="p1">The following snippet shows how to create a basic intent to open an url when the user taps the notification:</p>



<pre class="wp-block-preformatted"><span style="color: #008000;"><strong>//open the url when user taps the notification</strong></span>
Intent <strong>intent</strong> = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.c1ctech.com/"));
PendingIntent <strong>pendingIntent</strong> = PendingIntent.<strong>getActivity</strong>(this, 0, <strong>intent</strong>, 0);

Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
 .setSmallIcon(R.mipmap.ic_launcher_round)
 .setContentTitle(title)
 .setContentText(text)
 .setPriority(NotificationCompat.PRIORITY_DEFAULT)
 <strong><span style="color: #008000;">// Set the intent that will fire when the user taps the notification</span></strong>
 <strong>.setContentIntent(pendingIntent)</strong>
 <span style="color: #008000;"><strong>//removes the notification when the user taps it</strong></span>
 .setAutoCancel(true)
 .build();</pre>



<h4 class="p1 wp-block-heading"><span style="color: #0000ff;"><strong>Show the notification</strong></span></h4>



<p class="p1">To make the notification appear, call ;<strong><span class="s1" style="color: #008000;">NotificationManagerCompat.notify()</span></strong>, passing it a <span style="color: #008000;"><strong>unique ID</strong></span> for the notification and the <strong><span style="color: #008000;">Notification </span></strong><span style="color: #000000;">object</span>.</p>



<pre class="wp-block-preformatted">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)
 <strong><span style="color: #008000;">//Set the intent that will fire when the user taps the notification</span></strong>
 .setContentIntent(pendingIntent)
 .setAutoCancel(true)
 .build();
<span style="color: #008000;"><strong>//notificationId must be unique int for each notification</strong></span>
<span style="color: #000000;"><strong>mNotificationManagerCompat.notify(notificationId, notification);</strong></span></pre>



<h3 class="p1 wp-block-heading"><span style="color: #000080;"><strong>Android Notification Styles</strong></span></h3>



<p class="p1">By default, the notification&#8217;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 ;<span style="color: #008000;"><strong><span class="s1" style="color: #008000;"><a style="color: #008000;" href="https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setStyle(android.support.v4.app.NotificationCompat.Style)">setStyle()</a>.</span></strong></span></p>



<h4 class="p1 wp-block-heading"><span style="color: #0000ff;"><strong>BigText Style</strong></span></h4>



<p class="p1">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.</p>



<pre class="wp-block-preformatted">final String <span style="color: #0000ff;"><strong>CHANNEL_ID</strong></span> = "Important_mail_channel";

private void createBigTextNotification(String title, String text, int notificationId) {

 Bitmap <strong>bitmap</strong> = BitmapFactory.decodeResource(getResources(),
 R.drawable.nature_img);

 NotificationCompat.BigTextStyle <strong>style</strong> = new NotificationCompat.BigTextStyle().<span style="color: #008000;"><strong>bigText</strong></span>(text + " used for generating large-format notifications that include a lot of text.")
 <span style="color: #008000;"><strong>//set different title in expanded mode.</strong> </span>
 <strong>.setBigContentTitle(null)
 <span style="color: #008000;">//needed if an app sends notification from multiple sources(accounts).</span>
 .setSummaryText("BigTextStyle");</strong>

 Notification notification = new NotificationCompat.Builder(this, <strong><span style="color: #0000ff;">CHANNEL_ID</span></strong>)
 .setSmallIcon(R.mipmap.ic_launcher_round)
 .setContentTitle(title)
 .setContentText(text + " used for generating large-format notifications that include a lot of text.")
 <span style="color: #008000;"><strong>//set BigTextStyle</strong></span>
 <strong><span style="color: #0000ff;">.setStyle(style)</span>
</strong> <span style="color: #008000;"><strong>//set the large icon in the notification.</strong></span>
 .setLargeIcon(<strong>bitmap</strong>)
 .build();

 mNotificationManagerCompat.notify(notificationId, notification);
}</pre>



<p><span style="color: #0000ff;"><strong>Collapsed Mode:</strong></span></p>



<figure class="wp-block-image"><img src="https://c1ctech.com/wp-content/uploads/2020/08/BigTextStyleNotification.png" alt="BigTextStyleNotification" class="wp-image-1991"/></figure>



<p><span style="color: #0000ff;"><strong>Expanded Mode:</strong></span></p>



<figure class="wp-block-image"><img src="https://c1ctech.com/wp-content/uploads/2020/08/BigTextStyleNotificationExpandable.png" alt="BigTextStyleNotificationExpandable" class="wp-image-1992"/></figure>



<h4 class="p1 wp-block-heading"><span style="color: #0000ff;"><strong>BigPicture Style</strong></span></h4>



<p>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.</p>



<pre class="wp-block-preformatted">final String <span style="color: #0000ff;"><strong>CHANNEL_ID</strong></span> = "Important_mail_channel";

private void createBigPictureNotification(String title, String text, int notificationId) {

 Bitmap <strong>bitmap</strong> = BitmapFactory.decodeResource(getResources(),
 R.drawable.nature_img);

 NotificationCompat.BigPictureStyle <strong>style</strong> = new NotificationCompat.BigPictureStyle()
 <span style="color: #008000;"><strong> //set big picture</strong></span>
 .bigPicture(bitmap)
<span style="color: #008000;"> <strong style="color: #008000;">//set the content text in expanded form.</strong></span>
 .setSummaryText("BigPicture style is used to show large image.")
<span style="color: #008000;"><strong> //set different title in expanded mode.</strong></span>
 .setBigContentTitle(null)
 <span style="color: #008000;"><strong>//set different large icon in expanded mode.</strong></span>
 .bigLargeIcon(null);

 Notification notification = new NotificationCompat.Builder(this, <strong><span style="color: #0000ff;">CHANNEL_ID</span></strong>)
 .setSmallIcon(R.mipmap.ic_launcher_round)
 .setContentTitle(title)
 .setContentText(text)
 .setLargeIcon(<strong>bitmap</strong>)
 <span style="color: #008000;"><strong>//set Big picture template</strong></span>
 <strong><span style="color: #0000ff;">.setStyle(style)</span></strong>
 .build();

 mNotificationManagerCompat.notify(notificationId, notification);
}</pre>



<p><span style="color: #0000ff;"><strong>Collapsed Mode:</strong></span></p>



<figure class="wp-block-image"><img src="https://c1ctech.com/wp-content/uploads/2020/08/BigPictureStyleNotification.png" alt="BigPictureStyleNotification" class="wp-image-1993"/></figure>



<p><span style="color: #0000ff;"><strong>Expanded Mode:</strong></span></p>



<figure class="wp-block-image"><img src="https://c1ctech.com/wp-content/uploads/2020/08/BigPictureStyleNotificationExpandable.png" alt="BigPictureStyleNotificationExpandable" class="wp-image-1994"/></figure>



<h4 class="p1 wp-block-heading"><span style="color: #0000ff;"><strong>Inbox Style</strong></span></h4>



<p>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.</p>



<pre class="wp-block-preformatted">final String <span style="color: #0000ff;"><strong>CHANNEL_ID</strong></span> = "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()
 <span style="color: #008000;"><strong>//To add n lines, call it n times</strong></span>
 .addLine(line1)
 .addLine(line2)
 .addLine(line3)
 .addLine(line4)
 .addLine(line5)
 .addLine(line6)
 .addLine(line7)
<strong><span style="color: #008000;"> //needed if an app send notification from multiple sources(accounts).</span></strong>
 .setSummaryText("InboxStyle")
<span style="color: #008000;"><strong> //set different title in expanded form.</strong></span>
 .setBigContentTitle(null);

 Notification notification = new NotificationCompat.Builder(this, <strong><span style="color: #0000ff;">CHANNEL_ID</span></strong>)
 .setSmallIcon(R.mipmap.ic_launcher_round)
 .setContentTitle(title)
 .setContentText("It is used for notifications includes a list of (up to 5) strings.")
<strong><span style="color: #008000;"> //set inbox style in notification</span></strong>
 .setStyle(style)
 .build();

 mNotificationManagerCompat.notify(notificationId, notification);
}</pre>



<p><span style="color: #0000ff;"><strong>Collapsed Mode:</strong></span></p>



<figure class="wp-block-image"><img src="https://c1ctech.com/wp-content/uploads/2020/08/InboxNotification-1.png" alt="InboxNotification" class="wp-image-1997"/></figure>



<p><span style="color: #0000ff;"><strong>Expanded Mode:</strong></span></p>



<figure class="wp-block-image"><img src="https://c1ctech.com/wp-content/uploads/2020/08/InboxNotificationExpandable.png" alt="InboxNotificationExpandable" class="wp-image-1998"/></figure>



<h3 class="p1 wp-block-heading"><span style="color: #000080;"><strong>Add Action Buttons</strong></span></h3>



<p>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.</p>



<p>The following code shows how to send a broadcast to a specific receiver (cancels the notification of specific notificationId) on click of action buttons <strong>Dismiss</strong> and <strong>Delete</strong>:</p>



<pre class="wp-block-preformatted">final String CHANNEL_ID = "Important_mail_channel";

private void createActionNotification(String title, String text, int notificationId) {

 Intent <strong>intent</strong> = new Intent(getApplicationContext(), Receiver.class);
 intent.setAction("ACTION_CANCEL");
 <strong><span style="color: #008000;">//passing notificationId to receiver class through intent</span></strong>
 intent.putExtra("id", notificationId);

 PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, <span style="color: #000000;"><strong>intent</strong></span>, PendingIntent.FLAG_UPDATE_CURRENT);
<span style="color: #008000;"><strong> //Action fire on click of notification Dismiss action button</strong></span>
 NotificationCompat.Action <strong>actionDismiss</strong> =
 new NotificationCompat.Action.Builder(0,
 "Dismiss", pendingIntent)
 .build();
<span style="color: #008000;"><strong> //Action fire on click of notification Delete action button</strong></span>
 NotificationCompat.Action <strong>actionDelete</strong> =
 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."))
<span style="color: #008000;"><strong> //Add actions Dismiss and Delete to this notification.</strong></span>
 <strong>.addAction(actionDismiss)
 .addAction(actionDelete)</strong>
 .build();

 mNotificationManagerCompat.notify(notificationId, notification);
}</pre>



<p>A BigTextStyle notification with two action buttons.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>




<p><span style="color: #0000ff;"><strong>Collapsed Mode:</strong></span></p>



<figure class="wp-block-image"><img src="https://c1ctech.com/wp-content/uploads/2020/08/ActionNotification.png" alt="ActionNotification" class="wp-image-2000"/></figure>



<p><span style="color: #0000ff;"><strong>Expanded Mode:</strong></span></p>



<figure class="wp-block-image"><img src="https://c1ctech.com/wp-content/uploads/2020/08/ActionNotificationExpandable.png" alt="ActionNotificationExpandable" class="wp-image-2001"/></figure>



<h3 class="wp-block-heading"> ;</h3>



<h3 class="wp-block-heading"><span style="color: #000080;"><strong>Creating New Project</strong></span></h3>



<p><strong>1</strong>. Create a new project <span style="color: #0000ff;"><strong>NotificationTest</strong></span> in <span style="color: #008000;"><strong>Android Studio</strong></span> ;from ;<span style="color: #008000;"><strong>File ⇒ New Project</strong>,</span> fill the project details and then click on <span style="color: #008000;"><strong>finish</strong></span>.</p>



<p><strong>2</strong>. Open <strong><span style="color: #008000;">activity_main.xml</span></strong> file and add the following code as shown below:</p>



<p><span style="color: #0000ff;"><strong>activity_main.xml</strong></span></p>



<pre class="wp-block-preformatted"><;?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>;</pre>



<p>The layout of the above <span style="color: #008000;"><strong>activity_main.xml</strong></span> file will look like this:</p>



<div class="wp-block-image"><figure class="aligncenter is-resized"><img src="https://c1ctech.com/wp-content/uploads/2020/08/Screenshot_1598358882.png" alt="Screenshot_1598358882" class="wp-image-2005" width="395" height="702"/></figure></div>



<p><strong>3</strong>. Create a new class <span style="color: #008000;"><strong>Receiver.java</strong></span> inside project package folder. Receiver class <strong><span style="color: #008000;">onReceive()</span></strong> will call on click of notification action buttons (Delete and Dismiss).</p>



<p><span style="color: #0000ff;"><strong>Receiver.java</strong></span></p>



<pre class="wp-block-preformatted">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) {

 <span style="color: #008000;"><strong>//get notificationId of notification using intent</strong></span>
 int id = intent.getIntExtra("id", 0);
 NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);

 <strong><span style="color: #008000;">//cancel the specific notificationId notification.</span></strong>
 notificationManagerCompat.cancel(id);
 }
 }
}</pre>



<p><strong>4</strong>. To register the user defined broadcast receiver (Receiver.java) inside <strong><span style="color: #008000;">AndroidManifest.xml </span></strong><span style="color: #000000;">file write the below code:</span></p>



<p><span style="color: #0000ff;"><strong>AndroidManifest.xml</strong></span></p>



<pre class="wp-block-preformatted"><;?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>;

 <span style="color: #008000;"><strong><;receiver android:name=".Receiver">;</strong></span>
<span style="color: #008000;"><strong> <;intent-filter>;</strong></span>
<span style="color: #008000;"><strong> <;category android:name="android.intent.categeory.DEFAULT"/>;</strong></span>
<span style="color: #008000;"><strong> <;/intent-filter>;</strong></span>
<span style="color: #008000;"><strong> <;/receiver>;</strong></span>
 <;/application>;

<;/manifest>;</pre>



<p><strong>5</strong>. Open <strong><span style="color: #008000;">MainActivity.java</span></strong> and add the below code:</p>



<p><span style="color: #0000ff;"><strong>MainActivity.java</strong></span></p>



<pre class="wp-block-preformatted">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) {

 <span style="color: #008000;"><strong>//removes all previously shown notifications.</strong></span>
 mNotificationManagerCompat.cancelAll();

 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
 R.drawable.nature_img);

 <strong><span style="color: #008000;">//open the url when user taps the notification</span></strong>
 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)
 <strong><span style="color: #008000;">//Set the intent that will fire when the user taps the notification</span></strong>
 .setContentIntent(pendingIntent)
 .setAutoCancel(true)
 .build();

 <strong><span style="color: #008000;">// notificationId is a unique int for each notification that you must define</span></strong>
 mNotificationManagerCompat.notify(notificationId, notification);

 }


 private void createBigTextNotification(String title, String text, int notificationId) {

 <span style="color: #008000;"><strong>//removes all previously shown notifications.</strong></span>
 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.")
 <span style="color: #008000;"><strong>//set different title in expanded mode.</strong></span>
 .setBigContentTitle(null)
 <strong><span style="color: #008000;">//needed if an app sends notification from multiple sources(accounts).</span></strong>
 .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.")
 <strong><span style="color: #008000;"> //set Big text template</span></strong>
 .setStyle(style)
 <strong><span style="color: #008000;"> //Set the large icon in the notification.</span></strong>
 .setLargeIcon(bitmap)
 .build();

 mNotificationManagerCompat.notify(notificationId, notification);
 }


 private void createBigPictureNotification(String title, String text, int notificationId) {

 <span style="color: #008000;"><strong>//removes all previously shown notifications.</strong></span>
 mNotificationManagerCompat.cancelAll();


 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
 R.drawable.nature_img);


 NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle()
 <strong><span style="color: #008000;">//set big picture</span></strong>
 .bigPicture(bitmap)
 <strong><span style="color: #008000;">//set the content text in expanded form.</span></strong>
 .setSummaryText("BigPicture style is used to show large image.")
 <strong><span style="color: #008000;">//set different title in expanded mode.</span></strong>
 .setBigContentTitle(null)
 <strong><span style="color: #008000;">//set different large icon in expanded mode.</span></strong>
 .bigLargeIcon(null);


 Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
 .setSmallIcon(R.mipmap.ic_launcher_round)
 .setContentTitle(title)
 .setContentText(text)
 <strong><span style="color: #008000;">//Set the large icon in the notification.</span></strong>
 .setLargeIcon(bitmap)
 <span style="color: #008000;"><strong>//set Big picture template</strong></span>
 .setStyle(style)
 .build();

 mNotificationManagerCompat.notify(notificationId, notification);

 }

 private void createInboxNotification(String title, String text, int notificationId) {

 <span style="color: #008000;"><strong>//removes all previously shown notifications.</strong></span>
 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()
 <strong><span style="color: #008000;"> //To add n lines, call it n times</span></strong>
 .addLine(line1)
 .addLine(line2)
 .addLine(line3)
 .addLine(line4)
 .addLine(line5)
 .addLine(line6)
 .addLine(line7)
 <strong><span style="color: #008000;">//needed if an app sends notification from multiple sources(accounts).</span></strong>
 .setSummaryText("InboxStyle")
 <strong><span style="color: #008000;">//set different title in expanded mode.</span></strong>
 .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.")
 <strong><span style="color: #008000;">//set inbox style in notification</span></strong>
 .setStyle(style)
 .build();

 mNotificationManagerCompat.notify(notificationId, notification);
 }

 private void createActionNotification(String title, String text, int notificationId) {

 <strong><span style="color: #008000;">//removes all previously shown notifications.</span></strong>
 mNotificationManagerCompat.cancelAll();


 Intent intent = new Intent(getApplicationContext(), Receiver.class);
 intent.setAction("ACTION_CANCEL");

 <strong><span style="color: #008000;">//passing notificationId to receiver class through intent</span></strong>
 intent.putExtra("id", notificationId);

 PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

 <strong><span style="color: #008000;"> //action fire on click of notification Dismiss action button.</span></strong>
 NotificationCompat.Action actionDismiss =
 new NotificationCompat.Action.Builder(0,
 "Dismiss", pendingIntent)
 .build();

 <strong><span style="color: #008000;">//action fire on click of notification Delete action button.</span></strong>
 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."))
 <span style="color: #008000;"><strong> //Add actions Dismiss and Delete to this notification.</strong></span>
 .addAction(actionDismiss)
 .addAction(actionDelete)
 .build();

 mNotificationManagerCompat.notify(notificationId, notification);
 }

 private void createNotificationChannel() {

 <strong><span style="color: #008000;">// Create the NotificationChannel, but only on API 26+ because</span></strong>
<strong><span style="color: #008000;"> // the NotificationChannel class is new and not in the support library</span></strong>
 if (Build.VERSION.SDK_INT >;= Build.VERSION_CODES.O) {

 <strong><span style="color: #008000;"> //Channel name</span></strong>
 CharSequence name = "Important_mail_channel";

 <strong><span style="color: #008000;">//Channel description</span></strong>
 String description = "This channel will show notification only to important people";

 <strong><span style="color: #008000;">//The importance level you assign to a channel applies to all notifications that you post to it.</span></strong>
 int importance = NotificationManager.IMPORTANCE_DEFAULT;

 <span style="color: #008000;"><strong>//Create the NotificationChannel</strong></span>
 NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);

 <strong><span style="color: #008000;">//Set channel description</span></strong>
 channel.setDescription(description);

 <strong><span style="color: #008000;">// Register the channel with the system; you can't change the importance</span></strong>
<strong><span style="color: #008000;"> // or other notification behaviors after this</span></strong>
 NotificationManager notificationManager = getSystemService(NotificationManager.class);
 notificationManager.createNotificationChannel(channel);
 }
 }
}</pre>


