0% found this document useful (0 votes)
61 views4 pages

Notifications

The document discusses different types of notifications in Android mobile application development including status bar, notification drawer, heads-up, and lock-screen notifications. It provides steps for creating and sending notifications using NotificationCompat.Builder including setting properties, attaching actions, and issuing the notification.

Uploaded by

dharshujaym
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views4 pages

Notifications

The document discusses different types of notifications in Android mobile application development including status bar, notification drawer, heads-up, and lock-screen notifications. It provides steps for creating and sending notifications using NotificationCompat.Builder including setting properties, attaching actions, and issuing the notification.

Uploaded by

dharshujaym
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Mobile Application Development

UNIT II
Notifications
 Notification is a kind of message, alert, or status of an
application (probably running in the background) that is
visible or available in the Android’s UI elements.
 This application could be running in the background but
not in use by the user.
 The purpose of a notification is to notify the user about
a process that was initiated in the application either by
the user or the system
 Notifications could be of various formats and designs
depending upon the developer.
 There are 4 types of notifications:

1. Status Bar Notification - appears in the same


layout as the current time, battery percentage
2. Notification drawer Notification - appears in the
drop-down menu
3. Heads-Up Notification - appears on the overlay
screen, ex: Whatsapp notification, OTP messages
4. Lock-Screen Notification
 The properties of Android notification are set
using NotificationCompat.Builder object.
 Some of the notification properties are
 setSmallIcon(): It sets the icon of notification.
 setContentTitle(): It is used to set the title of
notification.
 setContentText(): It is used to set the text message.
 setAutoCancel(): It sets the cancelable property of
notification.
 setPriority(): It sets the priority of notification.

Create and Send Notifications

 Steps to create a notification

Step 1 - Create Notification Builder

 As a first step is to create a notification builder


using NotificationCompat.Builder.build().
 will use Notification Builder to set various Notification
properties like its small and large icons, title, priority etc.
NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(this)
Step 2 - Setting Notification Properties

 Once craeate Builder object, can set its Notification


properties using Builder object as per requirement.
 But this is mandatory to set at least following
o A small icon, set by setSmallIcon()
o A title, set by setContentTitle()
o Detail text, set by setContentText()

mBuilder.setSmallIcon(R.drawable.notification_icon);

mBuilder.setContentTitle("Notification Alert, Click Me!");


mBuilder.setContentText("Hi, This is Android
Notification Detail!");

Step 3 - Attach Actions

 This is an optional part and required if user wants to


attach an action with the notification.
 An action allows users to go directly from the
notification to an Activity in application, where they can
look at one or more events or do further work.
 The action is defined by a PendingIntent containing
an Intent that starts an Activity in the application.
 To associate the PendingIntent with a gesture, call the
appropriate method of NotificationCompat.Builder.
 For example, if want to start Activity when the user
clicks the notification text in the notification drawer, add
the PendingIntent by calling setContentIntent().
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder =
TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);

Step 4 - Issue the notification

 Finally, pass the Notification object to the system by


calling NotificationManager.notify() to send notification.
 Make sure
call NotificationCompat.Builder.build() method on
builder object before notifying it.
 This method combines all of the options that have been
set and return a new Notification object.

NotificationManager mNotificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationID, mBuilder.build());

You might also like