Category Archives: Notification

send a Notification and cancel

NotificationManager to display a persistent message at the top of the device, commonly known as the status bar (sometimes also referred to as the notification bar). The following Try It Out demonstrates how.

main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >

<Button
android:id=”@+id/btn_displaynotif”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Display Notification” />

</LinearLayout>

==================================================

notification.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>

<TextView
android:id=”@+id/textView1″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Here are the details of notification” />

</LinearLayout>

—————————————————————————————-

NotificationExample.java

public class NotificationExample extends Activity {
int notificationID = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.btn_displaynotif);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
displayNotification();
}
});

}

public void displayNotification(){
Intent i = new Intent(this, MainActivity.class);
i.putExtra(“notificationID”, notificationID);

PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,0);

NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);

Notification notif = new Notification(
R.drawable.icon,
“Reminder: Meeting starts in 5 minutes”,
System.currentTimeMillis());

CharSequence from = “System Alarm”;
CharSequence message = “Meeting with customer at 3pm…”;

notif.setLatestEventInfo(this, from, message, pendingIntent);

notif.sound=Uri.parse(“android.resource://com.example.android/”+R.raw.beep);

notif.vibrate = new long[] { 100, 250, 100, 500};
nm.notify(notificationID, notif);

}

———————————————————————————————

NotificationView.java

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
public class NotificationView extends Activity
{

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);

NotificationManager nm =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

nm.cancel(getIntent().getExtras().getInt(“notificationID”));

}

—————————————————————————————–

Manifest.xml

​<application​android:icon=”@drawable/icon”​android:label=”@string/app_name”>
​​​​​​​​<activity​android:name=”.NotificationExample”
​​​​​​​​​​​​​​​​​​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=”.NotificationView”
​​​​​​​​​​​​android:label=”Details of notification”>
​​​​​​​​​​​​​​​​​​​​</activity>
​​​​</application>

—————————————————————————————–

In this example, you added a key/value pair to the Intent object so that you can tag the notification ID, identifying the notification to the target activity. This ID will be used to dismiss the notifications later.

You would also need to create a PendingIntent object. A PendingIntent object helps you to perform an action on your application’s behalf, often at a later time, regardless of whether your application is running or not. In this case, you initialized it as follows:
​​​​​​​​PendingIntent​pendingIntent​= ​​​​​​​​​​​​PendingIntent.getActivity(this,​0,​i,​0);

The getActivity() method retrieves a PendingIntent object and you set it using the following arguments:
context — Application context
request code — Request code for the intent
intent — The intent for launching the target activity
flags — The flags in which the activity is to be launched

You then obtain an instance of the NotificationManager class and create an instance of the Notification class:

​​​​​​​​NotificationManager nm = (NotificationManager)
​​​​​​​​​​​​getSystemService(NOTIFICATION_SERVICE);

​​​​​​​​Notification notif = new Notification(
​​​​​​​​​​​​R.drawable.icon,
​​​​​​​​​​​​“Reminder: Meeting starts in 5 minutes”,
​​​​​​​​​​​​System.currentTimeMillis());

The Notification class enables you to specify the notification’s main information when the notification first appears on the status bar. The second argument to the Notification constructor sets the “ticker text” on the status bar

Next, you set the details of the notification using the setLatestEventInfo() method:
​​​​​​​​CharSequence from = “System Alarm”;
​​​​​​​​CharSequence message = “Meeting with customer at 3pm…”;
​​​​​​​​notif.setLatestEventInfo(this, from, message, pendingIntent);

​​​​​​​​//—100ms delay, vibrate for 250ms, pause for 100 ms and
​​​​​​​​// then vibrate for 500ms—
​​​​​​​​notif.vibrate = new long[] { 100, 250, 100, 500};

The preceding also sets the notification to vibrate the phone. Finally, to display the notification you use the notify() method:

​​​​​​​​nm.notify(notificationID, notif);

When the user clicks on the notification, the NotificationView activity is launched. Here, you dismiss the notification by using the cancel() method of the NotificationManager object and passing it the ID of the notification (passed in via the Intent object):

​​​​​​​​//—look up the notification manager service—
​​​​​​​​NotificationManager nm = (NotificationManager)
​​​​​​​​​​​​getSystemService(NOTIFICATION_SERVICE);

​​​​​​​​//—cancel the notification that we started
​​​​​​​​nm.cancel(getIntent().getExtras().getInt(“notificationID”));