I had a bit of a kerfuffle doing fancy Notifications for Android that needed to look and work the same on Honeycomb (3.x) , ICS (4.0.X), and JellyBean (4.1 – 4.2) (API11-16) . This was because the Google Android developers page….is wrong-ish..The Function Example Below shows you how to access many of the Fancier (IO 2012) Notification Abilities and appearance and add them to your application for consistent notifications to the user of status in an easy way. (Like a VPN Tunnel,  Private Wifi, Security Apps).

The way that this function uses the context of wherever  it is called and is placed in the code, allows you to use a single function that can update with multiple abilities for the user. For instance in my application, I have two critical components that must be functioning in order to maintain a connected state and a want to display real-time information from a system service. To be extremely fast and accurate, it is much better to simply call this function every time that the status of one of those items changed (on signalChanged, onLocationChanged, etc). It is also much more battery efficient, since you are waiting and only updating when required. This is important when you have an ongoing notification and/or application.

 //Code!!!
import java.io.*; //just always a good idea.....
 import android.graphics.*; //for our image conversion
 import android.app.Activity; // normal
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
private void alertNotification(){ //Normally we would be starting the function with Context context
 //We use this. instead of context because we will be calling it from multiple places and within other running services
       NotificationManager notificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
//Declare your New builder for your notification (This is slightly different from the Google example, but cleaner since we call it multiple ways )
       Notification.Builder builder = new Notification.Builder(this);
//Make sure that your syntax is correct for all of the info you will display
        String dbs = Integer.toString(decibels); String titles = null; String texts = null;
//Conditional Statements to change the Icons and Information
      if (connection_state == "Connected" ){
               titles = "Connected to System";
               texts = "Signal: " + dbs + "db " ;
               icon = R.drawable.connected_icon; // Small icon = 48pix or 36pix for xhdpi/hdpi
               l_icon = R.drawable.icon_connected; //Same size as launcher icons
       }
       if(connection_state != "Connected" ){
               l_icon = R.drawable.icon_start;
               titles = "Not Connected to System";
               texts = "Searching for Connection";
               icon = R.drawable.start_icon;
        }
// We have to convert a Drawable Resource to Bitmap using
        Bitmap large_icon = BitmapFactory.decodeResource(this.getResources(),l_icon);
//One Builder for both options of Connection
        builder
        .setContentTitle(titles) //Give it a title (String)
        .setContentText(texts) //Give a bit more detail (String)
        .setSmallIcon(icon) //Icon that appears in the Staus Bar and right bottom corner of Notification (R.drawable)
        .setNumber(decibels)// Only found this to work on CERTAIN API16 Devices (must be int) to dispay a number next to your icon
        .setTicker(titles) // The pop-up Text in Status Bar when the Notification is Called
        //.setPriority(1) Does Not allow for API11 - API15
        .setOngoing(true) // Cannot Have the icon Swept away, also forces it to the top in most cases
        .setLargeIcon(large_icon) //Icon to Appear on the Big Left Area
         //******************* //Pay attention here as it is NOT COVERED IN THE API DOCS BY GOOGLE!
        .getNotification();
 //You know where is says use .builder(), don't as its ONLY valid for API16+ or 4.1+ //******************* 
//Define what you just built as the Notification that you plan to use
         Notification notification = builder.getNotification();
//Run and Set your Notification!
         notificationManager.notify(R.drawable.ic_launcher, notification);
 }
Categories: Mobile Apps

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *