Android Notification Example Using NotificationCompat

nilan avtar

Written by:

Nilanchala,  5 min read,  updated on September 17, 2023

In this example, we will learn how to create Android notification using NotificationCompat class available in the Android Support library. This class supports big style notification with backward compatibility. Big style notification was introduced on Android version 4.1.x. If you are building app that supports older android phones like android 2.3 or above, in such cases you may like to go for NotificationCompat.Builder for showing notifications.

If you are using this library, then your newer version of android phone will show big styled notification, while older android phones will show old style notification.

Example using NotificationCompat

package com.javatechig.notification;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    // A integer, that identifies each notification uniquely
    public static final int NOTIFICATION_ID = 1;

    public void onCreate(Bundle savedInstanceState) {

        // Your application layout file

     * Send simple notification using the NotificationCompat API.
    public void sendNotification(View view) {

        // Use NotificationCompat.Builder to set up our notification.
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        //icon appears in device notification bar and right hand corner of notification

        // This intent is fired when notification is clicked
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(""));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        // Set the intent that will fire when the user taps the notification.

        // Large icon appears on the left of the notification
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));

        // Content title, which appears in large type at the top of the notification
        builder.setContentTitle("Notifications Title");

        // Content text, which appears in smaller text below the title
        builder.setContentText("Your notification content here.");

        // The subtext, which appears under the text on newer devices.
        // This will show-up in the devices with Android 4.2 and above only
        builder.setSubText("Tap to view documentation about notifications.");

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Will display the notification in the notification bar