How to create Status Bar Notification in Android
A status bar notification is a nice way to alert the user about some update from your application, when running in background, specially when you require a response from the user for the same. It adds an icon to the system's status bar along with an optional ticker-text message and an expanded message in the Notifications window. When the user selects the expanded notification message, Android fires an Intent that is defined by the notification which is usually used to launch an Activity. In this article, I will show you how we can implement this notification system in our application.
To demonstrate how to use status bar notification we will create a layout which will have a textview and button. The button will be used to launch the status bar notification. Create a new Android project with the following properties:
- Project Name – Status Bar Notification
- Name of the Application – Status Bar Notification
- Package name – com.botskool.StatusBarNotification
- Activity Name – StatusBarNotificationActivity
Create a new XML file in res/layout/ and name it as main.xml and copy the following code to it:
<?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="Click on the button below to see the Status Bar Notification" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Status Bar Notification" android:id="@+id/statusbarbutton" /> </LinearLayout>
Our application consists of a textview and a button. Whenever the user clicks on the button a notification will be displayed to him/her. Next, add the following code to StatusBarNotificationActivity.java:
package com.botskool.StatusBarNotification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class StatusBarNotificationActivity extends Activity { private static final int NOTIFICATION_ID = 1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /** Define configuration for our notification */ int icon = R.drawable.icon; CharSequence tickerText = "This is a sample notification"; long when = System.currentTimeMillis(); Context context = getApplicationContext(); CharSequence contentTitle = "Sample notification"; CharSequence contentText = "This notification has been generated as a result of button click."; Intent notificationIntent = new Intent(this, StatusBarNotificationActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); /** Initialize the Notification using the above configuration */ final Notification notification = new Notification(icon, tickerText, when); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); /** Retrieve reference from NotificationManager */ String ns = Context.NOTIFICATION_SERVICE; final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); /** Listener for click event of the button */ Button statusbarnotify = (Button) findViewById(R.id.statusbarbutton); statusbarnotify.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { mNotificationManager.notify(NOTIFICATION_ID, notification); } }); } }
Here, first we define the various details of our notification message. We define an icon and ticker message with the help of icon and tickerText variables respectively for the notification to be displayed in system tray. For expanded view of our notification message which is displayed in Notifications window we have defined title and message body by using contentTitle and contentText variables respectively. The variable when stores the time of creation of the notification.
Next, we define an object of PendingIntent which will be fired when our notification is selected. We initialize an instance of Notification class with these settings by using the constructor of this class and its setLatestEventInfo().
After defining our notification object we need to retrieve a reference to NotificationManager, which is a system service that manages all notifications. We do so by using getSystemService() method.
Finally, we bind ourselves to the on-click listener event of the button which when clicked launches the notification with the help of notify() method.
Thats it! Now compile and run the Android application. The app will look like this:
If we click on the button the notification appears in the system tray along with the ticker message as shown below:
The expanded view of the notification message is shown below: