How to create Status Bar Notification in Android

A status bar notification is a nice way to alert the user about some update from your application, when running in background, specially when you require a response from the user for the same. It adds an icon to the system's status bar along with an optional ticker-text message and an expanded message in the Notifications window. When the user selects the expanded notification message, Android fires an Intent that is defined by the notification which is usually used to launch an Activity. In this article, I will show you how we can implement this notification system in our application.

To demonstrate how to use status bar notification we will create a layout which will have a textview and button. The button will be used to launch the status bar notification. Create a new Android project with the following properties:

  • Project Name – Status Bar Notification
  • Name of the Application – Status Bar Notification
  • Package name – com.botskool.StatusBarNotification
  • Activity Name – StatusBarNotificationActivity

Create a new XML file in res/layout/ and name it as main.xml and copy the following code to it:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Click on the button below to see the Status Bar Notification"
    />
<Button 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" 
	android:text="Show Status Bar Notification" 
	android:id="@+id/statusbarbutton" 
	/>
</LinearLayout>

Our application consists of a textview and a button. Whenever the user clicks on the button a notification will be displayed to him/her. Next, add the following code to StatusBarNotificationActivity.java:  

package com.botskool.StatusBarNotification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class StatusBarNotificationActivity extends Activity {
	
	private static final int NOTIFICATION_ID = 1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        /** Define configuration for our notification */
        int icon = R.drawable.icon;
        CharSequence tickerText = "This is a sample notification";
        long when = System.currentTimeMillis();
        Context context = getApplicationContext();
        CharSequence contentTitle = "Sample notification";
        CharSequence contentText = "This notification has been generated as a result of button click.";
        Intent notificationIntent = new Intent(this, StatusBarNotificationActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        
        /** Initialize the Notification using the above configuration */
        final Notification notification = new Notification(icon, tickerText, when);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        /** Retrieve reference from NotificationManager */
        String ns = Context.NOTIFICATION_SERVICE;
        final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        
        /** Listener for click event of the button */
        Button statusbarnotify = (Button) findViewById(R.id.statusbarbutton);
        statusbarnotify.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {            	
                mNotificationManager.notify(NOTIFICATION_ID, notification);
            }
        });   
    }    
}

Here, first we define the various details of our notification message. We define an icon and ticker message with the help of icon and tickerText variables respectively for the notification to be displayed in system tray. For expanded view of our notification message which is displayed in Notifications window we have defined title and message body by using contentTitle and contentText variables respectively. The variable when stores the time of creation of the notification.

Next, we define an object of PendingIntent which will be fired when our notification is selected. We initialize an instance of Notification class with these settings by using the constructor of this class and its setLatestEventInfo().

After defining our notification object we need to retrieve a reference to NotificationManager, which is a system service that manages all notifications. We do so by using getSystemService() method.

Finally, we bind ourselves to the on-click listener event of the button which when clicked launches the notification with the help of notify() method.

Thats it! Now compile and run the Android application. The app will look like this:

If we click on the button the notification appears in the system tray along with the ticker message as shown below:

The expanded view of the notification message is shown below:

Comments

Member since:
7 August 2017
Last activity:
6 weeks 1 day

kanye west shoes

michael kors bags

ua shoes

ray ban erika

prada handbags

payless shoes online

official kate spade website

coach outlet online

pandora rings

pandoracharms

pandora rings uk

Coach Sunglasses Outlet

balenciaga outlet

Engagement Rings - Official

Nike Air Max 180

official michael kors

Ray Ban sunglasses

shoes sale

Nike Air Max

Air Max 180

burberry outlet

Dreamland Jewelry - Official

birkenstock sandals

birkenstock

Nike Air Max Couple

coach outlet online

prada outlet online

louis vuitton outlet online

Nike Air Jordan

gel kayano

official michael kors

michael kors outlet bags

Hugo Boss Store

payless shoes

Premier Jewelry - Official

ray ban outlet

Coach Handbags

pandora sale

coach watches

nike store

football jerseys

louis vuitton outlet online

coach outlet online sale

Add Your Link Free

backlink

louis vuitton us

michael kors

Nike Air Jordan Enfants

michael kors

Michael Kors Handbags Outlet Sale

kate spade outlet online

Green Cleaned

ugg boots

sunglasses

Nike Blazer Femmes

evbid

Wine

coach outlet

birkenstock outlet

Burberry Australia

payless near me

Chanel Outlet

north face sale

Nike Air Max

Prada Shoes - Official

michael kors outlet

ray ban wayfarer sunglasses

Oakley Sunglasses

puma sneakers outlet

payless shoes

nike free shoes

diesel watches

burberry outlet canada

prada sale

puma sneakers sale

louis vuitton outlet

Air Max 180 Homme

Jewelry Armoire - Official

coach handbags sale

Hermes Outlet

timberland shoes

Nike Air Max 2017

coachs outlet

louis vuitton outlet online

Nike Jordans

football jerseys

burberry outlet sale

Nike Free 3.0

michael kors outlet online

ray ban wayfarer

ray ban sunglasses

Rolex Watch

michael kors

michael kors

nike huarache shoes

ray ban sale

Adamson

pandora necklaces

kate spade italy

coach australia

pandora uk

pandora uk

swatch watch

michael kors watches

michael kors

rayban prescription sunglasses

Brighton Jewelry - Official

prada outlet online

Nike Air Force

michael kors outlet

fulam shoes

softball jerseys

Nike Air Max

balenciaga us

Air Max 1 Homme

michael kors

Watches

Air Jordan Enfant

chrome store canada

sunglasses store

coach outlet

michael kors outlet

Nike Free Run

mk outlet online

michael kors outlet

the beat

seiko watches

michael kors

Lifestyle

Air Max 90 Femme

hermes outlet

Nike Air Jordan enfants

hermes outlet

prada purses

kate spade outlet

nike jordan shoes

me adc8.7