Notifications in Android Using Android Studio

Introduction

 
You must have seen various notifications in an Android phone for messages, mails, WhatsApp and so on. So in this article, you will learn how to create a simple notification.
A notification is a message you can display to the user outside of your application's normal UI. A notification appears as an icon in the notification area. Further to have more information, the user can open the notification drawer. Both the notification area and notification drawer are system-controlled; that is, they can be viewed by the user at any time. 
 
Step 1
 
Open "activity_main" and add the following code to it:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity"  
  10.     android:background="#6b8e47">  
  11.     <Button  
  12.             android:id="@+id/button1"  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:onClick="make"  
  16.             android:text="Create Notification"  
  17.             android:paddingLeft="10dp"  
  18.             android:paddingRight="10dp"  
  19.             android:layout_marginTop="150dp"  
  20.             android:layout_marginLeft="80dp"  
  21.             android:background="#cc9744">  
  22.     </Button>  
  23. </LinearLayout>  
In the code above, clicking on the button will create a notification.
 
The layout looks like:
 
im1.jpg
 
Step 2
 
Create a layout file for showing the notification.
 
Right-click on layout then select "New" -> "Layout Resource file". Name this file "notification_layout" and add the following code to it:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.               android:layout_width="match_parent"  
  3.               android:layout_height="match_parent"  
  4.               android:orientation="vertical"  
  5.               android:background="#454545">  
  6.     <TextView  
  7.             android:id="@+id/textView1"  
  8.             android:layout_width="wrap_content"  
  9.             android:layout_height="wrap_content"  
  10.             android:text="Hii..."  
  11.             android:textSize="30dp"  
  12.             android:layout_marginTop="50dp"  
  13.             android:layout_marginLeft="30dp"  
  14.             android:textColor="#FFFFFF"/>  
  15.     <TextView  
  16.             android:id="@+id/textView2"  
  17.             android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content"  
  19.             android:text="My first notification...."  
  20.             android:textSize="30dp"  
  21.             android:layout_marginLeft="30dp"  
  22.             android:layout_marginTop="50dp"  
  23.             android:textColor="#FFFFFF"/>  
  24. </LinearLayout> 
This layout file will be displayed when the user clicks on the notification.
 
The layout looks like:
 
im2.jpg
 
Step 3
 
Open "MainActivity" and add the following code to it:
  1. package com.chhavi.notifications;  
  2.    
  3. import android.app.Notification;  
  4. import android.app.NotificationManager;  
  5. import android.app.PendingIntent;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.app.Activity;  
  9. import android.view.View;  
  10.    
  11. public class MainActivity extends Activity {  
  12.    
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.    
  16.             super.onCreate(savedInstanceState);  
  17.             setContentView(R.layout.activity_main);  
  18.         }  
  19.    
  20.     public void make(View view) {  
  21.          
  22.         Intent intent = new Intent(this, DisplayNotification.class);  
  23.         PendingIntent pending = PendingIntent.getActivity(this0, intent, 0);  
  24.    
  25.         Notification notifications = new Notification.Builder(this)  
  26.                 .setContentTitle("New notification like message from")  
  27.                 .setContentText("Hii").setSmallIcon(R.drawable.notification)  
  28.                 .setContentIntent(pending)  
  29.                 .addAction(R.drawable.reply, "Reply", pending)  
  30.                 .addAction(R.drawable.cancel, "cancel", pending)  
  31.                 .addAction(R.drawable.settings, "setings", pending).build();  
  32.    
  33.         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);       
  34.         notifications.flags |= Notification.FLAG_AUTO_CANCEL;  
  35.         manager.notify(0, notifications);  
  36.    
  37.     }  

According to the code above, the "DisplayNotification" activity will be loaded when the notification will be selected.
 
The image named "notification" will be displayed as an icon on the notification area when the notification is created.
 
On opening the notification drawer:
  • "setContentTitle" will set the title of the notification.
  • "setContentText" will be the second row of the notification description
  • "addAction" is to add any other action that is required.
For example: for a calling notification, answer and disconnect are two actions. A notification can display up to 3 actions.
 
"Notification.FLAG_AUTO_CANCEL" will remove the notification from the notification area once it has been selected.
 
Step 4
 
Create a new Java class in the same package. Name it "DisplayNotification" and add the following code to it:
  1. package com.chhavi.notifications;  
  2.    
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.    
  6. public class DisplayNotification extends Activity {  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.notification_layout);  
  11.     }  
  12. }  
Step 5
 
Open "AndroidManifest" and add the following code to it:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.chhavi.notifications"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="16"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.chhavi.notifications.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.         </activity>  
  24.         <activity android:name=".DisplayNotification"  
  25.                   android:label="Display Notification"/>  
  26.     </application>  
  27. </manifest>  
Note that minimum SDK version required by "Notification.Builder" is 11 and by "addAction" is 16.
 
Output snapshots:
 
im3.jpg
 
Clicking on the button (Notification icon displayed):
 
im4.jpg
 
Notification drawer pulled:
 
im5f.jpg
 
On selecting the notification:
 
im6.jpg
 
Note that in the image above, the notification icon is no longer visible.
 
Thank you.


Similar Articles