SlideShare a Scribd company logo
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

Android activity
Android activityAndroid activity
Android activityKrazy Koder
 
Android User Interface
Android User InterfaceAndroid User Interface
Android User Interface
Shakib Hasan Sumon
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
Aly Abdelkareem
 
[Android] Widget Event Handling
[Android] Widget Event Handling[Android] Widget Event Handling
[Android] Widget Event Handling
Nikmesoft Ltd
 
Event handling
Event handlingEvent handling
Event handling
swapnac12
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Android
ma-polimi
 
UI controls in Android
UI controls in Android UI controls in Android
UI controls in Android
DivyaKS12
 
Android Components
Android ComponentsAndroid Components
Android Components
Aatul Palandurkar
 
Web controls
Web controlsWeb controls
Web controls
Sarthak Varshney
 
Android Toast.pdf
Android Toast.pdfAndroid Toast.pdf
Android Toast.pdf
John Benetic
 
Android application development ppt
Android application development pptAndroid application development ppt
Android application development ppt
Gautam Kumar
 
Android App Development - 05 Action bar
Android App Development - 05 Action barAndroid App Development - 05 Action bar
Android App Development - 05 Action bar
Diego Grancini
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
ma-polimi
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Ahsanul Karim
 
android layouts
android layoutsandroid layouts
android layoutsDeepa Rani
 
Broadcast Receiver
Broadcast ReceiverBroadcast Receiver
Broadcast Receiver
nationalmobileapps
 

What's hot (20)

Android Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
 
Android activity
Android activityAndroid activity
Android activity
 
Android User Interface
Android User InterfaceAndroid User Interface
Android User Interface
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
 
[Android] Widget Event Handling
[Android] Widget Event Handling[Android] Widget Event Handling
[Android] Widget Event Handling
 
Event handling
Event handlingEvent handling
Event handling
 
Android intents
Android intentsAndroid intents
Android intents
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Android
 
UI controls in Android
UI controls in Android UI controls in Android
UI controls in Android
 
Android Components
Android ComponentsAndroid Components
Android Components
 
Web controls
Web controlsWeb controls
Web controls
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
 
Android Toast.pdf
Android Toast.pdfAndroid Toast.pdf
Android Toast.pdf
 
Android application development ppt
Android application development pptAndroid application development ppt
Android application development ppt
 
Android App Development - 05 Action bar
Android App Development - 05 Action barAndroid App Development - 05 Action bar
Android App Development - 05 Action bar
 
Android intent
Android intentAndroid intent
Android intent
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
 
android layouts
android layoutsandroid layouts
android layouts
 
Broadcast Receiver
Broadcast ReceiverBroadcast Receiver
Broadcast Receiver
 

Similar to Notification android

Local Notification Tutorial
Local Notification TutorialLocal Notification Tutorial
Local Notification Tutorial
Ketan Raval
 
Android Wearables ii
Android Wearables iiAndroid Wearables ii
Android Wearables ii
Ketan 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
 
Notifications
NotificationsNotifications
Notifications
Youssef ELBOUZIANI
 
Android action bar and notifications-chapter16
Android action bar and notifications-chapter16Android action bar and notifications-chapter16
Android action bar and notifications-chapter16
Dr. Ramkumar Lakshminarayanan
 
Create an other activity lesson 3
Create an other activity lesson 3Create an other activity lesson 3
Create an other activity lesson 3
Kalluri Vinay Reddy
 
Introduction to Android Wear
Introduction to Android WearIntroduction to Android Wear
Introduction to Android Wear
Emanuele De Bernardi
 
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
Vitali Pekelis
 
Android Wear – IO Extended
Android Wear – IO ExtendedAndroid Wear – IO Extended
Android Wear – IO Extended
Douglas Drumond
 
MobileAppDev Handout#6
MobileAppDev Handout#6MobileAppDev Handout#6
MobileAppDev Handout#6
trupti1976
 
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.0
Gracia Marcom
 
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
PushApps - Content Recommendation in Push Notifications
 
Exercises broadcast receiver,incoming phone call
Exercises broadcast receiver,incoming phone callExercises broadcast receiver,incoming phone call
Exercises broadcast receiver,incoming phone call
maamir farooq
 
Lab1-android
Lab1-androidLab1-android
Lab1-android
Lilia Sfaxi
 
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 wear
Thomas Oldervoll
 
Keynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptxKeynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptx
EqraKhattak
 
Day 4: Android: UI Widgets
Day 4: Android: UI WidgetsDay 4: Android: UI Widgets
Day 4: Android: UI WidgetsAhsanul Karim
 

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
 
Keynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptxKeynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptx
 
Day 4: Android: UI Widgets
Day 4: Android: UI WidgetsDay 4: Android: UI Widgets
Day 4: Android: UI Widgets
 

Recently uploaded

Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 

Recently uploaded (20)

Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 

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.