articles

Home / DeveloperSection / Articles / Notifications in Android

Notifications in Android

Manoj Pandey4979 04-Mar-2015

Notification is a message that doesn’t appear inside the main UI of the app but as an icon in the notification area, including more details in the notification drawer.

A notification is a message you can display to the user outside of your application's normal UI.   Notifications are used to alert users on some events that requires their attention.

Notifications alert the users through various forms:

  •       Display a status bar icon
  •       Flash the LED
  •       Vibrate
  •       Ringtones

You can also show Toast notification i.e.

Toast.makeText(getApplicationContext(), "My Message", Toast.LENGTH_LONG).show();

  1.      Create an Android Project
  2.     Add button in activity_main.xml

 

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity">

 

    <Button

        android:id="@+id/btnNotif"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Show Notification"/>

 

</RelativeLayout>

 

3.      Create another layout for notification view

 

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="horizontal">

 

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text=" Your notification test is successfull !"

        android:textSize="18sp"/>

 

</LinearLayout>

4.       Now add following code in MainActivity.class

 

package com.example.androidnotification;

 

import android.os.Bundle;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.support.v4.app.NotificationCompat;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

publicclass MainActivity extends Activity {

     NotificationManager notificationManager;

     staticintnotificationID = 100;

    

     Button btnMeaasge;

 

     @Override

     protectedvoid onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

           btnMeaasge = (Button) findViewById(R.id.btnNotif);

           btnMeaasge.setOnClickListener(new OnClickListener() {

 

                @Override

                publicvoid onClick(View v) {

                     // TODO Auto-generated method stub

                     displayMyNotification();

                }

           });

 

     }

 

     @Override

     publicboolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if //it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           returntrue;

     }

 

   // This method shows notification

   publicvoid displayMyNotification() {

   NotificationCompat.Builder builder = new NotificationCompat.Builder(

                     getApplicationContext());

 

           // set main title

           builder.setContentTitle("Demo Test");

 

           // set notification text

           builder.setContentText("You have received a message");

 

           // set Sticker for alert message

           builder.setTicker("New Message Alert!");

 

           // set icon

           builder.setSmallIcon(R.drawable.ic_launcher);

           // Use default sound for notification

           builder.setDefaults(Notification.DEFAULT_SOUND);

 

          notificationManager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);

          

           // A PendingIntent specifies an action to take in the

           // future

           PendingIntent myIntent = PendingIntent.getActivity(this, 0,new Intent(  this, MyNotification.class), 0);

          

           // on notification click open myIntent

           builder.setContentIntent(myIntent);

 

     /* Update the existing notification using same notification ID */

          notificationManager.notify(notificationID, builder.build());

 

     }

}

 

5.      Create MyNotification Activity and paste following code

 

package com.example.androidnotification;

 

import android.app.Activity;

import android.app.NotificationManager;

import android.os.Bundle;

import android.widget.Toast;

 

public class MyNotification extends Activity {

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           // TODO Auto-generated method stub

           super.onCreate(savedInstanceState);

           setContentView(R.layout.notif);

                          

           // Remove notification after open this activity

        NotificationManager notificationManager = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);

           notificationManager.cancel(MainActivity.notificationID);

     }

 

}

 

Now run your Application

 

  Notifications in Android  Notifications in Android

 

  

    After click on notification

 

   Notifications in Android

 

   


Updated 07-Sep-2019

Leave Comment

Comments

Liked By