Get A Quote
15Oct 2015

Different Types Of Notification In Android

15 Comments   Android
android_all_image1

Android allows user to notify on new message arrived as well as some calendar – based events by notification. Notification is a user interface element that will display outside of any other app’s normal UI to indicate that an event has occurred. Users can choose to view the notification while using other apps and respond to it when it’s convenient for them.

Android notification will be displayed in Notification area and to see the details regarding the notification, the user can expand it in by open the Notification drawer.

We will discuss about Local notification and Heads Up Notification here.

To Create Notification :

To create notification we have to use NotificationCompat.Builder’s object. Notification itself build by NotificationCompat.Builder.build(), which returns a Notification object containing with specifications of notification.

A Notification object must contain setSmallIcon() to set a small icon, setContentTitle() to set title and setContentText() to set detailed text. Another settings and contents are optional. Bellow shows uses of these properties in Normal and Big View.

normal_viewbig_view

To set action like as call another activity directly by click on notification, we have to use  PendingIntent with containing an Intent which starts an Activity of our application.

We can set notification priority using NotificationCompat.Builder.setPriority() by  passing one of the NotificationCompat priority constants.

There are five priority levels like as bellow :

1) PRIORITY_MAX =2

2) PRIORITY_HIGH =1

3) PRIORITY_DEFAULT =0

4) PRIORITY_LOW =-1

5) PRIORITY_MIN=-2

Here is a code to create notification :

1
2
3
4
5
6
7
8
9
10
11
 //build notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Simple notification")
.setContentText("This is test of simple notification.");
// Gets an instance of the NotificationManager service
NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

//to post your notification to the notification bar
notificationManager.notify(0 , mBuilder.build());

Output:

simple_notification

Expand Layout of a Notification :

To expand layout of notification we have to use Builder.setStyle() with NotificationCompat.Builder object.

Note : Expanded notifications are not available on platforms prior to Android 4.1.

There are different types of expanded notification as bellow :

1) Inbox Style Notification :

When you want to display multiline text  detail section, you can use NotificationCompat.InboxStyle.

Here is example code of inbox style notification :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 //set intents and pending intents to call activity on click of "show activity" action button of notification
Intent resultIntent = new Intent(this, ResultActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent piResult = PendingIntent.getActivity(this,
(int) Calendar.getInstance().getTimeInMillis(), resultIntent, 0);

//Assign inbox style notification
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();

inboxStyle.setBigContentTitle("Inbox Notification");
inboxStyle.addLine("Message 1.");
inboxStyle.addLine("Message 2.");
inboxStyle.addLine("Message 3.");
inboxStyle.addLine("Message 4.");
inboxStyle.addLine("Message 5.");
inboxStyle.setSummaryText("+2 more");

//build notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Inbox style notification")
.setContentText("This is test of inbox style notification.")
.setStyle(inboxStyle)
.addAction(R.drawable.ic_input_black_18dp, "show activity", piResult);

// Gets an instance of the NotificationManager service
NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

//to post your notification to the notification bar
notificationManager.notify(0, mBuilder.build());

Output :

device-2015-10-08-111304_inbox1

2) Big Text Style Notification :

When you have large text block in detail section of text you can use NotificationCompat.BigTextStyle.

Here is example code of Big Text style notification :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 //To set large icon in notification
Bitmap icon1 = BitmapFactory.decodeResource(getResources(),
R.drawable.big_image);

//Assign inbox style notification
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.");
bigText.setBigContentTitle("Big Text Notification");
bigText.setSummaryText("By: Author of Lorem ipsum");

//build notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Big Text notification")
.setContentText("This is test of big text style notification.")
.setLargeIcon(icon1)
.setStyle(bigText);

// Gets an instance of the NotificationManager service
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

//to post your notification to the notification bar
mNotificationManager.notify(0, mBuilder.build());

Output :

device-2015-10-08-111351_bigtext1

3)  Big Picture Style Notification :

When you have large format in which you want to display big image you can use NotificationCompat.BigPictureStyle.

Big picture contains a bitmap upto 256 dip tall in its detail section.

Here is example code of Big Picture style notification :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//build notification
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Big picture notification")
.setContentText("This is test of big picture notification.")
.setStyle(bigPictureStyle)
.addAction(R.drawable.ic_input_black_18dp, "show activity", piResult)
.addAction(R.drawable.ic_share_black_18dp, "Share",
PendingIntent.getActivity(getApplicationContext(), 0,
getIntent(), 0, null));
//set intents and pending intents to call activity on click of "show activity" action button of notification
Intent resultIntent = new Intent(this, ResultActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent piResult = PendingIntent.getActivity(this,
(int) Calendar.getInstance().getTimeInMillis(), resultIntent, 0);

// Assign big picture notification
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();

bigPictureStyle.bigPicture(
BitmapFactory.decodeResource(getResources(),
R.drawable.big_image)).build();

// Gets an instance of the NotificationManager service
NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

//to post your notification to the notification bar
notificationManager.notify(0, builder.build());

Output :

bigpicture_notification
Schedule using AlarmManager and broadcast Notification :

Sometimes user need to schedule notification to notify a user at particular date/time, even though the user has not opened the application. For this we have to use BroadcastReceiver of android sdk. We will discuss about BroadcastRreceiver below.

In addition to above class we also have to use AlarmManager class to schedule and add events. AlarmManager has access to the system alarm services. With the help of it you can schedule execution of code in future.

AlarmManager object can’t instantiate directly however it can be retrieved by calling Context.getSystemService(Context.ALARM_SERVICE). AlarmManager is always registered with Intent. When an alarm goes off, the Intent which has been registered with AlarmManager, is broadcasted by the system automatically. This intent starts the target application if it is not running. It is recommended to use AlarmManager when you want your application code to be run at a specific time, even if your application is not currently running.

Here is example code of Schedule notification :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 //set intents and pending intents to call activity on click of "show activity" action button of notification
Intent resultIntent = new Intent(this, ResultActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent piResult = PendingIntent.getActivity(this,
(int) Calendar.getInstance().getTimeInMillis(), resultIntent, 0);

// Assign big picture notification
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();

bigPictureStyle.bigPicture(
BitmapFactory.decodeResource(getResources(),
R.drawable.big_image)).build();

//build notification
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Big picture notification")
.setContentText("This is test of big picture notification.")
.setStyle(bigPictureStyle)
.addAction(R.drawable.ic_input_black_18dp, "show activity", piResult)
.addAction(R.drawable.ic_share_black_18dp, "Share",
PendingIntent.getActivity(getApplicationContext(), 0,
getIntent(), 0, null));

// Gets an instance of the NotificationManager service
NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

//to post your notification to the notification bar
notificationManager.notify(0, builder.build());

Now create BroadcastReceiver.

A Broadcast Receiver (short receiver) allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 public class TimeAlarm extends BroadcastReceiver {
NotificationManager nm;

@Override
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Schedule notification";
CharSequence message = "This is test of Schedule notification";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(), 0);
Notification notif = new Notification(R.drawable.snooze,
"Test Schedule notification", System.currentTimeMillis());
notif.defaults = Notification.DEFAULT_ALL;
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
}

}

At last don’t forget to register BroadcastReceiver class in AndroidManifest.xml

<receiver android:name=“.TimeAlarm” />

Output :

schedule_notification

 

 

Brevity

About Admin

Brevity Software provides services such as : Web design and development, Mobile application development, Web application development, Travel portal development, Event management systems, Transport & Logistics Software, Enterprise Mobility Solutions, eCommerce & M Commerce application and many more.

15 thoughts on “Different Types Of Notification In Android

  1. Great wordpress blog here.. It’s hard to find quality writing like yours these days. I really appreciate people like you! take care

  2. Go Here says:

    I just want to tell you that I am newbie to weblog and actually loved you’re blog site. Most likely I’m going to bookmark your site . You surely have great well written articles. Bless you for sharing with us your web-site.

  3. Everything is very open with a very clear description of the issues.
    It was really informative. Your website is very
    useful. Many thanks for sharing!

  4. Great post, thanks for sharing useful information.

  5. Frank says:

    Nice Post, Thanks for sharing this post.

  6. Helen says:

    My brother recommended I may like this website. He was once entirelly right.
    This sbmit truly made my day. You can not bbelieve just
    how so much time I hhad spent for this info! Thank you!

  7. KristopherHa says:

    I see your page needs some fresh & unique content.
    Writing manually is time consuming, but there is tool for this task.

  8. One of the better photos I’ve seen lately

  9. RenaKirschba says:

    I see your blog needs some unique & fresh articles. Writing manually is time
    consuming, there is tool for this task.

  10. Heya i am for the first time here. I came across this board and I find It truly useful & it helped me out a lot. I hope to give something back and help others like you aided me.

  11. Great blog here! Also your site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as fast as yours lol

  12. Excellent weblog right here! Also your web site so much up fast!

  13. Excellent post. I was checking constantly this blog and I’m impressed! Very useful info specifically the last part :) I care for such info a lot. I was looking for this particular information for a very long time. Thank you and good luck.

  14. Let me sharing this article with my fresher development team, hope they may not aware of this.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>