Android Core

Android – Using Jelly Bean notifications

Last post was long ago! I’ve been very busy lately… but learning a lot from Android! (and Node js too… I’m in love with this platform!)
Other things that I decided for those who are following me is that, since now, articles will only be in English (sorry =/ ) and all the source code of my examples will be available on github.
Obviously, being an Android developer, Google I/O has been a great source of new things to learn… And one of the was Jelly Bean (Android 4.1 for those that don’t know it yet).
One of the top new things Jelly Bean brings us is new notification functionalities. You can see them in the Google I/O Keynote or some of the articles all over the Internet (1, 2, 3, 4)
So, Where do we have to begin? First of all, you’ll have to create an Android Project with Jelly Bean SDK version.
Probably, most of you had used NotificationManager to push notifications on Android devices… For new Jelly Bean notifications it must be used almost in the same way. You can use something like this:
NotificationManager notificationManager = getNotificationManager();
Notification notification =
     new Notification(android.R.drawable.ic_menu_camera, "Hello camera!", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number += 1;

Intent intent = new Intent(this, MainActivity.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "title", "content", activity);

notificationManager.notify(0, notification);
What do we have to do to use new Jelly Bean notifications? It’s pretty easy…. and I must admit that I prefer this way using the Builder pattern. This is an example of the Big picture style:
Builder build = new Notification.Builder(this)
   .setContentTitle("New mail from me")
   .setContentText("subject")
   .setTicker("New email with photo")
   .setSmallIcon(R.drawable.ic_action_search)
   .addAction(
    android.R.drawable.ic_btn_speak_now,
    "Speak!",
    PendingIntent.getActivity(getApplicationContext(), 0,
     getIntent(), 0, null))
   .addAction(
    android.R.drawable.ic_dialog_map,
    "Maps",
    PendingIntent.getActivity(getApplicationContext(), 0,
     getIntent(), 0, null))
   .addAction(
    android.R.drawable.ic_dialog_info,
    "Info",
    PendingIntent.getActivity(getApplicationContext(), 0,
     getIntent(), 0, null));

Notification notification = new Notification.BigPictureStyle(build)
    .bigPicture(
      BitmapFactory.decodeResource(getResources(),
        R.drawable.jellybean)).build();

Intent notificationIntent = new Intent(this, MainActivity.class);

notificationManager.notify(0, notification);
All the code is available at my github account (where you can find the beggining of a new ORM solution for Android which I hope someday will be finished =/) on the following link. you can find in that project other example of the new notifications too (inbox style and big text).
Here is how the three notifications will show up in your Android devices…. (if you have Jelly Bean on them :-P )
I hope you like the article, and you can ask me any doubt, follow me on twitter or write me and email.
Reference: Using Jelly Bean notifications from our JCG partner Javier Manzano at the Javier Manzano’s Blog blog.

Javier Manzano

Javier is an Android and node.js developer. Working day to day on new pet projects. Founder of Parkuik and Codeely.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button