activity_main:
<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/notificationOne"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn1" />
<Button
android:id="@+id/notificationTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="60dp"
android:text="@string/btn2" />
</RelativeLayout>
Notification_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
MainActivity.java:
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private NotificationManager myNotificationManager;
private int notificationIdOne = 111;
private int numMessagesOne = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button notOneBtn = (Button) findViewById(R.id.notificationOne);
notOneBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
displayNotificationOne();
}
});
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
protected void displayNotificationOne() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("New Message ");
mBuilder.setContentText("New message received");
mBuilder.setTicker("Notification: New Message Received!");
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setNumber(++numMessagesOne);
Intent resultIntent = new Intent(this, NotificationOne.class);
resultIntent.putExtra("notificationId", notificationIdOne);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NotificationOne.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_ONE_SHOT);
mBuilder.setContentIntent(resultPendingIntent);
myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotificationManager.notify(notificationIdOne, mBuilder.build());
}
}
Notification.java
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
public class NotificationOne extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_one);
CharSequence s = "Inside the activity of Notification one ";
int id=0;
Bundle extras = getIntent().getExtras();
if (extras == null) {
s = "error";
}
else {
id = extras.getInt("notificationId");
}
TextView t = (TextView) findViewById(R.id.text1);
s = s+"with id = "+id;
t.setText(s);
NotificationManager myNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotificationManager.cancel(id);
}
}