How to create an Android notification (“Notifications 101”)

Sending/displaying a notification is one of the easy things to do in Android. To create and display an Android notification, all you have to do is:

  • Create the title text (ticker text) to display in the status bar when the notification is shown.
  • Use an icon to show in the status bar after the notification goes away.
  • Create a view/text to show in the notification drawer.
  • Create a PendingIntent that is fired when the user taps the notification in the drawer (or on the lock screen).

If that sounds like a lot of work, it really isn’t. Based on the code in the book, Android Programming: The Big Nerd Ranch Guide, here’s a simple Android notification method I just created:

public void showNotification() {
    PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, ShowNotificationDetailActivity.class), 0);
    Resources r = getResources();
    Notification notification = new NotificationCompat.Builder(this)
            .setTicker(r.getString(R.string.notification_title))
            .setSmallIcon(android.R.drawable.ic_menu_report_image)
            .setContentTitle(r.getString(R.string.notification_title))
            .setContentText(r.getString(R.string.notification_text))
            .setContentIntent(pi)
            .setAutoCancel(true)
            .build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
}

This code uses the NotificationCompat.Builder to create a Notification. If you’re comfortable with Android, the only thing that might need explanation is the PendingIntent. You set this intent on a Notification, and it controls what happens when the user taps the notification that’s displayed in the status bar drawer or lock screen. Everything else is just a matter of setting strings and an icon. In this case I just use an Android system icon.

It’s worth noting that the Activity you send the user to with the PendingIntent is an activity that should have a view. Most likely, that view will show the detailed information regarding your notification. (In my case that activity is named ShowNotificationDetailActivity.) It’s very easy to create a simple activity and view with Android Studio, so I won’t bother showing that code here, but at a minimum you’ll need (a) a Java/Android Activity class and (b) a simple view, typically written as an XML file in the res/layout directory of your project.

That’s really all you have to do to send a notification in Android. This example doesn’t cover the logic needed to trigger the sending of the notification. You’ll probably want to do that with some sort of background Service, and I’ll cover that in a future Android tutorial.

Import statments

It may also help to know that you’ll need these Java/Android import statements to get this notification example working:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

Hopefully your IDE will help with those imports, but I thought I’d show them here to make it easier to use this notification code in your own Android project.