Sunday, 18 January 2015

How to clear notification in Android

Cancel the Notification that we started
 
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager Nmang = (NotificationManager) getApplicationContext()
.getSystemService(ns);
Nmang .cancel(getIntent().getExtras().getInt("notificationID"));

Put the above code in Notification Activity...
For examble.
public class NotificationView extends Activity
{

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.notification);
      String ns = Context.NOTIFICATION_SERVICE;
NotificationManager Nmang = (NotificationManager)                 
getApplicationContext() .getSystemService(ns);
Nmang.cancel(getIntent().getExtras().getInt("notificationID"));
    }
  protected void displayNotification()
{

  int notificationID = 100;
    NotificationManager mNotificationManager;
   int numMessages = 0;
  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this);
    mBuilder.setContentTitle("New Message");
mBuilder.setContentText("You've received new message.");
mBuilder.setTicker("New Message Alert!");
mBuilder.setSmallIcon(R.drawable.ic_launcher);  
    mBuilder.setNumber(++numMessages);
    Intent resultIntent = new Intent(this, NotificationView.class);
resultIntent.putExtra("notificationID", notificationID);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NotificationView.class);
    stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);   
    mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager)          
    getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(notificationID, mBuilder.build());
}