How to show notification in Android


 

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.

Notification Display Elements

Notifications in the notification drawer can appear in one of two visual styles, depending on the version and the state of the drawer:

Normal view

The standard view of the notifications in the notification drawer.

Big view

A large view that’s visible when the notification is expanded. Big view is part of the expanded notification feature available as of Android 4.1.

Normal view

A notification in normal view appears in an area that’s up to 64 dp tall. Even if you create a notification with a big view style, it will appear in normal view until it’s expanded. This is an example of a normal view:

How to show notification in Android

The callouts in the illustration refer to the following:

  • 1. Content title
  • 2. Large icon
  • 3. Content text
  • 4. Content info
  • 5. Small icon6. Time that the notification was issued. You can set an explicit value with setWhen(); if you
    don’t it defaults to the time that the system received the notification.
  • Big view

    A notification’s big view appears only when the notification is expanded, which happens
    when the notification is at the top of the notification drawer, or when the user expands the
    notification with a gesture. Expanded notifications are available starting with Android 4.1.
    The following screenshot shows an inbox-style notification:

    How to show notification in Android

    Creating a Notification

    You 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 aNotification object
    containing your specifications. To issue the notification, you pass the Notificationobject to
    the system by calling NotificationManager.notify().

    Creating a simple notification

    The following 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 thePendingIntent for the action. This pattern is explained in more detail in
    the section Preserving Navigation when Starting an Activity:

    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());
    
    

    That’s it. Your user has now been notified.

    Applying a big view style to a notification

    To have a notification appear in a big view when it’s expanded, first create
    a NotificationCompat.Builderobject with the normal view options you want. Next,
    call Builder.setStyle() with a big view style object as its argument.

    Remember that expanded notifications are not available on platforms prior to Android 4.1. To
    learn how to handle notifications for Android 4.1 and for earlier platforms, read the section Handling compatibility.

    For example, the following code snippet demonstrates how to alter the notification created in the previous snippet to use the Inbox big view style:

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("Event tracker")
    .setContentText("Events received")
    NotificationCompat.InboxStyle inboxStyle =
    new NotificationCompat.InboxStyle();
    String[] events = new String[6];
    // Sets a title for the Inbox style big view
    inboxStyle.setBigContentTitle("Event tracker details:");
    ...
    // Moves events into the big view
    for (int i=0; i < events.length; i++) {
    inboxStyle.addLine(events[i]);
    }
    // Moves the big view style object into the notification object.
    mBuilder.setStyle(inBoxStyle);
    ...
    // Issue the notification here.
    
Posted in All Tutorials, Android Tutorials | Tagged | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Powered By: wpnova.com