Android Notification

  • Android notification is used to show the message or email when any new message comes to mobile phone then the message icon is displayed on the top of the mobile screen. It is called as notification.
  • When any notification comes then first their icon is shown on the top of the device. For example, if any mail came then email icon will displayed and after that if we want to open that email then we have to open the notification drawer and click on that icon.
  • In most devices, the notification drawer is placed at the top of the device but we can set the notification drawer at the bottom of the device by changing the setting of the notification drawer.
  • When we swipe the notification drawer from top to bottom then we can see the some icons such as Bluetooth, Wifi, screen brightness, settings icon etc.
  • If we want to perform any action by using this then we have to click on that particular icon means if we want to change the call settings then we have to click on the setting icon then the all setting options are displayed then we can choose the call setting and we can also take other actions.

Content of the Android Notification

  • Icon: it is a small icon that is used to represent the application related to the notification.
  • Title: it is used to set the title of the notification which can be used to show the purpose of the notification.
  • Detail Text: it is used to display the message.

How to create the Notification in Android

  • To create the notification, first we have to create the object of the NotificationCompact.Builder class. It is a public class.
  • We can create the notification easily by creating the object of Builder class in which we have to pass the reference of the current activity.
  • After that we have to set the contents of the notification by using the builder class object.
  • To set the icon for the notification, we use the setSmallIcon() method and pass the address of the icon which we want to display.
  • To set the title of the notification, we use the setContentTitle() method and the pass the text stream.
  • To set the message, we use the setContentText() method and pass the message in the form of string.
  • After that we have to call the build() method by using the object of the NotificationCompact.Builder class. This method return a notification and loaded with that contents which we have defined.
  • After that to issue the notification, we have to pass the returned build object by using the Notification class object means we have to assign the build() method into the reference variable of type Notification.
  • After that we have to pass the reference of getSystemService() method to it because we cannot create the object of the NotificationManager class directly.
  • Now we have to call the notify() method by using the NotificationManager class object and we have to pass the notification id as integer, and object of the Notification class.
  • We can create a notification using the following syntax:
NotificationCompat.Builder ncb = new NotificationCompat.Builder(this);
//crating the builder class object
ncb.setSmallIcon(R.drawable.yourIcon); // setting the icon
ncb.setContentTitle(“notification title"); //setting title
ncb.setContextText(" notification message");// setting text
Notification noti = ncb.build(); //creating notification
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0,noti); //showing notification