The handy class below sends a notification to Android’s Notification Service as follows:

  • Creates a notification containing the number of alerts , a ticker message, and associated icon.
  • If parameter set, flashes the led in a chosen color.
  • If parameter set, vibrates the phone.

It is meant to be called periodically as alerts are required, e.g., from a thread that periodically wakes up and checks for alerts.

Keep in mind that you can control the led flashing and vibrating.  This is the case where the user has not viewed recent notifications and we only want to update the number of alerts listed in the icon per determining that more alerts are available, but do not want more flashing and vibrating (so we set the flashLed and vibrate parameters to false).  Remember that too much notification can be an annoyance to users.

public class NotifierHelper implements IDebugSwitch {
    private static final int NOTIFY_1 = 0x1001;
    public static void sendNotification(Activity caller, Class<?> activityToLaunch, String title, String msg, int numberOfEvents, boolean flashLed, boolean vibrate) {
        NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);

        // Create this outside the button so we can increment the number drawn over the notification icon.
        // This indicates the number of alerts for this event.
        final Notification notify = new Notification(R.drawable.android_32, "", System.currentTimeMillis());

        notify.icon = R.drawable.android_32;
        notify.tickerText = "New Alerts from Your App!";
        notify.when = System.currentTimeMillis();
        notify.number = numberOfEvents;
        notify.flags |= Notification.FLAG_AUTO_CANCEL;

        if (flashLed) {
        // add lights
            notify.flags |= Notification.FLAG_SHOW_LIGHTS;
            notify.ledARGB = Color.CYAN;
            notify.ledOnMS = 500;
            notify.ledOffMS = 500;
        }

        if (vibrate) {
            // add vibs
            if (debug) Log.i("DEBUG", ">>>>> ViBrAtInG. >>>>>");
            notify.vibrate = new long[] {100, 200, 200, 200, 200, 200, 1000, 200, 200, 200, 1000, 200};
        }

        Intent toLaunch = new Intent(caller, activityToLaunch);
        PendingIntent intentBack = PendingIntent.getActivity(caller, 0, toLaunch, 0);

        notify.setLatestEventInfo(caller, title, msg, intentBack);
        notifier.notify(NOTIFY_1, notify);
    }

    public static void clear(Activity caller) {
        NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);
        notifier.cancelAll();
    }
}

Note the use of IDebugSwitch interface.

In the near future, I will post how to write a simple notification service that uses the NotificationHelper class. I will discuss topics such as disabling alert checking while notifications are being reviewed by the user, and determining that more alerts are available while current notifications are still pending (i.e., the user has not yet reviewed them). Therefore, we will not want to flash the led and vibrate the phone as we earlier touched in this post. Basically we will create a slick way to determine pending alerts.