How to Create Notification in Android

In this tutorial how to use Create Notification for Android Application using NotificationManager in Android is shown.

Notification means an alert message for user to display the information regarding software update, email, messaging on Status Bar.

The Android platform provides different ways of notifying the user by using sound, message, icon, blink light etc.

Status Bar is the standard location to indicate notification.

A simple notification has number important components

  • An icon
  • Ticker text
  • Notification title text
  • Notification body text
  • An intent

To create the notification, use the object of NotificationManager class.

To create the object of NotificationManager call getSystemService() method.

NotificationCompat.Builder class provide the wide range to create notification

To define notification action we have to handle Intent and PendingIntent.

Call notify() method to issue the notification, it will specify the notification id and also call build() method which will return Notification object.

Code:

main_activity.xml

<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”
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” >

<TextView
android:id=”@+id/tv1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”106dp”
android:text=”Notification Program” />

</RelativeLayout>

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;

public class MainActivity extends Activity{

static int num=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationCompat.Builder ncb=new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setContentTitle(“Hello!! “).setContentText(“This is a notification”).setLights(Color.GREEN, 10, 10).setNumber(++num).setAutoCancel(true);

Intent in=new Intent(this,secActivity.class);
PendingIntent pi=PendingIntent.getActivity(this,0,in,PendingIntent.FLAG_UPDATE_CURRENT);

ncb.setContentIntent(pi);

NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(001,ncb.build());

Vibrator v=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
long[] pat={0,300,500,200};
v.vibrate(pat,-1);

}

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

sec_activity.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent” >

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”122dp”
android:text=”This page is loaded on activity click” />

</RelativeLayout>

secActivity.java

package com.example.demo;

import android.app.Activity;
import android.os.Bundle;

public class secActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sec_activity);
}

}

AndroidManifest.xml

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

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />

<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.demo.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=”.secActivity”></activity>
</application>

</manifest>

Add a Comment

Your email address will not be published. Required fields are marked *