[Within the hour, the code should be formatted better]

 

Today, I have some code to present to the world. I typically make my code understandable by having descriptive variables. And I always put comments so that my future self (and others) would know why I wrote things. In this blog post I will describe how to create a functional notification that appears in the users’ status bar.

For basic notifications, you don’t need anything extra in AndroidManifest.xml. The function setNotification() was created in order to make setting up notifications easier and look cleaner in your code. To get this to work, just call the function name with a context as the parameter. Ex:

setNotification(MainActivity.this);

Then, place the following elsewhere on that same page:

private void setNotification(Context context) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My Notification Title")
.setContentText("Click here to do something!");
Intent resultIntent = new Intent(context, MainActivity.class); // Creates an explicit intent for an Activity in your app

// The stack builder object will contain an artificial back stack for the started Activity.
// This ensures that navigating backward from the Activity leads out of your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class); // TODO: Change this class to parent class // Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addNextIntent(resultIntent); // Adds the Intent that starts the Activity to the top of the stack
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setSound(getAlarmUri()); // Creates sound
mBuilder.setAutoCancel(true); // Allows notification to be cancelled when user clicks it
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 12345; // arbitrary number
mNotificationManager.notify(mId, mBuilder.build()); // mId allows you to update the notification later on.
}

Here is how you getAlarmUri():
//Get an alarm sound. Try for an alarm. If none set, try notification, otherwise, ringtone // Can be in any order you wish
private Uri getAlarmUri() {
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // Originally notification and alarm were switched, but my alarm gets annoying very fast. =b
if (alert == null) {
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alert == null) {
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
return alert;
}

Look out for my next Android post, How to Add an Alarm. In that post, I include full source code integrating this code and the alarm code so that a notification appears every time the alarm goes off.

More Resources: 

[0] For how to build other types of notifications, or even progress notification. Check out the awesome android.com resource: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

[1] For using NotificationCompat.Builder that I reference on line two: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html

Disclaimer: Only tested on my HTC Amaze, Android 4.0. Let me know by email if you have success with your device. Or, if you have any problems with this code, please feel free to email me and I will try my best to help.