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. 

15 comments:

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

    ReplyDelete
  2. Learn data visualization and analytics through a tableau developer course designed to build real-world dashboard and reporting skills.

    ReplyDelete
  3. The best power bi course helps you master data visualization, DAX, and real-world analytics skills step by step.

    ReplyDelete
  4. This tableau developer course equips you with hands-on skills to design interactive dashboards and insightful data visualizations.

    ReplyDelete
  5. The best power bi course helps you master data visualization, modeling, and dashboard creation with real-world projects.

    ReplyDelete
  6. Accelerate your career with hands-on salesforce dev training focused on real-world development skills and best practices.

    ReplyDelete
  7. Learn power bi classes online designed to help you master data visualization, dashboards, and reporting with real-world projects.

    ReplyDelete
  8. Learn data visualization from scratch with tableau course online designed for beginners and professionals alike.

    ReplyDelete
  9. Advance your skills with salesforce dev training designed for real-world development and hands-on practice.

    ReplyDelete
  10. Boost your CRM expertise with salesforce administrator classes designed for real-world business needs.

    ReplyDelete
  11. salesforce administrator classes
    provide hands-on training to manage users, automate workflows, and customize Salesforce for business needs.

    ReplyDelete
  12. Learn power bi classes online
    to master data visualization, dashboards, and real-time analytics from industry experts.

    ReplyDelete
  13. Great explanation! Strong data modeling class
    makes data accurate, organized, and easy to use across applications.

    ReplyDelete
  14. Boost your career with our comprehensive power bi live training designed to equip you with hands-on Salesforce development skills. Learn Apex, Visualforce, Lightning components, and real-world project experience to become a certified Salesforce developer.

    ReplyDelete
  15. Very helpful! Master dell boomi online training
    cloud integration with hands-on training and build job-ready enterprise integration expertise.

    ReplyDelete