Tutorial details

Android Jelly Bean notification tutorial | App Code for Sale | Preview

Android Jelly Bean notification tutorial | Android Tutorial

How Android Jelly Bean notification works

Overview PAGE TOP

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. Now 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.

Notification Type: PAGE TOP

  • Basic Notification – Shows simple and short notification with icon.
  • Big Picture Notification – Shows visual content such as bitmap.
  • Big Text Notification – Shows multiline Textview object.
  • Inbox Style Notification – Shows any kind of list, e.g messages, headline etc.

Old syntax requires us to create an object of notification but now Android uses builder patter to create the notification object. Notification.Builder class 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 Notification.BigPictureStyle, Notification.BigTextStyle, and Notification.InboxStyle. These classes are re-builder classes which take object created by Notification.Builder class and modify the behavior like so.

Project Information: Meta-data about the project.

Platform Version : Android API Level 16.

IDE : Eclipse Helios Service Release 2

Emulator : Android 4.1(API 16)

Prerequisite: Preliminary knowledge of Android application framework, and Intent.

First create project by Eclipse > File> New Project>Android Application Project. The following dialog box will appear. Fill the required field, i.e Application Name, Project Name and Package Name. Don’t forget to select the Build SDK version (for this tutorial Google API 16 has been selected). Now press the next button.

mobappcoding.16.1.png

Once the dialog box appears, select the BlankActivity and click the next button.

mobappcoding.16.2.png

Fill the Activity Name and Layout file name for the dialog box shown below and hit the finish button.

mobappcoding.16.3.png

This process will setup the basic project files. Now we are going to add four buttons in the activity_main.xml file. You can modify the layout file using either Graphical Layout editor or xml editor. The content of the file should look like this.

01 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
02     xmlns:tools="http://schemas.android.com/tools"
03     android:layout_width="match_parent"
04     android:layout_height="match_parent"
05     android:orientation="vertical"
06     android:gravity="center_horizontal">
07  
08     <Button
09         android:id="@+id/btBasicNotification"
10         android:layout_width="fill_parent"
11         android:layout_height="wrap_content"
12         android:gravity="center_horizontal|center_vertical"
13         android:onClick="sendBasicNotification"
14         android:text="@string/btBasicNotification"
15         android:background="@drawable/button_background"
16         android:textColor="#000000"
17         />
18     <Button
19         android:id="@+id/btBigTextNotification"
20         android:layout_width="fill_parent"
21         android:layout_height="wrap_content"
22         android:gravity="center_horizontal|center_vertical"
23         android:onClick="sendBigTextStyleNotification"
24         android:text="@string/btBigTextNotification"
25         android:background="@drawable/button_background"
26         android:textColor="#000000"
27         />
28    <Button
29         android:id="@+id/btBigPictureNotification"
30         android:layout_width="fill_parent"
31         android:layout_height="wrap_content"
32         android:gravity="center_horizontal|center_vertical"
33         android:onClick="sendBigPictureStyleNotification"
34         android:text="@string/btBigPictureNotification"
35         android:background="@drawable/button_background"
36         android:textColor="#000000" />
37    <Button
38         android:id="@+id/btInboxStyleNotification"
39         android:layout_width="fill_parent"
40         android:layout_height="wrap_content"
41         android:gravity="center_horizontal|center_vertical"
42         android:onClick="sendInboxStyleNotification"
43         android:text="@string/btInboxStyleNotification"
44         android:background="@drawable/button_background"
45         android:textColor="#000000"/>
46 </LinearLayout>

You may have noticed that onClick methods are associated with respective buttons. If you don’t know how to define and use the background file for view then ignore the android:background 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 Notification.Builder object, and customizing the object. Here builder pattern has been used to customize the object. Once the customization is done, call build() method to get the notification object. In this new notification system, at most three actions can be associated to a notification, which are displayed below the notification content. This 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. In the code given below, intent has been used to invoke the HandleNotificationActivity.

001 package com.example.jellybeannotificationexample;
002  
003 import android.app.Activity;
004 import android.app.Notification;
005 import android.app.Notification.Builder;
006 import android.app.NotificationManager;
007 import android.app.PendingIntent;
008 import android.content.Intent;
009 import android.graphics.BitmapFactory;
010 import android.os.Bundle;
011 import android.view.Menu;
012 import android.view.View;
013  
014 public class NotificationMainActivity extends Activity {
015  
016  @Override
017  public void onCreate(Bundle savedInstanceState) {
018   super.onCreate(savedInstanceState);
019   setContentView(R.layout.activity_main);
020  }
021  
022  @Override
023  public boolean onCreateOptionsMenu(Menu menu) {
024   getMenuInflater().inflate(R.menu.activity_main, menu);
025   return true;
026  }
027  
028  public void sendBasicNotification(View view) {
029   Notification notification = new Notification.Builder(this)
030     .setContentTitle("Basic Notification")
031     .setContentText("Basic Notification, used earlier")
032     .setSmallIcon(R.drawable.ic_launcher_share).build();
033   notification.flags |= Notification.FLAG_AUTO_CANCEL;
034   NotificationManager notificationManager = getNotificationManager();
035   notificationManager.notify(0, notification);
036  }
037  
038  public void sendBigTextStyleNotification(View view) {
039   String msgText = "Jeally Bean Notification example!! "
040     + "where you will see three different kind of notification. "
041     + "you can even put the very long string here.";
042  
043   NotificationManager notificationManager = getNotificationManager();
044   PendingIntent pi = getPendingIntent();
045   Builder builder = new Notification.Builder(this);
046   builder.setContentTitle("Big text Notofication")
047     .setContentText("Big text Notification")
048     .setSmallIcon(R.drawable.ic_launcher)
049     .setAutoCancel(true);
050     .setPriority(Notification.PRIORITY_HIGH)
051     .addAction(R.drawable.ic_launcher_web, "show activity", pi);
052   Notification notification = new Notification.BigTextStyle(builder)
053     .bigText(msgText).build();
054   
055   notificationManager.notify(0, notification);
056  }
057  
058  public void sendBigPictureStyleNotification(View view) {
059   PendingIntent pi = getPendingIntent();
060   Builder builder = new Notification.Builder(this);
061   builder.setContentTitle("BP notification")
062     // Notification title
063     .setContentText("BigPicutre notification")
064     // you can put subject line.
065     .setSmallIcon(R.drawable.ic_launcher)
066     // Set your notification icon here.
067     .addAction(R.drawable.ic_launcher_web, "show activity", pi)
068     .addAction(
069       R.drawable.ic_launcher_share,
070       "Share",
071       PendingIntent.getActivity(getApplicationContext(), 0,
072         getIntent(), 0, null));
073  
074   // Now create the Big picture notification.
075   Notification notification = new Notification.BigPictureStyle(builder)
076     .bigPicture(
077       BitmapFactory.decodeResource(getResources(),
078         R.drawable.big_picture)).build();
079   // Put the auto cancel notification flag
080   notification.flags |= Notification.FLAG_AUTO_CANCEL;
081   NotificationManager notificationManager = getNotificationManager();
082   notificationManager.notify(0, notification);
083  }
084  
085  public void sendInboxStyleNotification(View view) {
086   PendingIntent pi = getPendingIntent();
087   Builder builder = new Notification.Builder(this)
088     .setContentTitle("IS Notification")
089     .setContentText("Inbox Style notification!!")
090     .setSmallIcon(R.drawable.ic_launcher)
091     .addAction(R.drawable.ic_launcher_web, "show activity", pi);
092  
093   Notification notification = new Notification.InboxStyle(builder)
094     .addLine("First message").addLine("Second message")
095     .addLine("Thrid message").addLine("Fourth Message")
096     .setSummaryText("+2 more").build();
097   // Put the auto cancel notification flag
098   notification.flags |= Notification.FLAG_AUTO_CANCEL;
099   NotificationManager notificationManager = getNotificationManager();
100   notificationManager.notify(0, notification);
101  }
102  
103  public PendingIntent getPendingIntent() {
104   return PendingIntent.getActivity(this, 0, new Intent(this,
105     HandleNotificationActivity.class), 0);
106  }
107  
108  public NotificationManager getNotificationManager() {
109   return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
110  }
111 }

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.

01 package com.example.jellybeannotificationexample;
02  
03 import android.app.Activity;
04 import android.os.Bundle;
05  
06 public class HandleNotificationActivity extends Activity {
07  
08  @Override
09  protected void onCreate(Bundle savedInstanceState) {
10   super.onCreate(savedInstanceState);
11   setContentView(R.layout.handle_notification_activity);
12  }
13 }

The corresponding layout file(handlenotificationactivity.xml) is given below

01 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
02     android:layout_width="match_parent"
03     android:layout_height="match_parent"
04     android:orientation="vertical"
05      android:gravity="center_horizontal|center_vertical" >
06  
07     <TextView
08         android:id="@+id/textView1"
09         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:text="@string/tvHandleNotification"
12         android:textSize="20dp"
13         android:textStyle="bold|italic" />
14  
15 </LinearLayout>

Now you have to define the Android manifiest file. HandleNotificationActivity should be included in the manifest file and then put the intent filter for this activity.

01 05
06 09
10 13 16 17 18 19 20 21 24 25 26 27 28 29
30

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 pictures below are the notification when they were dragged down.

mobappcoding.16.4.jpg

Reference PAGE TOP

http://www.javacodegeeks.com/2012/08/android-jelly-bean-notification-tutorial.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.