Monday 27 August 2012

Status bar Notification in Android

In this tutorial, we will deal with making a simple Status Bar Notification in Android. We will implement a sample application having two activities Main and Content. Main is the default activity which is launched when the activity starts. Content is launched when we click on the notification.

First of all declare the two activities in the manifest.

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    <activity
        android:name=".Main"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".TestActivity" />
</application>
Now we will code the default launcher activity Main

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // click listener for the button to start Notification
    Button btnNotify = (Button) findViewById(R.id.button1);
    btnNotify.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            createNotification();

        }
    });
}

private void createNotification() {

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    int unique_id = 145558;
    Intent nintent = new Intent();
    nintent.setClass(this, TestActivity.class);
    PendingIntent pin = PendingIntent.getActivity(getApplicationContext(),
            0, nintent, 0);
    String title = "Notification";
    String body = "This is a new notification";
    Notification n = new Notification(R.drawable.ic_launcher, body,
            System.currentTimeMillis());
    n.contentIntent = pin;
    n.setLatestEventInfo(getApplicationContext(), title, body, pin);

    n.defaults = Notification.DEFAULT_ALL;
    nm.notify(unique_id, n);

}

The layout main.xml will have a button alone and will be like this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <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="Show Notification" />


</RelativeLayout>
The activity Content which is to loaded when we click on the notification will be like this.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText("Notification is the source of this Activity");
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.cancel(145558);
}
the xml layout test.xml will have a TextView and is shown below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dip"
        android:text="This activity is launched from the notification" />

</LinearLayout>
And finally we are done. 

1 comment:

  1. Thanks ...simple and to the point !!
    Can we send notifications at a certain time automatically?

    ReplyDelete