Saturday, 20 April 2013

Removing status bar notification from android application

Status bar notification used to show some important alert from in or outside the application in android. See Creating Simple notification is simple. This tutorial address the issue of removing notification from status bar while you are inside the application. Its easy to remove when you get involve with notification (like touch) but we will learn today how can we remove them without touch or involving with them Main concept run around notification id. You can remove any notification from anywhere if you notification id. So we need one data base to store id of notification while we generate this and later on remove notification


Steps 

Step 1) Required a simple user Interface with one button to generate notification and one to remove it. Create remove.xml 

<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=".NotificationRemove" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="Send Notification" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="79dp"
        android:text="Clear Notification" />

</RelativeLayout>

Notification
                                           
Step 2) Create a simple data base class DataBase.java to store the notification id

package com.notificationremoval;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBase extends SQLiteOpenHelper {

    public static final String tName = "tName";
    public static final String mNotiID = "mNotiID";

    public DataBase(Context context) {
        super(context, "temp.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String mNotifi = "create table noti(noti_id integer not null);";
        db.execSQL(mNotifi);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}


Step 3) Now create one Activity that Handle removal of notification


package com.notificationremoval;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class NotificationRemove extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remove);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
    }

    private void createAndGenerateNotifcation() {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Notification TItile").setContentText("Body");
        mBuilder.setAutoCancel(true);
        Intent resultIntent = new Intent();
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntent(resultIntent);
        int noti_id = (int) System.currentTimeMillis();
        insertNotifi(noti_id);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(noti_id, mBuilder.build());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            createAndGenerateNotifcation();
            break;
        case R.id.button2:
            RemoveAllNotification();
            break;
        }
    }

    public void RemoveAllNotification() {
        DataBase mBase = new DataBase(this);
        Cursor cur = mBase.getWritableDatabase().query("noti", null, null,
                null, null, null, null, null);
        cur.moveToFirst();
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        do {
            int id = cur.getInt(0);
            mNotificationManager.cancel(id);
        } while (cur.moveToNext());
        try {
            cur.close();
        } catch (Exception e) {
        }
    }

    public void insertNotifi(int id) {
        DataBase mBase = new DataBase(this);
        ContentValues cVales = new ContentValues();
        cVales.put("noti_id", id);
        SQLiteDatabase db = mBase.getWritableDatabase();
        long i = db.insert("noti", null, cVales);
        Log.i("Insert", String.valueOf(i));
        try {
            db.close();
        } catch (Exception e) {
        }
    }

}


Enjoy and keep commenting
Android News and source code