Tuesday, September 6, 2011

Custom Notifications in Notification Bar in Android

Following Example will illustrate on Creating and sending Custom Notifications in Notification bar and clearing the created notifications too.

The Documents regarding the Notification Manager and Notification bar can be found here and here.
I'm using this concepts and here is the working example for the same.

Following is the basic screen shot for the application.


On Pressing the "Trigger Notification" button an Notification will be seen in the Notification bar saying "One Notification Received!! " as follows:


Now, expand the Notification window by dragging, you will see the notification as follows,


Start with creating a new Android Project in Eclipse, and following is the layout and Activity code for the application.

Layout Code i.e. main.xml



<?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="Notification Sample, Click On Button To See A Sample Notification Above!!"/>
    
<Button android:text="Trigger Notification" 
android:id="@+id/btnNotification" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</Button>
<Button android:text="Clear Notification" 
android:id="@+id/btnClearNotification" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</Button>   
        
</LinearLayout>


Following is the Activity Code for the Application (NotificationExample.java):



package com.mayuri.notification.example;
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.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;


   public class NotificationExample extends Activity {   

Button btnNotification,btnClearNotification; 


int NOTFICATION_ID = 198990; 
NotificationManager nm = null;          
@Override    
public void onCreate(Bundle savedInstanceState) {        


super.onCreate(savedInstanceState);        
setContentView(R.layout.main);
        btnNotification = (Button) this.findViewById(R.id.btnNotification);
        btnNotification.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

nm = (NotificationManager)
getSystemService(NotificationExample.this.NOTIFICATION_SERVICE);
String MyText = "One Notification Received!!";
String TitleText = "Message From Mayuri";
String NotiText  = "Hope you liked My Example :-)";

Notification mNotification = new 
Notification(R.drawable.icon,MyText,System.currentTimeMillis());
Intent MyIntent = new Intent( getApplicationContext(), NotificationExample.class);

MyIntent.putExtra("extendedTitle", TitleText);
MyIntent.putExtra("extendedText" , NotiText);         


PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent,0);                    mNotification.setLatestEventInfo(getApplicationContext(),TitleText,NotiText, StartIntent);                                       
nm.notify(NOTFICATION_ID , mNotification );      
        }
});      
             
        btnClearNotification = (Button) this.findViewById(R.id.btnClearNotification);
        btnClearNotification.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (nm!=null){
nm.cancel(NOTFICATION_ID);
}
}
});      
    }
}



In the above example, on clicking the button, Clear Notification, will clear the notification from the Notification bar.


I hope my example was useful.


Happy Coding :-))


Mayuri

3 comments:

Prageeth said...

Hi Mayuri,

Thanx for this article, I need to know how to use this with a service,
I need to feed notification to users of my app via a web service while user is using the app, This should be done in the background without interrupting user's activities.

Can you please let me a way...

Thanx
BR

Mayuri Ruparel said...

Hi,

Try referring this, it might help!

http://catchmayuri.blogspot.in/2011/05/creating-remote-background-service-in.html

Safdar Ali said...

Is this code for the new android or for the old also? desktop alerting software

Animated Container in Flutter

Please check this flutter video tutorial for detailed example and implementation of Animated Container in Flutter.