Recipe Showing Notification in Status

The status bar across the top of the device screen shows pending notifications for the user to read at a convenient time. In general, because an activity mostly interacts with the user, services are more likely to utilize this feature. As a rule, notifications should be concise and minimal for the best user experience.

The steps for creating a status bar notification are

1. Declare a notification and specify how it displays on the status bar: String ns = Context.NOTIFICATION_SERVICE; mNManager = (NotificationManager) getSystemService(ns); final Notification msg = new Notification(R.drawable.icon, "New event of importance", System.currentTimeMillis());

2. Define how it looks when the status bar is expanded for details and the action taken when clicked (this future action is defined by a PendingIntent class): Context context = getApplicationContext();

CharSequence contentTitle = "ShowNotification Example"; CharSequence contentText = "Browse Android Cookbook Site"; Intent msgIntent = new Intent(Intent.ACTION_VIEW,

Uri.parse("http://www.pearson.com"));

PendingIntent intent =

PendingIntent.getActivity(ShowNotification.this, 0, msgIntent,

Intent.FLAG_ACTIVITY_NEW_TASK);

3. Add any further configurable information, such as whether to blink an LED, play a sound, or automatically cancel the notification after it is selected. The latter two are shown here:

msg.defaults |= Notification.DEFAULT_SOUND; msg.flags |= Notification.FLAG_AUTO_CANCEL;

4. Set the info for the notification event to the system: msg.setLatestEventInfo(context, contentTitle, contentText, intent);

5. On the event of interest, trigger notification with a unique identifier: mNManager.notify(NOTIFY_ID, msg);

6. Upon completion, clear notification as needed with the same identifier.

If any information gets changed, the notification should be updated rather than sending another notification. This can be done by updating the relevant information in step 2, and then again calling setLatestEventInfo.An example activity to show a notification is shown in Listing 3.21.

Listing 3.21 src/com/cookbook/show_notification/ShowNotification.java package com.cookbook.show_notification;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Context;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class ShowNotification extends Activity {

private NotificationManager mNManager; private static final int NOTIFY_ID=1100;

/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

String ns = Context.NOTIFICATION_SERVICE; mNManager = (NotificationManager) getSystemService(ns); final Notification msg = new Notification(R.drawable.icon, "New event of importance", System.currentTimeMillis());

Button start = (Button)findViewById(R.id.start); Button cancel = (Button)findViewById(R.id.cancel);

start.setOnClickListener(new OnClickListener() { public void onClick(View v) {

Context context = getApplicationContext(); CharSequence contentTitle = "ShowNotification Example"; CharSequence contentText = "Browse Android Cookbook Site"; Intent msgIntent = new Intent(Intent.ACTION_VIEW,

Uri.parse("http://www.pearson.com")); PendingIntent intent =

PendingIntent.getActivity(ShowNotification.this, 0, msgIntent,

Intent.FLAG_ACTIVITY_NEW_TASK);

msg.defaults |= Notification.DEFAULT_SOUND; msg.flags |= Notification.FLAG_AUTO_CANCEL;

msg.setLatestEventInfo(context, contentTitle, contentText, intent); mNManager.notify(NOTIFY_ID, msg);

cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) {

mNManager.cancel(NOTIFY_ID);

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment