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.

  • How to solve Firebase API initialization failure (Android + Firebase)
  • Android - SharedPreferences with serializable object
  • Cannot deserialize instance of object out of START_ARRAY token in Spring 3 REST Webservice
  • Waiting for asynchronous callback in Android's IntentService
  • How to disable the particular list item in list-view in android?
  • Genymotion Android emulator - adb access?
  • Related posts:

    Cannot lower case button text in android studio
    How to use local aar dependency?
    Disable soft-keyboard from EditText but still allow copy/paste?
    Does posting Runnable to an UI thread guarantee that layout is finished when it is run?
    Setting encoding for a Multipart Entity
    How to finish destroyed Activity
  • How to listen to GC events in Android
  • Gson deserialization of List<String> into realmList<RealmString>
  • Is it possible to show progress bar when download via Retrofit 2 Asynchronous?
  • How to draw dashed polyline with android google map sdk v2?
  • Android NativeActivity
  • set timeout Dialog in android?
  • 5 Solutions collect form web for “How to create a notification with NotificationCompat.Builder?”

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

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

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

    You can try this code this works fine for me
    NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this);

        Intent i = new Intent(noti.this, Xyz_activtiy.class);
        PendingIntent pendingIntent= PendingIntent.getActivity(this,0,i,0);
        mBuilder.setContentTitle("New notification title");
        mBuilder.setContentText("Notification text");
        NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    Use this code

                Intent intent = new Intent(getApplicationContext(), SomeActvity.class);
                PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(),
                        (int) System.currentTimeMillis(), intent, 0);
                NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(getApplicationContext())
                                .setContentTitle("Notification title")
                                .setContentText("Notification message!")
                NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.