Published on Feb 13,2014
2.5K Views
Email Post


This application requires Android version 4.1!

An Android 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. Android allows you to put notifications into the title-bar of your application. The user can slide the notification bar, select a notification and can start another activity.

If you are not clear with the basic concepts of Android, please attend this free Android webinar.

[dl url=”https://www.edureka.co/android-development-certification-course/?” title=”Take FREE Webinar” desc=”” type=”” align=”” for=”webinar”]

How to start?

Step 1:

  • Create a new android project and place a button in the xml layout.

activity_notifications.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnGenerateNotifications"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Generate a Notification" />

</RelativeLayout>

Generate a Notification

Step 2:

  • Now in your MainActivity, place the code shown below:

MainActivity.java


package com.hisham.notifications;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Notifications extends Activity {

	Button btnNotification;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notifications);

		btnNotification = (Button) findViewById(R.id.btnGenerateNotifications);
		btnNotification.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				// Prepare intent which is triggered if the
				// notification is selected
				Intent intent = new Intent(Notifications.this,
						NotificationClickedActivity.class);
				PendingIntent pendIntent = PendingIntent.getActivity(
						Notifications.this, 0, intent, 0);

				// Build notification
				// Actions are just fake
				Notification notiMail = new Notification.Builder(
						Notifications.this)
						.setContentTitle("support@edureka.in")
						.setContentText("Subject - Android Class")
						.setSmallIcon(R.drawable.ic_launcher)
						.setContentIntent(pendIntent).setAutoCancel(true).build();

				NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(0, notiMail);

			}
		});
	}
}

 

Step 3:

  • Now create another Activity to show Notification is clicked. This new activity will run even if the Application is no more in the memory.

And that’s it for the code!

Some important points about Android notification are:

  • You can dismiss all notification if you set your notification to auto-cancel.
  • Notification in Android also gets removed once the user selects it.
  • Calling the cancel() for a specific notification ID on the NotificationManager cancels that notification.
  • The cancelAll() method call removes all of the notifications previously issued.

Working:

When you click on the Generate a Notification Button, a notification will be generated that you can see on top of your Mobile screen. See this image:

Android Notification

Drag down the Notification bar and you can see the generated notification there!

Notification in Android

After clicking on the notification, you will be redirected to new Activity. Below is the image of the new Activity:

New Android Activity

Have a doubt in the discussed concepts? Ask our experts!

Stay tuned for more tutorials to learn how to create Android Widgets!

Happy Learning!

The following resources were used for creating this post: Edureka.in.

You may also like these related posts:

About Author
shivani kapahi
Published on Feb 13,2014

Share on

Browse Categories

Comments
1 Comment