articles

Home / DeveloperSection / Articles / Notification example in Android

Notification example in Android

Manoj Pandey3519 21-Aug-2015

Android provides notification for user information alert. It can toast notification or push notification. A notification is a message you can display to the user outside of your application's normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time.

See toast notification

Notification example in Android

See the push notification

Notification example in Android

 

Here I am creating a sample of push notification. I hope it will be helpful for
android users. Follow below steps to create sample

1.      Create an android project in eclipse or android studio.

2.      Create an activity which show on tap push notification and add this activity on
manifest file 
package com.example.androidnotification; 
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;

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);
            }

}

3. Xml layout for My Notification activity

<?

xml

version="1.0" encoding="utf-8"?>

<LinearLayout xmlns: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.   Xml layout for Main Activity 

<RelativeLayout xmlns: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"

    tools:context=".MainActivity" >

 

    <Button

        android:id="@+id/btnNotif"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Pop up Noification" />

 

</RelativeLayout>


5.      Add following code in MainActivity.class 

package com.example.androidnotification;

 

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.os.Bundle;

import android.support.v4.app.NotificationCompat;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

public class MainActivity extends Activity {

     NotificationManager notificationManager;

     // notification id

     static int notificationID = 100;

     int numMessagesOne = 0;

 

     Button btnMeaasge;

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

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

           btnMeaasge.setOnClickListener(new OnClickListener() {

 

                @Override

                public void onClick(View v) {

                     // TODO Auto-generated method stub

                     displayMyNotification();

                     // displayNotificationOne();

                }

           });

 

     }

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

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

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

           return true;

     }

 

     // For show notification method

     public void displayMyNotification() {

 NotificationCompat.Builder builder = new NotificationCompat.Builder(

                     getApplicationContext());

 

           // set main title

       builder.setContentTitle("Android Push Notification");

 

           // set notification text

     builder.setContentText("Welcome in Mindstick Developer Section");

 

           // 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);

 

           // set number of messages

           builder.setNumber(++numMessagesOne);

 

         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());

 

     }

 

}

 

Now run your application.

Notification example in Android


Updated 07-Sep-2019

Leave Comment

Comments

Liked By