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.setSmallIcon(R.mipmap.ic_launcher);
    bld.setContentTitle("New task");
    bld.setContentText("Feed the fish");
    bld.setSound(notificationSoundUri);
    // bld.setPriority(NotificationCompat.PRIORITY_MAX);
    // bld.setLights(Color.BLUE, 500, 500);

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

Result

android_notificationcompat.png
comments powered by Disqus