• Have Any Question?
  • contact@eitworld.com

write an application for push notification and title and message should be entered by the user.





In this application, we have provided the facility to enter the title of the notification and user can set the message according to their need. Here is the xml layout file as follows:

<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/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Android Notification Example” />

<Button
android:id=”@+id/btnSend”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true”
android:layout_marginBottom=”130dp”
android:text=”Generate Notification” />

<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView1″
android:layout_below=”@+id/textView1″
android:layout_marginTop=”54dp”
android:text=”Title”
android:textAppearance=”?android:attr/textAppearanceLarge” />

<EditText
android:id=”@+id/etTitle”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentRight=”true”
android:layout_alignTop=”@+id/textView2″
android:ems=”10″ />

<TextView
android:id=”@+id/textView3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView2″
android:layout_below=”@+id/etTitle”
android:layout_marginTop=”38dp”
android:text=”Message”
android:textAppearance=”?android:attr/textAppearanceMedium” />

<EditText
android:id=”@+id/etMessage”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentRight=”true”
android:layout_alignTop=”@+id/textView3″
android:ems=”10″
android:inputType=”textPostalAddress” />

</RelativeLayout>

In this xml file, we have used the relative layout; here we have used two edit fields to take the input from the user. Here 1st edit text is simple edit text and 2nd is used for the message of type postal address so that user can set the long message. Below is the java file:

package com.example.androidnotification;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
Button generateNotification;
EditText title,message;
String t,m;

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

generateNotification = (Button) findViewById(R.id.btnSend);
title = (EditText) findViewById(R.id.etTitle);
message = (EditText) findViewById(R.id.etMessage);

generateNotification.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t = title.getText().toString();
m = message.getText().toString();
getNotification(t,m);
}
});
}

public void getNotification(String title, String message)
{
NotificationCompat.Builder ncb = new NotificationCompat.Builder(this);
ncb.setSmallIcon(R.drawable.ic_launcher);
ncb.setContentTitle(title);
ncb.setContentText(message);
Notification n = ncb.build();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0,n);
}

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

}

In this java file we have used the following packages:

android

After that we have to declare the reference variable of Button class so that we can use the button and edit text defined in the xml file as follows:
android
Here we have declared a button type variable called as geneeratNotification and the title, message of type EditText; here we have declared two string type variables to hold the data of edit text. After that we have to register the controls using their id which is defined in the xml file as follows:
android
After registering this, now we have to define the action when we click on the button. In java file, when we define the action for button then we have to use setOnClickListener() method. And inside this method, we have used two variables of string type to hold the data of the edit text in the form of string because we used the toString() method and we have called another method called getNotification() that is user defined that takes two parameters as follows:
android
Inside this we have created the object of the NotificationCompat.Builder object and pass the current context means pass this for the current activity.
After that we have set the icon for the notification using the ncb.setSmallIcon() method which takes one argument for icon. After that we have set the title for the notification using the ncb.setContentTitle() method which takes one string parameter title of type string, and after that we have set the message using the ncb.setContentText() method which takes one parameter of type string for the message.
After that we have created the object of the Notification class and pass the reference of the build() method means it is used to set the notification. At this point, we have created the notification but now we have to show the notification. For this we have to create the variable of type NotificationManager and pass the reference of the getSystemService() method which takes one parameter of inbuilt string type.
After that we have to call the notify() method which takes two arguments : first argument for id of the notification of integer type and other argument is the object of the Notification class.
The output is as follows:
android
When launched
android
When clicked on button
android
when slide the notification bar

android



Real Time Web Analytics