Smartphones

Android Jelly Bean makes notifications even fancier

Learn how to use Android Jelly Bean's notification builder class to create impressive notifications that support intents, images, and previews.

Since Google released Android Honeycomb, the framework team has continued to work at making notifications useful and a thing of beauty. Google's latest and greatest foray into the world of smartphone operating systems, Jelly Bean, provides the most feature rich API for status notifications yet. Using the notification builder class, you can create impressive notifications that support intents, images, and previews all in a few lines of code. In this tutorial, we will do just that.

In order for the code to compile and run, you will need to update your SDK and your ADT plug-in (if you haven't done so already) to Android 4.1 (API 16). I upgraded both from within Eclipse Indigo without any hiccups. Once your environment is ready, you can follow along with the walkthrough, or simply download and import the project.

1. Create a new Android project in Eclipse. Make sure to target Android 4.1. Rename the startup file to Main.java.

2. In your /res directory, add a new folder called drawable. Here you can place any PNG image and call it avatar.png. While the code scales the image to the system-defined size for the notification dialog, try to use something square so as not to lose the aspect ratio. My image is 255x255 pixels.

3. For this example, we don't have any reason to modify the default layout, so let's skip straight to the /src/Main.java file.

Main.java
package com.authorwjf.e_notifications;
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.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bm = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.avatar),
getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height),
true);
Intent intent = new Intent(this, Main.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 01, intent, Intent.FLAG_ACTIVITY_CLEAR_TASK);
Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setContentTitle("This is the title");
builder.setContentText("This is the text");
builder.setSubText("Some sub text");
builder.setNumber(101);
builder.setContentIntent(pendingIntent);
builder.setTicker("Fancy Notification");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setLargeIcon(bm);
builder.setAutoCancel(true);
builder.setPriority(0);
Notification notification = builder.build();
NotificationManager notificationManger =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManger.notify(01, notification);
}
}
As you can see, thanks to the Notification.Builder helper, there isn't a lot to be done. Utilizing the default template, we can add things like icons, a count, and the subtext for a lengthy preview of content. It's straightforward and a lot of bang for your buck (Figure A). Figure A

In my mind, it is the incremental releases like Jelly Bean that really helps to narrow any gap that still exists between Android and iOS. What do you think of Google's latest SDK? Sound off in the comments.

About

William J Francis began programming computers at age eleven. Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intellige...

1 comments

Editor's Picks