Displaying Notifications
×
Home Courses Community Support
Gray Matter

First Step Towards Android: Introduction »

Understanding Activities and Intents »

Hands On With UI Designing In Android (Controls-Views,Widgets and Layouts) »

Updating... »

Displaying Notifications

We can also use Toast class to display messages to the user. While the Toast class is a handy way to show users alerts, it is not persistent. It flashes on the screen for a few seconds and then disappears. If it contains important information, users may easily miss it if they are not looking at the screen. For messages that are important, you should use a more persistent method. In this case, you should use the NotificationManager to display a persistent message at the top of the device, commonly known as the status bar (sometimes also referred to as the notification bar).


To bundle the Notifications on the Status Bar follow these steps -

Step1: Create a new Android project and name it Notifications.
Step2: Create a new activity. To create a new activity make a right click on your package and then locate  New/Other../Android/Android Activity click on it and click Next, then choose Blank Activity and again click on Next. name this activity as Notification View and click on Finish. Your new activity has been created.
 


 

Step3: Now we will modify activity_notification_view.xml file as-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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=".NotificationView" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Here are the details for the notification..... You have to upload geeku file on server" />

</LinearLayout>


Step4: Now we will modify NotificationView.java file. So add the following code into NotificationView.java file-

package com.example.notifications;

import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.view.Menu;

public class NotificationView extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notification_view);
		
		//---look up the notification manager service---
		NotificationManager nm = (NotificationManager)
		getSystemService(NOTIFICATION_SERVICE);
		//---cancel the notification that we started
		nm.cancel(getIntent().getExtras().getInt("notificationID"));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.notification_view, menu);
		return true;
	}

}


Step5: Now we will modify Manifest file. So add the following code into Manifest.xml file-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notifications"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.VIBRATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.notifications.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.notifications.NotificationView"
            android:label="Details of notification" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                </intent-filter>
        </activity>
    </application>

</manifest>

 

Step6: Now we will modify activity_main.xml file. So add the following code into activity_main.xml file-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    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/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Display Notification" />

</LinearLayout>


Step7: Now we will modify MainActivity.java file. So add the following code into MainActivity.java file-

package com.example.notifications;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
	int notificationID=1; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button button= (Button)findViewById(R.id.button1);
        button.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View v) {
        	displayNotification();
        	}
        	});
    }
    
    protected void displayNotification() {
    	//---PendingIntent to launch activity if the user selects
    	// this notification---
    Intent i = new Intent(this, NotificationView.class);
    i.putExtra("notificationID", notificationID);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
		
    NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Notification notif = new Notification(R.drawable.ic_launcher,"Reminder: Upload files on server",System.currentTimeMillis());
    CharSequence from = "System Alarm";
    CharSequence message = "We have to upload geeku file...";
    notif.setLatestEventInfo(this, from, message, pendingIntent);
    //---100ms delay, vibrate for 250ms, pause for 100 ms and
    // then vibrate for 500ms---
    notif.vibrate = new long[] { 100, 250, 100, 500};
    nm.notify(notificationID, notif);
	}


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

 

Step8: Now Press F11 key to debug the application on the Android Emulator or on the device. Let's see the output window now.


 

Step9: Now click on Display Notification button. As soon as you will click on the button you will see the Reminder at the top.


 

Step10: Now slide down to see the notification.

 

Step11: Now click on Notification which says System Alert. As soon aas you will click on the button you will see message like-

 

 

← Preview

Dealing With Intent Object And Intent Filters

Next →

Controls - Some Basic Views (TextView, ImageView, Button etc.)

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App