Android Notifications

Android Notifications

Android notifications is a note you can screen to an individual beyond your application’s normal User Interface. Whenever you notify the functional system to concern a notification, it seems as an icon in the notification area first. To start to see the information on the notification, the notification is exposed by an individual drawer. Notification area and the notification drawer both are the system-controlled areas that an individual can view anytime.

android notifications

Android OS Toast class offers a useful way showing users notifications but problem is these alerts aren’t persistent this means alert flashes on the display for a couple of seconds and then invisible.

android notifications

To start to see the information on the notification, you will need to choose the icon that may screen notification drawer having details about the notification. While dealing with emulator with exclusive device, you will need to click and move down the position bar to increase it that will give you depth as follows. This can be just 64 dp extra tall and called it is normal view

android notifications

Broadened form (given above) can have a Big View that may have additional fine detail about the android notifications. You can include up-to six additional lines in the notification. The next display shot shows such notification or alerts.

How To Create Android Notifications and Send Notifications

You could have simple way to make a notification. Follow the next steps in the application to make a notification

Step 1 – Create Android Notifications Builder

As an initial step is to make a notification contractor using You use Notification Contractor to create various Notification properties like its small and large symbols, title, concern etc.

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)


Step 2 – Setting Android Notifications Properties

When you’ve Builder object, you can placed its Notification properties by using Builder object according to your need. But this is necessary to create at least following

  • A small icon, set bysetSmallIcon()
  • A title, set bysetContentTitle()
  • Detail text, set bysetContentText()
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi, This is Android Notification Detail!");


You have lots of optional properties which you can establish for your notification. To find out more on them, start to see the reference documents for NotificationCompat.Builder

Step 3 – Attach Actions

This step is an optional part and required if you wish to add an action with the notification. An action allows users to look from the notification to an Activity in the application immediately, where they can look at a number of events or do further work.

The action is described with a PendingIntent comprising an Intent that starts a task/Activity in the application. To relate the PendingIntent with a gesture, call the correct approach to NotificationCompat.Builder. For instance, if you need to start out Activity when an individual clicks the notification wording in the notification drawer, you add the PendingIntent by getting in touch with setContentIntent().

A PendingIntent object allows you to perform an action on your applications behalf, at another time often, without caring of if the application is running.

We take help of stack constructor object that may contain an unnatural backside stack for the began Activity. This means that navigating backward from the experience leads out of the application to the home display screen.

Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the Intent that starts the Activity to the top of the stack
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

Step 4 – Issue the android notifications

At last, you cross the Notification object to the machine by contacting NotificationManager.notify() to send your notification. Be sure you NotificationCompat method on constructor subject before notifying it. This technique combines all the options which may have been arranged and return a fresh Notification object.

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.


Android Notifications Example

Pursuing android notifications example shows the operation of a Android os notification by using a NotificationCompat.Builder School which includes been launched in Android os 4.1.

Steps       Description

1- You use Android studio IDE to generate an Android software and name it as androdevelopment under a deal com.example.notificationdemo. While creating this task, ensure you Focus on SDK and Compile With at the latest version of Google android SDK to utilize higher levels/degrees of APIs.

2- Modify src/ &  add the code to notify(“”), if customer go through the button,it’ll call android notification service.

3- Create a fresh Java data file src/, which is used to show new layout as part of new activity which is started when end user will click the notifications

4- Modify design XML data file res/structure/activity_main.xml to include Notification button in comparative layout.

5- Create a fresh layout XML res/design/notification.xml. This may be utilized as layout apply for new activity that may start when consumer will click the notifications.

6- No need to improve default string constants. Android os studio manages default string constants

7- Run the application form to launch Google android emulator and check the consequence of the changes done in the application form.

Following is the content of the modified main activity file src/com.example.notificationdemo/ This file can include each of the fundamental android life-cycle methods.

package com.example.notificationdemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
   Button b1;
   protected void onCreate(Bundle savedInstanceState) {
      b1.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            Notify("You have received new message");
   private void Notify(String notificationTitle, String notificationMessage){
      NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      Notification notification = new Notification(,"New Message", System.currentTimeMillis());
      Intent notificationIntent = new Intent(this,NotificationView.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
      notification.setLatestEventInfo(MainActivity.this, notificationTitle,notificationMessage, pendingIntent);
      notificationManager.notify(9999, notification);
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(, menu);
      return true;
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      //noinspection SimplifiableIfStatement
      if (id == {
         return true;
      return super.onOptionsItemSelected(item);


Following will be the content of res/layout/notification.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
   android:layout_height="fill_parent" >
      android:text="Hi, Your Detailed notification view goes here...." />


Following is the content of the modified main activity file src/com.example.notificationdemo/

package com.example.notificationdemo;

import android.os.Bundle;

public class NotificationView extends Activity{
   public void onCreate(Bundle savedInstanceState){


Following will be the content of res/layout/activity_main.xml file

<RelativeLayout xmlns:android=""
      android:text="Notification Example"
      android:textSize="30dp" />
      android:text="Andro Development "
      android:layout_marginTop="48dp" />
      android:layout_marginTop="42dp" />
      android:layout_centerHorizontal="true" />


Following will be the content of res/values/strings.xml to define two new constants −

<?xml version="1.0" encoding="utf-8"?>
   <string name="action_settings">Settings</string>
   <string name="app_name">androdevelopment </string>  


Following is the default content of AndroidManifest.xml −

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
   android:versionName="1.0" >
      android:theme="@style/AppTheme" >
         android:label="@string/app_name" >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
      <activity android:name=".NotificationView"
         android:label="Details of notification"


How about we attempt to run our androdevelopment application we simply created. I accept you had made your AVD while doing android environment setup. To run the application from Android studio, open one of your project activity file and snap  Android Studio Run Icon    from the tool bar. Android Studio installs the application on your AVD and begins it and if all is well with your set-up and application, it will show taking after Emulator window

android notifications

Click button now, you will notice at the very top a note “New Subject matter Alert!” will screen momentarily and you then will have pursuing screen having a tiny icon at the very top left corner.

Now enables grow the view, long go through the small icon, after another it will screen date information which is enough time when you should pull status club down without liberating mouse. You shall see position pub will expand and you’ll get pursuing display

android notifications

Big View Android Notifications

The next code snippet shows how to improve the notification created in the last snippet to work with the Inbox big view style. I’ll update displayNotification() adjustment solution to show this efficiency

protected void displayNotification() {
   Log.i("Start", "notification");

   /* Invoking the default notification service */
   NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this);
   mBuilder.setContentTitle("New Message");
   mBuilder.setContentText("You've received new message.");
   mBuilder.setTicker("New Message Alert!");
   /* Increase notification number every time a new notification arrives */
   /* Add Big View Specific Configuration */
   NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
   String[] events = new String[6];
   events[0] = new String("This is first line....");
   events[1] = new String("This is second line...");
   events[2] = new String("This is third line...");
   events[3] = new String("This is 4th line...");
   events[4] = new String("This is 5th line...");
   events[5] = new String("This is 6th line...");
   // Sets a title for the Inbox style big view
   inboxStyle.setBigContentTitle("Large Title Details:");
   // Moves events into the big view
   for (int i=0; i < events.length; i++) {
   /* Creates an explicit intent for an Activity in your app */
   Intent resultIntent = new Intent(this, NotificationView.class);
   TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

   /* Adds the Intent that starts the Activity to the top of the stack */
   PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
   mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
   /* notificationID allows you to update the notification later on. */


Now if you will attempt to run the application then you will see following bring about broadened form of the view