Wednesday, November 21, 2012

Notifications In Android

What is Notification


Android allows to put notification into the titlebar of your application. The user can expand the notification bar and by selecting the notification the user can trigger another activity.

A notification is a message you can display to the user outside of your application's normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time.


Elements of Notification




  1. Content title
  2. Large icon
  3. Content text
  4. Content info
  5. Small icon
  6. Time that the notification was issued.

Creating a Notification


We need to specify the UI information and actions for a notification in a NotificationCompat.Builder object. To create the notification itself, you call NotificationCompat.Builder.build(), which returns a Notification object containing your specifications. To issue the notification, you pass the Notification object to the system by calling NotificationManager.notify().

A Notification object must contain the following:

  • A small icon, set by setSmallIcon()
  • A title, set by setContentTitle()
  • Detail text, set by setContentText()
The following code snippet illustrates a simple notification that specifies an activity to open when the user clicks the notification. Notice that the code creates a TaskStackBuilder object and uses it to create the PendingIntent for the action

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);

// 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(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());


Removing or Cancelling Notifications


Notifications remain visible until one of the following happens:

The user dismisses the notification either individually or by using "Clear All" (if the notification can be cleared).
The user clicks the notification, and you called setAutoCancel() when you created the notification.
You call cancel() for a specific notification ID. This method also deletes ongoing notifications.
You call cancelAll(), which removes all of the notifications you previously issued.

5 comments:

  1. Nice post with great details. I really like your job. Thanks for sharing. mass notification software

    ReplyDelete
  2. Hi,

    I need your help for Notification....My app is dealing with sensex related data. I want to setAlarms for specific sensex value whether my app is running or closed. e.g if I set Alarm in my app for gold rates of 25000, whenever sensex value reach to 25000 i should get alarm or notification whether my app is running or not....It's very urget plz!!!!

    ReplyDelete
  3. Hi,

    I want your help for sending of Notifications. In my app i have to set notifications from 5 activities, how to set notifications from all activities at user selected time and repeated days like(mon,tue,fri,... and every hour, every two hour, every six hour, etc,..)

    ReplyDelete
  4. thanks you for sharing this tutorial. I am a newbie and i find tutorials on your site very helpful. I am a big fan of learning by doing. Another important trick is to download an apk from apkmonk or apkmirror then decompile it using java compiler. Using this technique one can get code written in production environments.

    ReplyDelete
  5. Thanks so much for this information. sb game hacker apk I have to let you know I concur on several of the points you make here and others may require some further review, game killer apk but I can see your viewpoint. geometry dash full apk

    ReplyDelete