Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Creating a Notification in the Status Bar

Author: Ian Darwin -- Published? true -- FormatLanguage: W

Problem:

You want to place a notification icon in the notice bar at the top of the screen, to call the user's attention to an event that occurred or to remind them of a service that is running in the background.

Solution:

Create a Notification object, provide it with a PendingIntent which wraps a real Intent for what to do when the user selects the Notification. At the same time you pass in the PendingIntent you also pass a title and text to be displayed in the notification area. You should set the AUTO_CANCEL flag unless you want to remove the notification from the notice bar manually. Finally, you find and ask the NotificationManager to display (notify) your Notification, associating with it an ID so that you can refer to it later, e.g to remove it.

Discussion:

Notifications are normally used from a running Service class to notify (hence the name) the user of some fact. Either an event has occurred (receipt of a message, loss of contact with a server, or whatever), or, you just want to remind the user that a long-running Service is still running.

Create a Notification object; the constructor takes an Icon id, the text to display briefly in the Notice bar, and the time at which the event occurred (timestamp in milliseconds). Before you can show the Notification you have to provide it with a PendingIntent for what to do when the user selects the Notification, and ask the NotificationManager to display your Notification.

The following shows doing the right thing in the wrong place; Notifications are normally shown from Services. This Recipe is just focusing on the Notification API.

public class Main 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);

                int icon = R.drawable.icon;  // Preferably a distinct icon

                // Create the notification itself
                String noticeMeText = getString(R.string.noticeMe);
                Notification n = 
                        new Notification(
                        icon, noticeMeText, System.currentTimeMillis());

                // And the Intent of what to do when user selects notification
                Context applicationContext = getApplicationContext();
                Intent notIntent = new Intent(this, NotificationTarget.class);
                PendingIntent wrappedIntent = 
                        PendingIntent.getActivity(this, 0, 
                        notIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

                // Condition the Notification
                String title = getString(R.string.title);
                String message = getString(R.string.message);
                n.setLatestEventInfo(applicationContext, title, 
                        message, wrappedIntent);
                n.flags |= Notification.FLAG_AUTO_CANCEL;

                // Now invoke the Notification Service
                String notifService = Context.NOTIFICATION_SERVICE;
                NotificationManager mgr = 
                        (NotificationManager) getSystemService(notifService);
                mgr.notify(NOTIFICATION_ID, n);
        }
}

The following is the file strings.xml:

<resources>
    <string name="app_name">NotificationDemo</string>
    <string name="hello">Hello World, Main!</string>
    <string name="noticeMe">Lookie Here!!</string>
    <string name="title">My Notification</string>
    <string name="message">This is my message</string>
    <string name="target_name">Notification Target</string>
    <string name="thanks">Thank you selecting the notification.</string>
</resources>

The "noticeMe" string appears briefly (a few seconds only) in the status bar:

Then the main view will appear;

When the user drags the status bar down, it expands to show the details, which includes the icons and the title and message strings (unless you are using a custom view, see below).

If you have set auto-clear, the notification will no longer appear in the status bar. If the user selects the notification box, the PendingIntent becomes current. Ours simply shows a simple Thank You notification. If the user clicks the Clear button, however, the Intent does not get run (even with Auto-Clear, which can leave you in a bit of a lurch).

TODO updating the status.

TODO Other flags, e.g., LED colors.

TODO Custom View.

See Also:

The official tutorial is at [1].

Download:

The source code for this project is in the Android Cookbook repository at http://github.com/IanDarwin/Android-Cookbook-Examples, in the subdirectory NotificationDemo.
No records found.