Notification Manager

base read: api

Creating status bar notifications: article


// icon from resources
int icon = R.drawable.notification_icon;        

// ticker-text
CharSequence tickerText = "Hello";              

// notification time
long when = System.currentTimeMillis();         

// application Context
Context context = getApplicationContext();      

// expanded message title
CharSequence contentTitle = "My notification";  

// expanded message text
CharSequence contentText = "Hello World!";      

Intent notificationIntent = 
   new Intent(this, MyClass.class);
PendingIntent contentIntent = 
    PendingIntent.getActivity(this, 0, notificationIntent, 0);

// the next two lines initialize the Notification, 
// using the configurations above
Notification notification = 
   new Notification(icon, tickerText, when);

notification.setLatestEventInfo(context, contentTitle, 
                        contentText, contentIntent);

Android how to see notifications

Search for: Android how to see notifications

1. Hold and drag notification icon down using mouse or touch.

2. Or use Home/Menu/Notifications. You can also clear all notifications here.


private void sendNotification(Context ctx, String message)
{
   String ns = Context.NOTIFICATION_SERVICE;
   NotificationManager nm = 
      (NotificationManager)ctx.getSystemService(ns);
   
   int icon = R.drawable.robot;
   CharSequence tickerText = "Hello";
   long when = System.currentTimeMillis();
   
   Notification notification = 
      new Notification(icon, tickerText, when);
   nm.notify(1, notification);
}

Because a "notification" object is not complete with out a contentView and a contentIntent. You can correct this using the following


private void sendNotification1(Context ctx, String message)
{
   String ns = Context.NOTIFICATION_SERVICE;
   NotificationManager nm = 
      (NotificationManager)ctx.getSystemService(ns);
   
   int icon = R.drawable.robot;
   CharSequence tickerText = "Hello";
   long when = System.currentTimeMillis();
   
   Notification notification = 
      new Notification(icon, tickerText, when);
   
   Intent intent = new Intent(Intent.ACTION_VIEW);
   intent.setData(Uri.parse("http://www.google.com"));
   PendingIntent pi = PendingIntent.getActivity(ctx, 0, intent, 0);

   notification.setLatestEventInfo(ctx, "title", "text", pi);
      
   nm.notify(1, notification);
}

The method "setLatestEventInfo" is a shortcut for setting the contentView and the contentIntent. I wish they took those two as constructor arguments otherwise the notification object is not complete without them.

when you tap on the icon you will only see the bottom portion of the (the one with the dots on it) pull down. You will have to hold the mouse down and drag it down in a simple motion.


//Instantiate a remoteviews object
//with an acceptable layout
RemoteViews contentView = new RemoteViews(getPackageName(), 
                    R.layout.custom_notification_layout);

//set the image using the id from the layout                
contentView.setImageViewResource(R.id.image, 
                 R.drawable.notification_image);

//set the text using the id from the layout
contentView.setTextViewText(R.id.text, 
   "Hello, this message is in a custom expanded view");

//set it in the contentview
notification.contentView = contentView;

Reference to remoteviews class

Reference to pending intent

Read up on widgets as they deal with remoteviews as well


FrameLayout 
LinearLayout 
RelativeLayout 

AnalogClock 
Button 
Chronometer 
ImageButton 
ImageView 
ProgressBar 
TextView

Notification bar seem to be enhanced in ICS or 4.0

A background service should never launch an activity on its own in order to receive user interaction. The service should instead create a status bar notification that will launch the activity when selected by the user.


NotificationManager nm;

int uniqueNotificationIdWithinMyApp = 1;
Notification notification;

nm.notify(uniqueNotificationIdWithinMyApp, notification);

Then you can use this unique id to cancel or update the notification

One can use NotificationBuilder to create notifications: here is the api


status bar: Old bar at the top of your phone
  Pre 2.3, battery, signal etc
  notifications

system bar: Tablet bar at the bottom
  3.0 for honeycomb tablets

Navigation bar: 4.0 for phones
  for phones, home, back etc
  tuned system bar
  minus status bar
  has the status bar at the top as well
  notifications are still in status bar

you can pretty much do everything you are able to do on a homescreen widget here as well.

There are lot of flags that can control the notification behavior

For example you can use the no_clear to keep your notification there for ever, I suppose. Perhaps this is how the play list stays here on some devices.

on_going is another one of these flags

Notification.Builder API

How do you set flags on an Android Notification object

Search for: How do you set flags on an Android Notification object

Here is some notes from SOF


notification.flags |= Notification.FLAG_AUTO_CANCEL;

private void sendNotification(Context ctx, String message)
    {
       //Get the notification manager
       String ns = Context.NOTIFICATION_SERVICE;
       NotificationManager nm = 
          (NotificationManager)ctx.getSystemService(ns);
       
       //Create Notification Object
      int icon = R.drawable.robot;
      CharSequence tickerText = "Hello";
      long when = System.currentTimeMillis();
      
      //Set ContentView using setLatestEvenInfo
       Intent intent = new Intent(Intent.ACTION_VIEW);
       intent.setData(Uri.parse("http://www.google.com"));
       PendingIntent pi = PendingIntent.getActivity(ctx, 0, intent, 0);
       
      Notification notification =
         new Notification.Builder(ctx)
            .setContentTitle("title")
            .setContentText(tickerText)
            .setSmallIcon(icon)
            .setWhen(when)
            .setContentIntent(pi)
            .setContentInfo("Addtional Information:Content Info")
            .build();
           
       //Send notification
      nm.notify(1, notification);
    }

Builder also has a setContent(RemoteViews) method to set the remote views directly