Different Types Of Notification In Android
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 the 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.
To set action like as call another activity directly by clicking on the 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 NotificationCompatpriority constants.
There are five priority levels like as below:
- PRIORITY_MAX =2
- PRIORITY_HIGH =1
- PRIORITY_DEFAULT =0
- PRIORITY_LOW =-1
- PRIORITY_MIN=-2
Here is a code to create notification:
[cc lang="java"] //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());
[/cc]
Expand Layout of a Notification
To expand the 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 below :
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:
[cc lang="java"] //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());
[/cc]
Big Text Style Notification
When you have a large text block in detail section of text you can use NotificationCompat.BigTextStyle
Here is example code of Big Text style notification:
[cc lang="java"] //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());
[/cc]
Big Picture Style Notification
When you have a large format in which you want to display a big image you can use NotificationCompat.BigPictureStyle
The big picture contains a bitmap up to 256 dip tall in its detail section.
Here is example code of Big Picture style notification:
[cc lang="java"]
//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());
[/cc]
Schedule using AlarmManager and broadcast Notification
Sometimes the user needs to schedule a notification to notify a user at a 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 BroadcastRreceiver below.
In addition to the 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:
[cc lang="java"] //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());
[/cc]
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.
[cc lang="java"] 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);
}
}
[/cc]
At last, don’t forget to register BroadcastReceiver class in AndroidManifest.xml
<receiver android:name=".TimeAlarm"/>
Related Post
-
F
-
A
-
Q
An Android notification is a user interface element that informs users when an event occurs, such as a new message or a calendar event. It appears in the notification area outside the normal interface of an application. Users can open the notification drawer to view more details and decide when they want to respond to the notification.
`NotificationCompat.Builder` is used to create and configure notifications in an Android application. A basic notification should include a small icon, a content title, and detailed content text, while other settings can be added when required. After building the notification, the `NotificationManager` is used to display it in the notification area.
Android provides five notification priority levels: `PRIORITY_MAX`, `PRIORITY_HIGH`, `PRIORITY_DEFAULT`, `PRIORITY_LOW`, and `PRIORITY_MIN`. These levels help determine how prominently a notification should be presented to the user. Developers can select an appropriate priority based on how important or urgent the notification is.
Expanded notifications allow users to see more information than what is displayed in the normal notification layout. Developers can use `Builder.setStyle()` with different notification styles to provide additional content when the notification is expanded. The blog discusses expanded notification styles such as Inbox Style, Big Text Style, and Big Picture Style.
Inbox Style Notification is useful when an application needs to display multiple lines of text within an expanded notification. Developers can use `NotificationCompat.InboxStyle` to add several individual lines and provide a summary of additional messages. This style is particularly useful for displaying multiple messages, emails, or other grouped pieces of information in one notification.
Big Text Style Notification is designed for situations where the notification needs to display a larger block of text. Developers can use `NotificationCompat.BigTextStyle` to show expanded text along with a larger content title and summary information. It is useful when a short notification preview is not enough and the user needs to read more detailed content.
Big Picture Style Notification is used when a notification needs to display a large image in its expanded layout. Developers can use `NotificationCompat.BigPictureStyle` and provide a bitmap image that can be displayed as part of the notification. This style is useful for image-focused notifications where visual content is more important than a large amount of text.
Android applications can use `AlarmManager` to schedule notification-related actions for a particular date or time. The scheduled event can trigger a `BroadcastReceiver`, which receives the system event and creates the notification even when the application is not currently open. This approach is useful for reminders, scheduled events, and other notifications that need to appear at a specific time.
A `BroadcastReceiver` allows an Android application to respond to system or application events when they occur. When a registered event happens, the Android runtime notifies the appropriate receiver, which can then perform a specific action such as creating a scheduled notification. The receiver class must also be registered in the application's `AndroidManifest.xml` so Android knows that it should respond to the relevant event.
Want to Scale Your Business? Let’s Meet & Discuss!
CANADA
30 Eglinton Ave W Mississauga, Ontario L5R 3E7
INDIA
3rd floor Purusharth Plaza, Amin Marg, Rajkot, Gujarat. 360002
INDIA
1116, Zion Z1, Sindhu Bhavan Marg, Nr. Maple County Road, Bodakdev, Ahmedabad, Gujarat. 380059
Get a Quote Now
Let's delve into a thorough understanding of your challenges and explore potential solutions together