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:
01 NotificationManager notificationManager = getNotificationManager(); 02 Notification notification = 03 new Notification(android.R.drawable.ic_menu_camera, "Hello camera!", System.currentTimeMillis()); 04 notification.flags |= Notification.FLAG_AUTO_CANCEL; 05 notification.number += 1; 06 07 Intent intent = new Intent(this, MainActivity.class); 08 PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0); 09 notification.setLatestEventInfo(this, "title", "content", activity); 10 11 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:
01 Builder build = new Notification.Builder(this) 02 .setContentTitle("New mail from me") 03 .setContentText("subject") 04 .setTicker("New email with photo") 05 .setSmallIcon(R.drawable.ic_action_search) 06 .addAction( 07 android.R.drawable.ic_btn_speak_now, 08 "Speak!", 09 PendingIntent.getActivity(getApplicationContext(), 0, 10 getIntent(), 0, null)) 11 .addAction( 12 android.R.drawable.ic_dialog_map, 13 "Maps", 14 PendingIntent.getActivity(getApplicationContext(), 0, 15 getIntent(), 0, null)) 16 .addAction( 17 android.R.drawable.ic_dialog_info, 18 "Info", 19 PendingIntent.getActivity(getApplicationContext(), 0, 20 getIntent(), 0, null)); 21 22 Notification notification = new Notification.BigPictureStyle(build) 23 .bigPicture( 24 BitmapFactory.decodeResource(getResources(), 25 R.drawable.jellybean)).build(); 26 27 Intent notificationIntent = new Intent(this, MainActivity.class); 28 29 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.
You might also like: