---
title: "Notification example in Android"  
description: "Android provides notification for user information alert. It can toast notification or push notification."  
author: "Manoj Pandey"  
published: 2015-08-21  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1840/notification-example-in-android  
category: "android"  
tags: ["android", "android alertdialog"]  
reading_time: 3 minutes  

---

# Notification example in Android

[Android](https://www.mindstick.com/articles/1567/navigation-drawer-implementation-in-android) provides notification for user information alert. It can toast notification or [push notification](https://www.mindstick.com/forum/23082/windows-phone-7-push-notification-when-the-app-is-not-running). A notification is a [message](https://www.mindstick.com/interview/682/which-are-the-various-message-map-macros) you can display to the user outside of [your application](https://answers.mindstick.com/qa/95487/how-do-you-troubleshoot-when-your-application-link-is-not-responding)'s normal UI. When you tell the [system](https://yourviews.mindstick.com/view/87103/allahabad-high-court-bulldozed-the-islamic-madrasa-system-in-up) to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-[controlled](https://yourviews.mindstick.com/view/81837/has-pakistan-controlled-corona-pandemic) areas that the user can view at any time.\

See toast notification

![Notification example in Android](https://www.mindstick.com/mindstickarticle/f535c62e-24dd-4d88-ade0-a96629f60511/images/c86cfc82-11ec-4344-9f0d-44f896eb5d5e.png)

See the push notification

![Notification example in Android](https://www.mindstick.com/mindstickarticle/f535c62e-24dd-4d88-ade0-a96629f60511/images/706743e9-7bd1-4ebb-9640-dfd6d4a4ea3a.png)

##### Here I am creating a sample of push notification. I hope it will be helpful for

##### android users. Follow below steps to create sample

##### \
1. Create an android project in eclipse or android studio.

##### \
2. Create an activity which show on tap push notification and add this activity on

##### manifest file

```
package com.example.androidnotification; import android.app.Activity;import android.app.NotificationManager;import android.os.Bundle;
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);
            }

}
```

3. Xml [layout](https://www.mindstick.com/articles/12853/android-layout-and-its-type) for My Notification [activity](https://www.mindstick.com/forum/23119/how-to-save-activity-state-in-android)

```
<?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. Xml layout for Main Activity

```
<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"    tools:context=".MainActivity" >     <Button        android:id="@+id/btnNotif"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Pop up Noification" /> </RelativeLayout>
```

\

5. Add following code in MainActivity.class

```
package com.example.androidnotification; import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;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;     // notification id     static int notificationID = 100;     int numMessagesOne = 0;      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();                     // displayNotificationOne();                }           });      }      @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;     }      // For show notification method     public void displayMyNotification() { NotificationCompat.Builder builder = new NotificationCompat.Builder(                     getApplicationContext());            // set main title       builder.setContentTitle("Android Push Notification");            // set notification text     builder.setContentText("Welcome in Mindstick Developer Section");            // 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);            // set number of messages           builder.setNumber(++numMessagesOne);          notificationManager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);            // A PendingIntent specifies an action to take in the           // futurePendingIntent 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());      } }
```

Now run your application.

![Notification example in Android](https://www.mindstick.com/mindstickarticle/f535c62e-24dd-4d88-ade0-a96629f60511/images/e66862f3-8be7-4092-bda7-5385ab459592.png)

---

Original Source: https://www.mindstick.com/articles/1840/notification-example-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
