Version 1.8
Copyright © 2010, 2009 - 2011 Lars Vogel
10.11.2011
| Revision History | ||
|---|---|---|
| Revision 0.1 | 17.07.2010 | Lars Vogel |
| Created | ||
| Revision 0.2 - 1.8 | 19.07.2010 - 06.01.2012 | Lars Vogel |
| bug fixes and enhancements | ||
Table of Contents
The following assumes that you have already basic knowledge in Android development. Please check the Android development tutorial to learn the basics.
Android allows to put notification into the titlebar of your application. The user can open the notification bar and by selecting the notification the user can trigger another activity.
Notifications in Android are represented by the
Notification
class.
To create notifications you use the
NotificationManager
class which can be received from the Activity via the
getSystemService()
method.
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
You then configure you
Notification
class and create an
Intent
you will call the target
Activity.
// Create Notifcation Notification notification = new Notification(R.drawable.icon, "A new notification", System.currentTimeMillis()); // Cancel the notification after its selected notification.flags |= Notification.FLAG_AUTO_CANCEL; // notification.number += 1; // Specify the called Activity Intent intent = new Intent(this, NotificationReceiver.class);
You use then a
PendingIntent
to assign the Intent to the notification and finally assign it to the
NotificationManager.
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0); notification.setLatestEventInfo(this, "This is the title", "This is the text", activity); notificationManager.notify(0, notification);
A PendingIntent is a token that you give to another application (e.g. Notification Manager, Alarm Manager or other 3rd party applications), which allows this other application to use the permissions of your application to execute a predefined piece of code.
To perform a broadcast
via a
pending intent so get a PendingIntent
via
PendingIntent.getBroadcast(). To perform an activity via an
pending intent you receive the
activity via
PendingIntent.getActivity().
Create a new project "de.vogella.android.notificationmanager" with the Activity "CreateNotification". Create the following layout "result.xml".
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is the result activity opened from the notification" > </TextView> </LinearLayout>
Change "main.xml" to the following.
<?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" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="match_parent" android:onClick="createNotification" android:text="Create Notification" > </Button> </LinearLayout>
Create a new activity "NotificationReceiver" with the following coding. Don't forget to register the activity in the "AndroidManfest.mf".
package de.vogella.android.notificationmanager; import android.app.Activity; import android.os.Bundle; public class NotificationReceiver extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.result); } }
Change the activity "CreateNotification" to the following.
package de.vogella.android.notificationmanager; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.View; public class CreateNotification extends Activity {/** Called when the activity is first created. */@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void createNotification(View view) { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.icon, "A new notification", System.currentTimeMillis()); // Hide the notification after its selected notification.flags |= Notification.FLAG_AUTO_CANCEL; Intent intent = new Intent(this, NotificationReceiver.class); PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0); notification.setLatestEventInfo(this, "This is the title", "This is the text", activity); notification.number += 1; notificationManager.notify(0, notification); } }
Run your application and press the button. A new notification is created. If you select it your second activity will be displayed.

Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.
Eclipse RCP Training (German) Eclipse RCP Training with Lars Vogel
Android Tutorial Introduction to Android Programming
GWT Tutorial Program in Java and compile to JavaScript and HTML
Eclipse RCP Tutorial Create native applications in Java
JUnit Tutorial Test your application
Git Tutorial Put everything you have under distributed version control system