android push notifications tutorial

Android push notifications are very simple to implement. they are useful for showing events, alerts etc…

Working code for android push notifications.

 public void ShowNotification()  
  int mNotificationID = 10; //Make sure this number is unique, we use this to update or cancel notification.  
  CharSequence mNotificationTitle = "This the Title";  
  CharSequence mNotificationDescription = "This is the description";  
  //Sound & icon related to notification  
  Uri mNotificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);  
  Drawable d = getResources().getDrawable(com.example.androidnotificationexample.R.drawable.ic_launcher);  
    Bitmap bitmap = ((BitmapDrawable)d).getBitmap();  
    //Build the object and set the title, text, sound etc.. properties  
     NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(  
        .setSmallIcon(com.example.androidnotificationexample.R.drawable.ic_launcher) //to small icon on the right hand side   
      .setWhen(System.currentTimeMillis()); // Displays the date on right side bottom  
     Notification mNotificationObject =;  
     //This is to keep the default settings of notification, 
     mNotificationObject.defaults |= Notification.DEFAULT_VIBRATE;  
     mNotificationObject.flags |= Notification.FLAG_AUTO_CANCEL;   
    //This is to show the ticker text which appear at top.  
     mNotificationObject.tickerText = mNotificationTitle + "n" + mNotificationDescription;   
     //Trigger the notification
     NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
    manager.notify(mNotificationID, mNotificationObject);  

Some of the important tips about push notifications: 

mNotificationSoundUri, Pointing to the default sound, however it can be changed easily.
Use below example code to get alarm tone.

 Uri mNotificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);  

Notification.FLAG_AUTO_CANCEL: Cancel notification with simple swipe or with clear option.

To display sticky notification always,Use flag like below example, user can’t clear or cancel. these type can be cancelled through programmatically.

 mNotificationObject.flags = Notification.FLAG_ONGOING_EVENT;  

Cancelling the notification.

 int notifID = 10;  
 NotificationManager notificationManager = (NotificationManager)ct.getSystemService(Context.NOTIFICATION_SERVICE);