time

How to notify user in Android by NotificationCompat

Notifications provide an excellent means to send information to your user in the titlebar, and the user can click on this notfication to move another activity.

To create a notification we will use NotificationCompat.Builder class.

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

Once you have the Builder object, you can set Notification properties using the Builder object according to your requirements.

  • setSmallIcon(R.drawable.icon). Set the small icon to use in the notification layouts.
  • setContentTitle("Title"). Set the text (first row) of the notification, in a standard notification.
  • setContentText("Text"). Set the text (second row) of the notification, in a standard notification.
  • addAction(R.drawable.ic_launcher, "View", IntentOBJ). Set the PendingIntent for action on click.
  • setAutoCancel(true). Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel.

To issue notification we use of NotificationManager object and call notify method on this object by passing Notification ID and Builder Object.

NotificationManager mng = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = 1;
mng.notify(notificationId, bld.build());

The action is defined by a PendingIntent containing an Intent that starts an Activity in your application. You can add a button (up to three) using the addAction() method on the notification builder. An Action requires three parameters - the image, the text, and a PendingIntent. The first two items are for the visual display, while the third item, the PendingIntent, is called when the user presses the button.

Content of activity_main.xml layout

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Dialog"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="showNotification"/>

Open MainActivity.java and add the following

public void showNotification(View v) {
    int notificationId = 1;
    Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder bld = new NotificationCompat.Builder(this);
    bld.setContentTitle("New task");
    bld.setContentText("Feed the fish");
    bld.setSound(notificationSoundUri);
    // bld.setPriority(NotificationCompat.PRIORITY_MAX);
    // bld.setLights(Color.BLUE, 500, 500);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        bld.setSmallIcon(R.drawable.silhouette);
        bld.setColor(getResources().getColor(R.color.colorAccent));
        bld.setLargeIcon(BitmapFactory.decodeResource(activity.getResources(), R.drawable.icon));
    } else {
        bld.setSmallIcon(R.drawable.icon);
    }

    Intent i = new Intent(this,MainActivity.class);
    // send something
    // i.putExtra("msg", "hello");

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
    bld.addAction(android.R.drawable.ic_dialog_email, "Email", pendingIntent);

    NotificationManager notificationManager =  (NotificationManager) 
        this.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(notificationId, bld.build());    
}

Post android Lollipop release android has changed the guidelines for displaying notification icons in the Notification bar. The official documentation says "Update or remove assets that involve color. The system ignores all non-alpha channels in action icons and in the main notification icon. You should assume that these icons will be alpha-only. The system draws notification icons in white and action icons in dark gray". Now what that means in lay man terms is "Convert all parts of the image that you don’t want to show to transparent pixels. All colors and non transparent pixels are displayed in white".

Result

android_notificationcompat.png
comments powered by Disqus