http://www.vogella.de/articles/AndroidNotifications/article.html 

Create a new project "de.vogella.android.notificationmanager" with the Activity "CreateNotification". Create the following layout "result.xml".

				
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent" android:layout_height="match_parent">
	<TextView android:text="This is the result activity opened from the notification"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:id="@+id/textView1"></TextView>
</LinearLayout>

			

Change "main.xml" to the following.

				
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<Button android:text="Create Notification" android:onClick="createNotification"
		android:id="@+id/button1" android:layout_height="match_parent"
		android:layout_width="match_parent"></Button>
</LinearLayout>

			

Create a new activity "NotificationReceiver" with the following coding. Don't forget to register the activity in the "AndroidManfest.mf".

				
package de.vogella.android.notificationmanager;

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

public class NotificationReceiver extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
	}
}

			

Change the activity "CreateNotification" to the following.

				
package de.vogella.android.notificationmanager;

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;

public class CreateNotification extends Activity {
	
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void createNotification(View view) { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.icon, "A new notification", System.currentTimeMillis()); // Hide the notification after its selected notification.flags |= Notification.FLAG_AUTO_CANCEL; Intent intent = new Intent(this, NotificationReceiver.class); PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0); notification.setLatestEventInfo(this, "This is the title", "This is the text", activity); notification.number += 1; notificationManager.notify(0, notification); } }

Run your application and press the button. A new notification is created. If you select it your second activity will be displayed.

Showing the Notification bar with the created notification

+ Recent posts