Quick Link

Android Notifications Tutorial

1- Android Notification

A notification is a message you can display to the user outside of your application's normal UI. When you tell the system 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 areas that the user can view at any time.

2- Example

Create Android project - AndroidNotification
Drag and drop a Button to interface. When you click on this button, a message will be posted on the system.
  • ID: button
  • Text: Click for Notification
  • Properties:
    • onClick: notiButtonClicked
  • MainActivity.java
package org.o7planning.androidnotification;
import android.app.Notification;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class MainActivity extends Activity {

   private NotificationCompat.Builder notBuilder;

   private static final int MY_NOTIFICATION_ID = 12345;

   private static final int MY_REQUEST_CODE = 100;

   protected void onCreate(Bundle savedInstanceState) {


       this.notBuilder = new NotificationCompat.Builder(this);

       // The message will automatically be canceled when the user clicks on Panel



   public void notiButtonClicked(View view)  {

       // --------------------------
       // Prepare a notification
       // --------------------------

       this.notBuilder.setTicker("This is a ticker");

       // Set the time that the event occurred.
       // Notifications in the panel are sorted by this time.
       this.notBuilder.setWhen(System.currentTimeMillis()+ 10* 1000);
       this.notBuilder.setContentTitle("This is title");
       this.notBuilder.setContentText("This is content text ....");

       // Create Intent
       Intent intent = new Intent(this, MainActivity.class);

       // PendingIntent.getActivity(..) will start an Activity, and returns PendingIntent object.
       // It is equivalent to calling Context.startActivity(Intent).
       PendingIntent pendingIntent = PendingIntent.getActivity(this, MY_REQUEST_CODE,
                              intent, PendingIntent.FLAG_UPDATE_CURRENT);


       // Get a notification service (A service available on the system).
       NotificationManager notificationService  =

       // Builds notification and issue it

       Notification notification =  notBuilder.build();
       notificationService.notify(MY_NOTIFICATION_ID, notification);


Running app: