Notification Manager

satya - Wednesday, May 26, 2010 10:13:56 PM

base read: api

base read: api

satya - Wednesday, May 26, 2010 10:17:31 PM

Creating status bar notifications: article

Creating status bar notifications: article

satya - Wednesday, June 02, 2010 5:33:05 PM

Minimum needed to work with notification


// 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);

satya - Wednesday, June 02, 2010 5:43:40 PM

Android how to see notifications

Android how to see notifications

Search for: Android how to see notifications

satya - Wednesday, June 02, 2010 6:03:41 PM

Two ways

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

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

satya - Wednesday, June 02, 2010 6:05:59 PM

The following code will fail


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

satya - Wednesday, June 02, 2010 6:07:31 PM

Here is the code that will work


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);
}

satya - Wednesday, June 02, 2010 6:08:41 PM

Because....

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.

satya - Wednesday, June 02, 2010 6:10:32 PM

Here is the look of a notification

satya - Wednesday, June 02, 2010 6:12:36 PM

Here is what it looks like when

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.

satya - Wednesday, June 02, 2010 6:18:12 PM

Here is a recommendation from google for using a remoteview


//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;

satya - Wednesday, June 02, 2010 6:22:35 PM

Reference to remoteviews class

Reference to remoteviews class

satya - Wednesday, June 02, 2010 6:23:36 PM

Reference to pending intent

Reference to pending intent

satya - Wednesday, June 02, 2010 6:26:22 PM

Read up on widgets as they deal with remoteviews as well

Read up on widgets as they deal with remoteviews as well

satya - Wednesday, June 02, 2010 6:27:50 PM

Allowed list of remoteview object


FrameLayout 
LinearLayout 
RelativeLayout 

AnalogClock 
Button 
Chronometer 
ImageButton 
ImageView 
ProgressBar 
TextView

satya - Saturday, November 12, 2011 9:12:54 AM

Notification bar seem to be enhanced in ICS or 4.0

Notification bar seem to be enhanced in ICS or 4.0

satya - Saturday, November 12, 2011 9:13:37 AM

The primary note still holds

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.

satya - Saturday, November 12, 2011 9:21:53 AM

A notification has its own unique id with in your application context


NotificationManager nm;

int uniqueNotificationIdWithinMyApp = 1;
Notification notification;

nm.notify(uniqueNotificationIdWithinMyApp, notification);

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

satya - Saturday, November 12, 2011 9:31:17 AM

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

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

satya - Saturday, November 12, 2011 9:51:53 AM

The battle of bars


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

satya - Saturday, November 12, 2011 10:05:06 AM

Because you can place any remote view as a notification

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

satya - Saturday, November 12, 2011 10:08:47 AM

There are lot of flags that can control the notification behavior

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.

satya - Saturday, November 12, 2011 10:09:37 AM

on_going is another one of these flags

on_going is another one of these flags

satya - 5/4/2013 11:38:07 AM

Notification.Builder API

Notification.Builder API

satya - 5/4/2013 11:38:29 AM

How do you set flags on an Android Notification object

How do you set flags on an Android Notification object

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

satya - 5/4/2013 11:41:11 AM

Here is some notes from SOF

Here is some notes from SOF

satya - 5/4/2013 11:42:16 AM

So you can do this for auto cancel


notification.flags |= Notification.FLAG_AUTO_CANCEL;

satya - 7/13/2014 9:43:33 AM

Things have changed since then, esp API 16. Use the following builder based code instead


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);
    }

satya - 7/13/2014 9:43:58 AM

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

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