Hello Baby widget feature update - notification tutorial

SDK Version: 
Hello readers, my name is Tamas, and I'm the newest member of the HelloAndroid.com team :)
Today we decided to update our Hello Baby widget, with a notification feature. So I'm using this opportunity, to show you how to use notifications with widgets.
The plan was to alert the user with a status bar notification 1,2,3 and 7 days before the days-left counter reaches 0.

Our widget updates every 30 minutes, so the user would be really annoyed, if he/she got a notification every single time the widget updates. I want it to notify the user once per day max, so I decided to store the fact, that a notification is/was made(daysLeftStored). When the user presses save at the settings window, the daysLeftStored will be reseted, so if the user sets a date which is 1,2,3 or 7 days from day 0, it will give a notification immediately.

  1. //daysLeft: stored after user gave input date
  2. //daysLeftStored: stored after notification is made

  1. //daysLeftStored!=7 this is checked so it won't notify the user multiple times on the same day
  2. if(daysLeft==7&&daysLeftStored!=7){
  3.     helloBabyNotification("Your baby's birth is due in 1 week!", "Hello Baby!"); //input is: alert text and alert text title
  4.     saveNotificationUpdate(7); //so we know what day the notification was made
  5. }

  1. if(daysLeft==3&&daysLeftStored!=3){
  2.     helloBabyNotification("Your baby's birth is due in 3 days!", "Hello Baby!");
  3.     saveNotificationUpdate(7); //so we know what day the notification was made
  4. }


  1. //saving the date to preferences, on an onClick event of the save button (in the widgets settings screen)
  2.  public void saveNotificationUpdate(int day){
  3.     SharedPreferences.Editor editor = Msettings.edit();    //Msettings your settings
  4.     editor.putInt("daysLeft",day);
  5.     editor.commit();
  6.  }

  1. //loading from preferences. Gives -1 if there was no value set previously.
  2.  public int loadNotificationUpdate(){
  3.     return Msettings.getInt("daysLeft", -1);
  4.  }


So we know now, when to do the notification, let's notify the user!

  1. //this part is in the onUpdate part of the widget
  2. public void helloBabyNotification(Context context, int icon, String text, String title){
  3.     String ns = context.NOTIFICATION_SERVICE;
  4.     NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns); //I get the context, from onUpdate()
  5.     CharSequence tickerText = "Hello Baby"; //this text appears in the notification bar with the icon, when the notification launches
  6.     long when = System.currentTimeMillis(); //you can display a notification now, or set a different time in the future
  7.     Notification notification = new Notification(icon, tickerText, when);
  8.     CharSequence contentTitle = title;
  9.     CharSequence contentText = text;
  10.     Intent notificationIntent = new Intent();
  11.     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
  12.     notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
  13.     int HELLO_ID = 1;
  14.     mNotificationManager.notify(HELLO_ID, notification);
  15. }

So if the user sets a date that is 1,2,3,7 days from day 0, and presses the save settings button on the settings screen, the notification will be shown immediately.

Hello Baby on the Home Screen:

Hello Baby Screenshot
And the notification will look like this:

Hello Baby Android Screenshot Notification

The Hello Baby widget update will be available in the upcoming days, on the Android market!