Académique Documents
Professionnel Documents
Culture Documents
Services
Products
Books
Blog
Donate
Contact us
TrainingBooks
Table of Contents
1. Prerequisites for this tutorial
2. Notification Manager
2.1. Notification Manager
2.2. Setting up Notifications
2.3. Canceling Notifications
3. Pending Intent
4. Example: NotificationManager
2. Notification Manager
2.1. Notification Manager
Android allows to put notification into the titlebar of your application. The user can expand the
notification bar and by selecting the notification the user can trigger another activity.
// build notification
// the addAction re-use the same intent to keep the example short
Notification n
= new Notification.Builder(this)
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
3. Pending Intent
A pending intent is a token that you give to another application (e.g., notification manager,
alarm manager or other 3rd party applications), which allows this other application to use the
permissions of your application to execute a predefined piece of code.
To perform a broadcast via a pending intent, get a PendingIntent via
the getBroadcast() method of the PendingIntent class. To perform an activity via a
pending intent, you receive the activity via PendingIntent.getActivity().
4. Example: NotificationManager
Create a new activity called NotificationReceiverActivity with the following coding. Don't forget
to register the activity in theAndroidManfest.mf.
package de.vogella.android.notificationmanager;
import android.app.Activity;
import android.os.Bundle;
public class NotificationReceiverActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
}
}
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 CreateNotificationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void createNotification(View view) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle("New mail from " + "test@gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.addAction(R.drawable.icon, "Call", pIntent)
.addAction(R.drawable.icon, "More", pIntent)
.addAction(R.drawable.icon, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
}
Run your application and press the button. A new notification is created. If you select it your
second activity will be displayed.
If you find errors in this tutorial, please notify me (see the top of the page). Please note that
due to the high volume of feedback I receive, I cannot answer questions to your
implementation. Ensure you have read the vogella FAQ as I don't respond to questions
already answered there.