Android Notifications via compatibility support library




Special imports:
import android.app.Notification;
//You may find v4 support library here
import android.support.v4.app.NotificationCompat;
import android.app.NotificationManager;
import android.app.PendingIntent;


Initialise notification manager:
NotificationManager mNotificationManager=(NotificationManager)
                              getSystemService(Context.NOTIFICATION_SERVICE);

Prepare target intent: This intent will be started when user clicks on the notification which is being sent.
Intent intentTargetActivity=new Intent();
Add the parameters according to the requirement. Mean to say you may pass the context & class object just like starting normal activity. Ensure if you are passing class object, it is your application property :P


Embed target intent into pending intent:
Create a pending intent using target intent. Now, why this PendingIntent??? Answer is if you wish to have a control on how your target intent being launched[like security constraints] then you can do so using pending intents. If you have not specified any then default target specifications will be used to launch the target intent.
PendingIntent  pendingIntentTarget=PendingIntent.getActivity(context,
                  REQUEST_CODE,intentTargetActivity,/*PendingIntent.FLAG_UPDATE_CURRENT*/
                  android.content.Intent.FLAG_ACTIVITY_NEW_TASK);

Prepare the notification:
Note: Pre HoneComb[3.0], Building Notification directly is not directly supported. So you need use compatibility class given in v4 support library. i.e
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
      notificationBuilder.setSmallIcon(R.drawable.ic_launcher);//Mandatory
      notificationBuilder.setContentIntent(pendingIntentTarget);//Mandatory
      notificationBuilder.setWhen(System.currentTimeMillis());//Mandatory
      notificationBuilder.setTicker("TickerText");//Mandatory
      notificationBuilder.setContentTitle("ContentTitle");//Mandatory
      notificationBuilder.setContentText("ContentText");//Mandatory
     
Finally build the notification instance and set specific flags if you wish any ..
Notification notificationInfo=notificationBuilder.build();
      notificationInfo.flags|=Notification.FLAG_AUTO_CANCEL;//Notification gets cancelled once user clicks on it.
     
Value addition:
You may wish to customize notification behavior? A few are shown below
//Don’t forget to add in manifest:
// <uses-permission android:name="android.permission.VIBRATE"/>
//notificationInfo.vibrate=VIBRATE_PATTERN;
//where VIBRATE_PATTERN is something {100,200,100,200};

//similarly you may set audio effect to your notification
//by setting uri of sound file like:
//notificationInfo.sound=soundUri;
     
//Main thing: custom view!!
//notificationInfo.contentView=remoteView;
     
//Else you may wish to use the default notification behavior by:
//notificationInfo.defaults=Notification.DEFAULT_ALL;
  


Finally notify! :
How to send..? Like this:
//Using notification count itself as ID..
//Note using this notification ID we can controle post notification behaviour
// i.e we may cancel the notification if it exists or check for
//presence of the notification with same id.. etc..
mNotificationManager.notify(NOTIFICATION_ID, notificationInfo);
      

Need a working soldier? Go to My SampleNotification project on GitHub

Happy coding

.