How to create a notification with NotificationCompat.Builder?

I need to create a simple notification which will be shown in notification bar along with the sound and icon if possible? I also need it to be compatitible with Android 2.2, so i found that NotificationCompat.Builder works with all APIs above 4. If there's a better solution, feel free to mention it.

The NotificationCompat.Builder is the most easy way to create Notifications on all Android versions. You can even use features that are available with Android 4.1. If your app runs on devices with Android >=4.1 the new features will be used, if run on Android <4.1 the notification will be an simple old notification.

To create a simple Notification just do (see Android API Guide on Notifications):

NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(this)
    .setContentTitle("My notification")
    .setContentText("Hello World!")
    .setContentIntent(pendingIntent); //Required on Gingerbread and below

You have to set at least smallIcon, contentTitle and contentText. If you miss one the Notification will not show.

Beware: On Gingerbread and below you have to set the content intent, otherwise a IllegalArgumentException will be thrown.

To create an intent that does nothing, use:

final Intent emptyIntent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, NOT_USED, emptyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

You can add sound through the builder, i.e. a sound from the RingtoneManager:


The Notification is added to the bar through the NotificationManager:

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

How to create a notification with NotificationCompat.Builder in , The NotificationCompat.Builder is the most easy way to create Notifications on all Android versions. You can even use features that are available with Android  Before getting into NotificationCompact.Builder, we should know what is notification in android. This example demonstrate about how to integrate Android Notification. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Working example:

    Intent intent = new Intent(ctx, HomeActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder b = new NotificationCompat.Builder(ctx);

     .setContentTitle("Default notification")
     .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
     .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)

    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

How to create a notification with NotificationCompat.Builder?, When creating a notification, specify the UI content and actions with a NotificationCompat.Builder object. At bare minimum, a Builder object must include the following: A small icon, set by  To create a notification action that supports direct reply: Create an instance of RemoteInput.Builder that you can add to your notification action. This class's constructor accepts a string that the system uses as the key for the text input. Later, your handheld app uses that key to retrieve the text of the input.

I make this method and work fine. (tested in android 6.0.1)

public void notifyThis(String title, String message) {
    NotificationCompat.Builder b = new NotificationCompat.Builder(this.context);
            .setTicker("{your tiny message}")

    NotificationManager nm = (NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE);

Updating Notifications, val CHANNEL_ID = "CHANNEL_ID" val builder = NotificationCompat. NotificationCompat to make sure our notification is compatible with Android 8.0 (​Oreo)  The NotificationCompact.Builder, similar to other class builders, provides an interface that is used to create a Notification object. NotificationCompact.Builder help you constructing the typical notification layouts.

Show Notificaton in android 8.0

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)

  public void show_Notification(){

    Intent intent=new Intent(getApplicationContext(),MainActivity.class);
    NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,"name",NotificationManager.IMPORTANCE_LOW);
    PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,0);
    Notification notification=new Notification.Builder(getApplicationContext(),CHANNEL_ID)

    NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


Hiding foreground services notifications in Android | Blog, Java Code Examples for Creation of notification on operations completed */ public NotificationBuilder  By default, each new notification in a particular channel will increment the number displayed in the long-press menu. You can change the number appearing by setting a number of your choice while building the notification. Creating a Push Notification on Android Oreo. To create a notification, you will use the NotificationCompat.Builder class.

Building a Notification, NotificationCompat$Builder (Showing top 20 results out of 315) getService(this​, 0, exitIntent, 0)); notification =; notification.flags |= Notification. Summary. This walkthrough used NotificationCompat.Builder to create and display notifications. It showed a basic example of how to start up a second Activity as a way to respond to user interaction with the notification, and it demonstrated the transfer of data from the first Activity to the second Activity.

Android Notifications Overview, In this article, I'm going to show you how to get started with notifications by creating a very simple and straightforward notification that, when  In this article, I’m going to show you how to get started with notifications by creating a very simple and straightforward notification that, when tapped, launches the user’s browser and boots, In this post i explain you how to create notification using NotificationCompat.​Builder in android. NotificationManager. Android Notifications are  Finally, you pass the Notification object to the system by calling NotificationManager.notify() to send your notification. Make sure you call method on builder object before notifying it. This method combines all of the options that have been set and return a new Notification object.$Builder java code examples , Creating notifications. You create a notification using the NotificationCompat.​Builder class. (Use NotificationCompat for the best backward compatibility. For more  Create images that you want to use and add them to your project’s drawable folder. Title Text: You can either set your notification’s title by referencing a string resource, or by adding the text to your notification directly.