{"id":24,"date":"2013-03-04T20:15:03","date_gmt":"2013-03-04T20:15:03","guid":{"rendered":"https:\/\/code4reference.com\/?p=24"},"modified":"2013-03-04T20:15:03","modified_gmt":"2013-03-04T20:15:03","slug":"code4referencetutorial-on-new-android-jelly-bean-notification","status":"publish","type":"post","link":"https:\/\/code4reference.com\/?p=24","title":{"rendered":"Code4ReferenceTutorial on new Android Jelly Bean notification"},"content":{"rendered":"<p>You may have heard about Android Jelly Bean (API level 16). Google has improved a lot of features and introduced new features. One of them is the notification.\u00a0Now they have made the notification more versatile by introducing media rich notification. Google has come up with three special style of notification which are mentioned below. Even developer can write his own customized notification style using remote view.The old Notification class constructor has been deprecated and a brand new and enhanced version of Notification has been introduced.<\/p>\n<ul>\n<li><b>Basic Notification<\/b> \u2013 Shows simple and short notification with icon.<\/li>\n<li><b>Big Picture Notification<\/b> \u2013 Shows visual content such as bitmap.<\/li>\n<li><b>Big Text Notification<\/b> \u2013 Shows multiline Textview object.<\/li>\n<li><b>Inbox Style Notification \u2013<\/b> Shows any kind of list, e.g messages, headline etc.<b>\u00a0<\/b><\/li>\n<\/ul>\n<p>Old syntax requires us to create an object of notification but now Android uses builder patter to create the notification object. <b><i>Notification.Builder<\/i><\/b>\u00a0class has been introduced to make this task easier. This class returns the builder object which is configurable according to your requirements. The helper classes have been introduced like\u00a0<b><i>Notification.BigPictureStyle, Notification.BigTextStyle,<\/i><\/b> and <b><i>Notification.InboxStyle<\/i><\/b>. These classes are re-builder classes which take object created by Notification.Builder class and \u00a0modify the behavior like so.<\/p>\n<p><b>Project Information:<\/b> Meta-data about the project.<\/p>\n<p><b>Platform Version :<\/b> Android API Level 16.<br \/>\n<b>IDE :<\/b> Eclipse Helios Service Release 2<br \/>\n<b>Emulator :<\/b> Android 4.1(API 16)<\/p>\n<p><b>Prerequisite:<\/b>\u00a0Preliminary\u00a0knowledge of Android application framework, and Intent.<\/p>\n<p>First create project by<b>\u00a0<i>Eclipse &gt; File&gt; New Project&gt;Android Application Project<\/i>.<\/b>\u00a0The following dialog box will appear. Fill the required field, i.e Application Name, Project Name and Package Name. Don\u2019t forget to select the Build SDK version (for this tutorial Google API 16 has been selected). Now press the\u00a0<b>next<\/b> button.<\/p>\n<p><a href=\"http:\/\/1.bp.blogspot.com\/-InjJ6PfEJPc\/UATmpu0RplI\/AAAAAAAAAko\/p1e3IksDugY\/s1600\/New+Android+App+_001.png\"><img alt='newandroidapp_001-9374474' src='https:\/\/code4reference.com\/wp-content\/uploads\/2013\/03\/NewAndroidApp_001-9374474.png' \/><\/a><\/p>\n<p>\u00a0Once the dialog box appears, select the BlankActivity and click the\u00a0<b><i>next<\/i><\/b>\u00a0button.<\/p>\n<p><a href=\"http:\/\/3.bp.blogspot.com\/-a1cKXArzL-E\/UATmpxbf37I\/AAAAAAAAAkw\/JYwZqZIzpwo\/s1600\/New+Android+App+_002.png\"><img alt='newandroidapp_002-8042471' src='https:\/\/code4reference.com\/wp-content\/uploads\/2013\/03\/NewAndroidApp_002-8042471.png' \/><\/a><\/p>\n<p>\u00a0Fill the Activity Name and Layout file name for the dialog box shown below and hit the\u00a0<b><i>finish<\/i><\/b>\u00a0button.<\/p>\n<p><a href=\"http:\/\/1.bp.blogspot.com\/-ohwvSvfZSX8\/UATmqSg9HFI\/AAAAAAAAAk4\/tXvuKyKPFE4\/s1600\/New+Android+App+_003.png\"><img alt='newandroidapp_003-1169528' src='https:\/\/code4reference.com\/wp-content\/uploads\/2013\/03\/NewAndroidApp_003-1169528.png' \/><\/a><\/p>\n<p>This process will setup the basic project files. Now we are going to add four buttons in the <b><i>activity_main.xml<\/i><\/b> file. \u00a0You can modify the layout file using either Graphical Layout editor or xml editor. The content of the file should look like this.<\/p>\n<pre>\n\n\n\n    \n    \n   \n   \n\n<\/pre>\n<p>You may have noticed that <i><b>onClick<\/b><\/i> methods are associated with respective buttons. If you don\u2019t know how to define and use the background file for view then ignore the <b><i>android:background<\/i><\/b> field. Now we are going to define the methods sendBasicNotification, sendBigTextStyleNotification, sendBigPictureStyleNotification and sendInboxStyleNotification. As the method name suggests, it sends that particular kind of notification. In each method we are creating <b><i>Notification.Builder<\/i><\/b> object, and customizing the object. Here builder pattern has been used to customize the object. Once the customization is done, \u00a0call build() method to get the notification object.<b>\u00a0<\/b>In this new notification system, at most three actions can be associated to a notification, which are displayed below the notification content. \u00a0This can be achieved by calling addAction() method on the builder object. The same number of icons you will see on the notification as you will notice for sendBigPictureStyleNotifcation() method. Notification priority can also be set by calling setPriority() method as shown in sendBigTextStyleNotification() method.\u00a0In the code given\u00a0below,\u00a0intent has been used to invoke the\u00a0HandleNotificationActivity.<\/p>\n<pre>\n\npackage com.example.jellybeannotificationexample;\n\nimport android.app.Activity;\nimport android.app.Notification;\nimport android.app.Notification.Builder;\nimport android.app.NotificationManager;\nimport android.app.PendingIntent;\nimport android.content.Intent;\nimport android.graphics.BitmapFactory;\nimport android.os.Bundle;\nimport android.view.Menu;\nimport android.view.View;\n\npublic class NotificationMainActivity extends Activity {\n\n @Override\n public void onCreate(Bundle savedInstanceState) {\n  super.onCreate(savedInstanceState);\n  setContentView(R.layout.activity_main);\n }\n\n @Override\n public boolean onCreateOptionsMenu(Menu menu) {\n  getMenuInflater().inflate(R.menu.activity_main, menu);\n  return true;\n }\n\n public void sendBasicNotification(View view) {\n  Notification notification = new Notification.Builder(this)\n    .setContentTitle(\"Basic Notification\")\n    .setContentText(\"Basic Notification, used earlier\")\n    .setSmallIcon(R.drawable.ic_launcher_share).build();\n  notification.flags |= Notification.FLAG_AUTO_CANCEL;\n  NotificationManager notificationManager = getNotificationManager();\n  notificationManager.notify(0, notification);\n }\n\n public void sendBigTextStyleNotification(View view) {\n  String msgText = \"Jeally Bean Notification example!! \"\n    + \"where you will see three different kind of notification. \"\n    + \"you can even put the very long string here.\";\n\n  NotificationManager notificationManager = getNotificationManager();\n  PendingIntent pi = getPendingIntent();\n  Builder builder = new Notification.Builder(this);\n  builder.setContentTitle(\"Big text Notofication\")\n    .setContentText(\"Big text Notification\")\n    .setSmallIcon(R.drawable.ic_launcher)\n    .setAutoCancel(true);\n    .setPriority(Notification.PRIORITY_HIGH)\n    .addAction(R.drawable.ic_launcher_web, \"show activity\", pi);\n  Notification notification = new Notification.BigTextStyle(builder)\n    .bigText(msgText).build();\n \n  notificationManager.notify(0, notification);\n }\n\n public void sendBigPictureStyleNotification(View view) {\n  PendingIntent pi = getPendingIntent();\n  Builder builder = new Notification.Builder(this);\n  builder.setContentTitle(\"BP notification\")\n    \/\/ Notification title\n    .setContentText(\"BigPicutre notification\")\n    \/\/ you can put subject line.\n    .setSmallIcon(R.drawable.ic_launcher)\n    \/\/ Set your notification icon here.\n    .addAction(R.drawable.ic_launcher_web, \"show activity\", pi)\n    .addAction(\n      R.drawable.ic_launcher_share,\n      \"Share\",\n      PendingIntent.getActivity(getApplicationContext(), 0,\n        getIntent(), 0, null));\n\n  \/\/ Now create the Big picture notification.\n  Notification notification = new Notification.BigPictureStyle(builder)\n    .bigPicture(\n      BitmapFactory.decodeResource(getResources(),\n        R.drawable.big_picture)).build();\n  \/\/ Put the auto cancel notification flag\n  notification.flags |= Notification.FLAG_AUTO_CANCEL;\n  NotificationManager notificationManager = getNotificationManager();\n  notificationManager.notify(0, notification);\n }\n\n public void sendInboxStyleNotification(View view) {\n  PendingIntent pi = getPendingIntent();\n  Builder builder = new Notification.Builder(this)\n    .setContentTitle(\"IS Notification\")\n    .setContentText(\"Inbox Style notification!!\")\n    .setSmallIcon(R.drawable.ic_launcher)\n    .addAction(R.drawable.ic_launcher_web, \"show activity\", pi);\n\n  Notification notification = new Notification.InboxStyle(builder)\n    .addLine(\"First message\").addLine(\"Second message\")\n    .addLine(\"Thrid message\").addLine(\"Fourth Message\")\n    .setSummaryText(\"+2 more\").build();\n  \/\/ Put the auto cancel notification flag\n  notification.flags |= Notification.FLAG_AUTO_CANCEL;\n  NotificationManager notificationManager = getNotificationManager();\n  notificationManager.notify(0, notification);\n }\n\n public PendingIntent getPendingIntent() {\n  return PendingIntent.getActivity(this, 0, new Intent(this,\n    HandleNotificationActivity.class), 0);\n }\n\n public NotificationManager getNotificationManager() {\n  return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);\n }\n}\n<\/pre>\n<p>We have defined basic HandleNotificationActivity which just shows a simple message when intent is fired for this activity. The content of the file is as following.<\/p>\n<pre>\npackage com.example.jellybeannotificationexample;\n\nimport android.app.Activity;\nimport android.os.Bundle;\n\npublic class HandleNotificationActivity extends Activity {\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n  super.onCreate(savedInstanceState);\n  setContentView(R.layout.handle_notification_activity);\n }\n}\n<\/pre>\n<p>The corresponding layout file(handle_notification_activity.xml) is given below<\/p>\n<pre>\n\n\n    \n\n\n<\/pre>\n<p>Now you have to define the Android manifiest file.\u00a0HandleNotificationActivity should be included in the manifest file and then put the intent filter for this activity.<\/p>\n<pre>\n\n\n\n    \n\n    \n        \n            \n                \n                \n            \n        \n        \n            \n                \n            \n        \n    \n\n\n<\/pre>\n<p>Once you are done with the coding, just execute it. You will see the application as shown in the picture below. On clicking the button you will see the corresponding notification on the upper part of the screen. If you drag the notification down then you can see the entire message and corresponding icon. The \u00a0pictures\u00a0below\u00a0are the notification when they were dragged down.<\/p>\n<table>\n<tr>\n<td>\n<table>\n<tr>\n<td><a href=\"http:\/\/2.bp.blogspot.com\/-z97UaYu5wh8\/UAO8HoBs7lI\/AAAAAAAAAkc\/k2hmUdPG28U\/s1600\/jelly-bean-notification-tutorial-example.png\"><img alt='jelly-bean-notification-tutorial-example-6212304' src='https:\/\/code4reference.com\/wp-content\/uploads\/2013\/03\/jelly-bean-notification-tutorial-example-6212304.png' \/><\/a><\/td>\n<\/tr>\n<tr>\n<td>Application<\/td>\n<\/tr>\n<\/table>\n<\/td>\n<td>\n<table>\n<tr>\n<td><a href=\"http:\/\/1.bp.blogspot.com\/-XmurThpQE9k\/UAO8GaLhMmI\/AAAAAAAAAj8\/UevCGLVfIJk\/s1600\/jelly-bean-basic-notification-tutorial-example.png\"><img alt='jelly-bean-basic-notification-tutorial-example-7475193' src='https:\/\/code4reference.com\/wp-content\/uploads\/2013\/03\/jelly-bean-basic-notification-tutorial-example-7475193.png' \/><\/a><\/td>\n<\/tr>\n<tr>\n<td>Basic notification<\/td>\n<\/tr>\n<\/table>\n<\/td>\n<td>\n<table>\n<tr>\n<td><a href=\"http:\/\/1.bp.blogspot.com\/-Y351CR37Uqw\/UAO8HEXavFI\/AAAAAAAAAkM\/yYvHAGBBzy4\/s1600\/jelly-bean-big-text-notification-tutorial.png\"><img alt='jelly-bean-big-text-notification-tutorial-6588213' src='https:\/\/code4reference.com\/wp-content\/uploads\/2013\/03\/jelly-bean-big-text-notification-tutorial-6588213.png' \/><\/a><\/td>\n<\/tr>\n<tr>\n<td>Big Text Style<\/td>\n<\/tr>\n<\/table>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<table>\n<tr>\n<td><a href=\"http:\/\/1.bp.blogspot.com\/-R_9uAzGNMoA\/UAO8G0XkXxI\/AAAAAAAAAkE\/WXyrdogLgy8\/s1600\/jelly-bean-big-picture-notification-tutorial-example.png\"><img alt='jelly-bean-big-picture-notification-tutorial-example-1954019' src='https:\/\/code4reference.com\/wp-content\/uploads\/2013\/03\/jelly-bean-big-picture-notification-tutorial-example-1954019.png' \/><\/a><\/td>\n<\/tr>\n<tr>\n<td>Big Picture Style<\/td>\n<\/tr>\n<\/table>\n<\/td>\n<td>\n<table>\n<tr>\n<td><a href=\"http:\/\/1.bp.blogspot.com\/-4xi15aKfP94\/UAO8HULClzI\/AAAAAAAAAkU\/-UgBFuDsn5Q\/s1600\/jelly-bean-inbox-style-notification-tutorial-example.png\"><img alt='jelly-bean-inbox-style-notification-tutorial-example-6066159' src='https:\/\/code4reference.com\/wp-content\/uploads\/2013\/03\/jelly-bean-inbox-style-notification-tutorial-example-6066159.png' \/><\/a><\/td>\n<\/tr>\n<tr>\n<td>Inbox Style<\/td>\n<\/tr>\n<\/table>\n<\/td>\n<\/tr>\n<\/table>\n<p>If you are interested\u00a0to know more about\u00a0source code, you can find it over <a href=\"http:\/\/github.com\/rakeshcusat\/Code4Reference\/tree\/master\/AndroidProjects\/JellyBeanNotificationExample\">here<\/a>.<\/p>\n<p>Your valuable comments are always welcomed. It will help to improve my post and understanding.<\/p>\n<p><i>\u201cBy three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest.\u201d<\/i><br \/>\nBy : Confucius<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You may have heard about Android Jelly Bean (API level 16). Google has improved a lot of features and introduced new features. One of them is the notification.\u00a0Now they have made the notification more versatile by introducing media rich notification. Google has come up with three special style of notification which are mentioned below. Even [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24"}],"version-history":[{"count":0,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions"}],"wp:attachment":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}