SlideShare a Scribd company logo
1 of 12
Notification :
 A service, running in the background, needs a way to let users know something of
interest has occurred, such as when email has been received.
 We used toast to show details for some actions but they are not persistent to show for longer time. And
another activity which is going on background.
 For important messages to be given to the user, it is required to have more persistent method. A
notification is a message you can display as an icon at the top of the device which we call notification bar or
status bar.
 To see the details of the notification, you will have to select the icon from the android phone, tablet
or other devices which will display notification drawer having detail about the notification.
 we have to set Two type of notification view.
1. Normal View
2. Big View
Normal View :
 While working with emulator with virtual device, you will have to click and drag down the status
bar to expand it which will give you detail as follows. This will be just 64 dp tall and called normal view.
Big View :
 Big View which will have additional detail about the notification. You can add up to six additional lines in the
notification. The following screenshot shows such notification.
Creating a Notification in android :
 For making or create a notification in android we have to do following steps :
1. Create Notification Builder
2. Setting Notification Properties
3. Attach Actions
4. Issue the notification
Create Notification Builder :
 The first step is to create a notification builder using NotificationCompat.Builder.build(). You 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);
Setting Notification Properties :
 Once you have Builder object, you can set its Notification properties using Builder object as per your
requirement. But this is mandatory to set at least following:
A small icon, set by setSmallIcon()
A title, set by setContentTitle()
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!");
Attach Actions :
 This is an optional part and required if you want to attach an action with the notification. An action allows
users to go directly from the notification to an Activity in your 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 your application. To
associate the PendingIntent with a gesture, call the appropriate method of NotificationCompat.Builder. For
example, if you want to start Activity when the user clicks the notification text in the notification drawer, you
add the PendingIntent by calling setContentIntent().
 A PendingIntent object helps you to perform an action on your application’s behalf, often at a later
time, without caring of whether or not your application is running.
 Example :
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Issue the notification :
 Finally, you pass the Notification object to the system by calling NotificationManager.notify() to send your
notification. Make sure you 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);
// notificationID allows you to update the notification later on.
mNotificationManager.notify(notificationID, mBuilder.build());
The NotificationCompat.Builder class allows easier control over all the flags, as well as help
constructing the typical notification layouts. Following are few important and most frequently used
methods available as a part of NotificationCompat.Builder class :
 Notification build() :
Combine all of the options that have been set and return a new Notification object.
 NotificationCompat.Builder setAutoCancel (boolean autoCancel) :
Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel.
 NotificationCompat.Builder setContent (RemoteViews views) :
Supply a custom RemoteViews to use instead of the standard one.
 NotificationCompat.Builder setContentInfo (CharSequence info) :
Set the large text at the right-hand side of the notification.
 NotificationCompat.Builder setContentIntent (PendingIntentintent) :
Supply a PendingIntent to send when the notification is clicked.
 NotificationCompat.Builder setContentText (CharSequence text) :
Set the text (second row) of the notification, in a standard notification.
 NotificationCompat.Builder setContentTitle (CharSequence title) :
Set the text (first row) of the notification, in a standard notification.
 NotificationCompat.Builder setDefaults (int defaults) :
Set the default notification options that will be used.
 NotificationCompat.Builder setLargeIcon (Bitmap icon) :
Set the large icon that is shown in the ticker and notification.
 NotificationCompat.Builder setNumber (int number) :
Set the large number at the right-hand side of the notification.
 NotificationCompat.Builder setOngoing (boolean ongoing) :
Set whether this is an ongoing notification.
 NotificationCompat.Builder setSmallIcon (int icon) :
Set the small icon to use in the notification layouts.
 NotificationCompat.Builder setStyle (NotificationCompat.Stylestyle) :
Add a rich notification style to be applied at build time.
 NotificationCompat.Builder setTicker (CharSequence tickerText) :
Set the text that is displayed in the status bar when the notification first arrives.
 NotificationCompat.Builder setVibrate (long[] pattern) :
Set the vibration pattern to use.
 NotificationCompat.Builder setWhen (long when) :
Set the time that the event occurred. Notifications in the panel are sorted by this time.
Push Notification :
 Push Notification is used to create your own notifications in android very easily.
 Android provides NotificationManager class for this purpose.
 In order to use this class, you need to instantiate an object of this class by requesting the android system
through getSystemService() method. Its syntax is given below:
NotificationManager NM;
NM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
 After that you will create Notification through Notification class and specify its attributes such as
icon, title and time etc. Its syntax is given below:
Notification notify=new
Notification(android.R.drawable.stat_notify_more,title,System.currentTimeMillis());
 The next thing you need to do is to create a PendingIntent by passing context and intent as a parameter.
parameter. By giving a PendingIntent to another application, you are granting it the right to perform the
operation you have specified as if the other application was yourself.
PendingIntent pending=PendingIntent.getActivity(getApplicationContext(), 0, new Intent(),0);
 The last thing you need to do is to call setLatestEventInfo method of the Notification class and pass the
pending intent along with notification subject and body details. Its syntax is given below. And then finally
call the notify method of the NotificationManager class.
notify.setLatestEventInfo(getApplicationContext(), subject, body,pending);
NM.notify(0, notify);
 Apart from the notify method, there are other methods available in the NotificationManager class. They
are listed below:
cancel(int id) :
This method cancels a previously shown notification.
cancel(String tag, int id) :
This method also cancels a previously shown notification.
cancelAll() :
This method cancels all previously shown notifications.
notify(int id, Notification notification) :
This method posts a notification to be shown in the status bar.
notify(String tag, int id, Notification notification) :
This method also Post a notification to be shown in the status bar.

More Related Content

What's hot

Intents in Android
Intents in AndroidIntents in Android
Intents in Androidma-polimi
 
Events and Listeners in Android
Events and Listeners in AndroidEvents and Listeners in Android
Events and Listeners in Androidma-polimi
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in JavaTushar B Kute
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in javaGoogle
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)slire
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & viewsma-polimi
 
Android share preferences
Android share preferencesAndroid share preferences
Android share preferencesAjay Panchal
 
Android animation
Android animationAndroid animation
Android animationKrazy Koder
 
Maps in android
Maps in androidMaps in android
Maps in androidSumita Das
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)Manisha Keim
 
Asp.NET Validation controls
Asp.NET Validation controlsAsp.NET Validation controls
Asp.NET Validation controlsGuddu gupta
 

What's hot (20)

Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Android
 
SQLITE Android
SQLITE AndroidSQLITE Android
SQLITE Android
 
Lab manual asp.net
Lab manual asp.netLab manual asp.net
Lab manual asp.net
 
Events and Listeners in Android
Events and Listeners in AndroidEvents and Listeners in Android
Events and Listeners in Android
 
Android User Interface
Android User InterfaceAndroid User Interface
Android User Interface
 
Broadcast Receiver
Broadcast ReceiverBroadcast Receiver
Broadcast Receiver
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Action Bar in Android
Action Bar in AndroidAction Bar in Android
Action Bar in Android
 
android menus
android menusandroid menus
android menus
 
Android Intent.pptx
Android Intent.pptxAndroid Intent.pptx
Android Intent.pptx
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
 
Android share preferences
Android share preferencesAndroid share preferences
Android share preferences
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Android animation
Android animationAndroid animation
Android animation
 
Maps in android
Maps in androidMaps in android
Maps in android
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Asp.NET Validation controls
Asp.NET Validation controlsAsp.NET Validation controls
Asp.NET Validation controls
 

Similar to Notification android

Local Notification Tutorial
Local Notification TutorialLocal Notification Tutorial
Local Notification TutorialKetan Raval
 
Android Wearables ii
Android Wearables iiAndroid Wearables ii
Android Wearables iiKetan Raval
 
Implementation of Push Notification in React Native Android app using Firebas...
Implementation of Push Notification in React Native Android app using Firebas...Implementation of Push Notification in React Native Android app using Firebas...
Implementation of Push Notification in React Native Android app using Firebas...naseeb20
 
Create an other activity lesson 3
Create an other activity lesson 3Create an other activity lesson 3
Create an other activity lesson 3Kalluri Vinay Reddy
 
Session #8 adding magic to your app
Session #8  adding magic to your appSession #8  adding magic to your app
Session #8 adding magic to your appVitali Pekelis
 
Android Wear – IO Extended
Android Wear – IO ExtendedAndroid Wear – IO Extended
Android Wear – IO ExtendedDouglas Drumond
 
MobileAppDev Handout#6
MobileAppDev Handout#6MobileAppDev Handout#6
MobileAppDev Handout#6trupti1976
 
Android notifications
Android notificationsAndroid notifications
Android notificationsKetan Raval
 
3.5 the controls object
3.5   the controls object3.5   the controls object
3.5 the controls objectallenbailey
 
Android Notifications in Android Nougat 7.0
Android Notifications in Android Nougat 7.0Android Notifications in Android Nougat 7.0
Android Notifications in Android Nougat 7.0Gracia Marcom
 
Exercises broadcast receiver,incoming phone call
Exercises broadcast receiver,incoming phone callExercises broadcast receiver,incoming phone call
Exercises broadcast receiver,incoming phone callmaamir farooq
 
Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAhsanul Karim
 
Developing for android wear
Developing for android wearDeveloping for android wear
Developing for android wearThomas Oldervoll
 
Keynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptxKeynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptxEqraKhattak
 

Similar to Notification android (20)

Local Notification Tutorial
Local Notification TutorialLocal Notification Tutorial
Local Notification Tutorial
 
Android Wearables ii
Android Wearables iiAndroid Wearables ii
Android Wearables ii
 
Implementation of Push Notification in React Native Android app using Firebas...
Implementation of Push Notification in React Native Android app using Firebas...Implementation of Push Notification in React Native Android app using Firebas...
Implementation of Push Notification in React Native Android app using Firebas...
 
Notifications
NotificationsNotifications
Notifications
 
Android action bar and notifications-chapter16
Android action bar and notifications-chapter16Android action bar and notifications-chapter16
Android action bar and notifications-chapter16
 
Create an other activity lesson 3
Create an other activity lesson 3Create an other activity lesson 3
Create an other activity lesson 3
 
Introduction to Android Wear
Introduction to Android WearIntroduction to Android Wear
Introduction to Android Wear
 
Session #8 adding magic to your app
Session #8  adding magic to your appSession #8  adding magic to your app
Session #8 adding magic to your app
 
Android Wear – IO Extended
Android Wear – IO ExtendedAndroid Wear – IO Extended
Android Wear – IO Extended
 
MobileAppDev Handout#6
MobileAppDev Handout#6MobileAppDev Handout#6
MobileAppDev Handout#6
 
Android notifications
Android notificationsAndroid notifications
Android notifications
 
3.5 the controls object
3.5   the controls object3.5   the controls object
3.5 the controls object
 
Android Notifications in Android Nougat 7.0
Android Notifications in Android Nougat 7.0Android Notifications in Android Nougat 7.0
Android Notifications in Android Nougat 7.0
 
How to create android push notifications with custom view
How to create android push notifications with custom viewHow to create android push notifications with custom view
How to create android push notifications with custom view
 
Exercises broadcast receiver,incoming phone call
Exercises broadcast receiver,incoming phone callExercises broadcast receiver,incoming phone call
Exercises broadcast receiver,incoming phone call
 
Lab1-android
Lab1-androidLab1-android
Lab1-android
 
Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form Widgets
 
Developing for android wear
Developing for android wearDeveloping for android wear
Developing for android wear
 
Android UI
Android UIAndroid UI
Android UI
 
Keynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptxKeynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptx
 

Recently uploaded

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 

Recently uploaded (20)

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 

Notification android

  • 1. Notification :  A service, running in the background, needs a way to let users know something of interest has occurred, such as when email has been received.  We used toast to show details for some actions but they are not persistent to show for longer time. And another activity which is going on background.  For important messages to be given to the user, it is required to have more persistent method. A notification is a message you can display as an icon at the top of the device which we call notification bar or status bar.  To see the details of the notification, you will have to select the icon from the android phone, tablet or other devices which will display notification drawer having detail about the notification.  we have to set Two type of notification view. 1. Normal View 2. Big View
  • 2. Normal View :  While working with emulator with virtual device, you will have to click and drag down the status bar to expand it which will give you detail as follows. This will be just 64 dp tall and called normal view. Big View :  Big View which will have additional detail about the notification. You can add up to six additional lines in the notification. The following screenshot shows such notification.
  • 3. Creating a Notification in android :  For making or create a notification in android we have to do following steps : 1. Create Notification Builder 2. Setting Notification Properties 3. Attach Actions 4. Issue the notification Create Notification Builder :  The first step is to create a notification builder using NotificationCompat.Builder.build(). You 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);
  • 4. Setting Notification Properties :  Once you have Builder object, you can set its Notification properties using Builder object as per your requirement. But this is mandatory to set at least following: A small icon, set by setSmallIcon() A title, set by setContentTitle() 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!"); Attach Actions :  This is an optional part and required if you want to attach an action with the notification. An action allows users to go directly from the notification to an Activity in your 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 your application. To associate the PendingIntent with a gesture, call the appropriate method of NotificationCompat.Builder. For example, if you want to start Activity when the user clicks the notification text in the notification drawer, you add the PendingIntent by calling setContentIntent().
  • 5.  A PendingIntent object helps you to perform an action on your application’s behalf, often at a later time, without caring of whether or not your application is running.  Example : Intent resultIntent = new Intent(this, ResultActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(ResultActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent);
  • 6. Issue the notification :  Finally, you pass the Notification object to the system by calling NotificationManager.notify() to send your notification. Make sure you 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); // notificationID allows you to update the notification later on. mNotificationManager.notify(notificationID, mBuilder.build()); The NotificationCompat.Builder class allows easier control over all the flags, as well as help constructing the typical notification layouts. Following are few important and most frequently used methods available as a part of NotificationCompat.Builder class :
  • 7.  Notification build() : Combine all of the options that have been set and return a new Notification object.  NotificationCompat.Builder setAutoCancel (boolean autoCancel) : Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel.  NotificationCompat.Builder setContent (RemoteViews views) : Supply a custom RemoteViews to use instead of the standard one.  NotificationCompat.Builder setContentInfo (CharSequence info) : Set the large text at the right-hand side of the notification.  NotificationCompat.Builder setContentIntent (PendingIntentintent) : Supply a PendingIntent to send when the notification is clicked.  NotificationCompat.Builder setContentText (CharSequence text) : Set the text (second row) of the notification, in a standard notification.  NotificationCompat.Builder setContentTitle (CharSequence title) : Set the text (first row) of the notification, in a standard notification.
  • 8.  NotificationCompat.Builder setDefaults (int defaults) : Set the default notification options that will be used.  NotificationCompat.Builder setLargeIcon (Bitmap icon) : Set the large icon that is shown in the ticker and notification.  NotificationCompat.Builder setNumber (int number) : Set the large number at the right-hand side of the notification.  NotificationCompat.Builder setOngoing (boolean ongoing) : Set whether this is an ongoing notification.  NotificationCompat.Builder setSmallIcon (int icon) : Set the small icon to use in the notification layouts.  NotificationCompat.Builder setStyle (NotificationCompat.Stylestyle) : Add a rich notification style to be applied at build time.  NotificationCompat.Builder setTicker (CharSequence tickerText) : Set the text that is displayed in the status bar when the notification first arrives.
  • 9.  NotificationCompat.Builder setVibrate (long[] pattern) : Set the vibration pattern to use.  NotificationCompat.Builder setWhen (long when) : Set the time that the event occurred. Notifications in the panel are sorted by this time.
  • 10. Push Notification :  Push Notification is used to create your own notifications in android very easily.  Android provides NotificationManager class for this purpose.  In order to use this class, you need to instantiate an object of this class by requesting the android system through getSystemService() method. Its syntax is given below: NotificationManager NM; NM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);  After that you will create Notification through Notification class and specify its attributes such as icon, title and time etc. Its syntax is given below: Notification notify=new Notification(android.R.drawable.stat_notify_more,title,System.currentTimeMillis());  The next thing you need to do is to create a PendingIntent by passing context and intent as a parameter. parameter. By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself. PendingIntent pending=PendingIntent.getActivity(getApplicationContext(), 0, new Intent(),0);
  • 11.  The last thing you need to do is to call setLatestEventInfo method of the Notification class and pass the pending intent along with notification subject and body details. Its syntax is given below. And then finally call the notify method of the NotificationManager class. notify.setLatestEventInfo(getApplicationContext(), subject, body,pending); NM.notify(0, notify);  Apart from the notify method, there are other methods available in the NotificationManager class. They are listed below: cancel(int id) : This method cancels a previously shown notification. cancel(String tag, int id) : This method also cancels a previously shown notification. cancelAll() : This method cancels all previously shown notifications.
  • 12. notify(int id, Notification notification) : This method posts a notification to be shown in the status bar. notify(String tag, int id, Notification notification) : This method also Post a notification to be shown in the status bar.