---
title: "Notifications in Android"  
description: "In this article I am explaining about Notifications in Android"  
author: "Manoj Pandey"  
published: 2015-03-04  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1628/notifications-in-android  
category: "android"  
tags: ["android", "broadcast", "service"]  
reading_time: 3 minutes  

---

# Notifications in Android

[Notification](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) is a [message](https://www.mindstick.com/interview/682/which-are-the-various-message-map-macros) that doesn’t appear inside the main UI of the app but as an icon in the notification area, including more details in the notification drawer.\

A notification is a message you can display to the user outside of your application's normal UI. Notifications are used to alert users on some events that requires their attention.

**Notifications alert the users through various forms:**

- Display a [status bar](https://www.mindstick.com/articles/98/using-progress-bar-status-bar-timer-control-in-csharp-dot-net) icon
- Flash the LED
- Vibrate
- Ringtones

You can also show **Toast** notification i.e.

Toast.makeText(getApplicationContext(), "My Message", Toast.LENGTH_LONG).show();

1. Create an [Android](https://www.mindstick.com/articles/1567/navigation-drawer-implementation-in-android) [Project](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful)
2. Add [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) in activity_main.xml

```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >     <Button        android:id="@+id/btnNotif"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Show Notification" /> </RelativeLayout> 3.      Create another layout for notification view  <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >     <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text=" Your notification test is successfull !"        android:textSize="18sp" /> </LinearLayout>
```

4. Now add following code in MainActivity.class

```
package com.example.androidnotification; import android.os.Bundle;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.support.v4.app.NotificationCompat;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button; public class MainActivity extends Activity {     NotificationManager notificationManager;     static int notificationID = 100;          Button btnMeaasge;      @Override     protected void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.activity_main);           btnMeaasge = (Button) findViewById(R.id.btnNotif);           btnMeaasge.setOnClickListener(new OnClickListener() {                 @Override                public void onClick(View v) {                     // TODO Auto-generated method stub                     displayMyNotification();                }           });      }      @Override     public boolean onCreateOptionsMenu(Menu menu) {           // Inflate the menu; this adds items to the action bar if            //it is present.           getMenuInflater().inflate(R.menu.main, menu);           return true;     }    // This method shows notification    public void displayMyNotification() {   NotificationCompat.Builder builder = new NotificationCompat.Builder(                     getApplicationContext());            // set main title           builder.setContentTitle("Demo Test");            // set notification text           builder.setContentText("You have received a message");            // set Sticker for alert message           builder.setTicker("New Message Alert!");            // set icon           builder.setSmallIcon(R.drawable.ic_launcher);           // Use default sound for notification           builder.setDefaults(Notification.DEFAULT_SOUND);           notificationManager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);                      // A PendingIntent specifies an action to take in the           // future           PendingIntent myIntent = PendingIntent.getActivity(this, 0,new Intent(  this, MyNotification.class), 0);                      // on notification click open myIntent           builder.setContentIntent(myIntent);      /* Update the existing notification using same notification ID */          notificationManager.notify(notificationID, builder.build());      }}
```

5. Create MyNotification [Activity](https://www.mindstick.com/forum/23119/how-to-save-activity-state-in-android) and paste following code

```
package com.example.androidnotification; import android.app.Activity;import android.app.NotificationManager;import android.os.Bundle;import android.widget.Toast; public class MyNotification extends Activity {      @Override     protected void onCreate(Bundle savedInstanceState) {           // TODO Auto-generated method stub           super.onCreate(savedInstanceState);           setContentView(R.layout.notif);                                      // Remove notification after open this activity        NotificationManager notificationManager = (NotificationManager)     getSystemService(getApplicationContext().NOTIFICATION_SERVICE);           notificationManager.cancel(MainActivity.notificationID);     } }
```

Now run [your Application](https://answers.mindstick.com/qa/95487/how-do-you-troubleshoot-when-your-application-link-is-not-responding)

![Notifications in Android](https://www.mindstick.com/mindstickarticle/55943cf9-b0a1-41fa-b0da-db74f507b928/images/efdea38f-ed61-4e4a-80fa-de2c8edb1305.png) ![Notifications in Android](https://www.mindstick.com/mindstickarticle/55943cf9-b0a1-41fa-b0da-db74f507b928/images/def7bbca-39f4-4912-9000-6495a69b92b8.png)

After click on notification

![Notifications in Android](https://www.mindstick.com/mindstickarticle/55943cf9-b0a1-41fa-b0da-db74f507b928/images/936f7f70-7d54-4ea2-a97c-cee70fa94956.png)

---

Original Source: https://www.mindstick.com/articles/1628/notifications-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
