Getting started with Android notifications
A notification is a message which can display to the user outside of your application’s normal UI. When you tell the system to show a notification, it first appears as an icon in the notification area. To see the notification in details, the user opens the notification drawer. Here notification area and notification drawer both are system-controlled areas which the user can view at any time.
Notifications in android are represented by the Notification class.
getSystemService() method denotes the NotificationManager class and that to be used for creating Notifications.
NotificationManager testNotification= (NotificationManager)
To generate notification, first you need to create notification object in which you can set the title, content text, ticker text etc… The following code demonstrates how to create notification object and set title, content text, ticker text in it.
Notification notif = new Notification.Builder(this);
notif.setContentTitle(“Set your title”);
notif.setContentText(“Set Content text”);
After that, you can set the intent using below code that should be launched on clicking on the notification.
Intent main = new Intent(mContext,Main.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext,NOTIFICATION_ID, main,PendingIntent.FLAG_ONE_SHOT);
NotificationManager testNotif = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Additionally, it is possible to determine a big view which appears only when the notification is expanded. It happens only when the notification is at the top of the drawer or when the user expands the notification with a gesture.
With help of V4 appcompat library, you can implement this for all devices from Android 2.0 onward.
Here we used Notification.Builder class which has been introduced to make this task easier. It returns the builder object which is configurable according to requirements.
There are three class available in Notification.Builder.
1) Big picture style:
The following code demonstrates how to show large image in notification bar. This style of notifications is known as BigPictureStyle Notifications. E.g: Flipkart, Mobikwik, Paytm.They randomly post big picture notifications, the image contains of brief guide about offers.BigPictureStyle shows visual content such as bitmap.
To load image in notification, first create a bitmap of an image which needs to be shown as big picture and set it. you can also set the summary text which appears on expanding the notification.
Bitmap test_bitmap = BitmapFactory.decodeStream(this.getResources(),R.drawable.my_big_icon);
NotificationCompat.BigPictureStyle bigPicStyle = new NotificationCompat.BigPictureStyle().bigPicture(test_bitmap).setSummaryText("Summary text appears on expanding the notification");
2) Big text style:
The following code demonstrates how to displays a large text block to show more details on the notification drawer. BigTextStyle generates large notifications that include a lot of text. BigTextStyle shows multiline textview object which allows you to use up to 256 dp.
String demo = "This is solely testing text of the typesetting business. Testing this has been the industry's commonplace dummy text ever since the decennium, once Associate in Nursing unknown printer took a galley of sort and disorganized it to create a sort specimen book. it's survived not solely in six centuries, however conjointly the leap into typesetting, remaining unchanged. it has been popularised within the Eighties with the discharge of Testing sheets containing Demo passages, and additional recently with publication package like testing together with versions of android";
NotificationCompat.BigTextStyle big_text = new NotificationCompat.BigTextStyle().bigText(demo);
3) Inbox style:
The following code demonstrates how to generate large-format notifications that include a list of strings. InboxStyle displays rows of text like a listView. For example : Gmail notification for multiple emails, messages, headline etc… InboxStyle allows you to include a list of (up to 5 line) strings
If the platform does not provide large-format or picture-format notifications, then above listed method has no effect. The user will always see the normal notification view.