How exactly to use Notification.Builder

I found that I am using a deprecated method for noficitations (notification.setLatestEventInfo())

It says to use Notification.Builder.

  • How do I use it?

When I try to create a new instance, it tells me:

Notification.Builder cannot be resolved to a type

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Notification.Builder API 11 or NotificationCompat.Builder API 1

This is a usage example.

Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
        YOUR_PI_REQ_CODE, notificationIntent,

NotificationManager nm = (NotificationManager) ctx

Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);

            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
Notification n =;

nm.notify(YOUR_NOTIF_ID, n);

Solution 2

This is in API 11, so if you are developing for anything earlier than 3.0 you should continue to use the old API.

Update: the NotificationCompat.Builder class has been added to the Support Package so we can use this to support API level v4 and up:

Solution 3

in addition to the selected answer here is some sample code for the NotificationCompat.Builder class from Source Tricks :

// Add app running notification  

    private void addNotification() {

    NotificationCompat.Builder builder =  
            new NotificationCompat.Builder(this)  
            .setContentTitle("Notifications Example")  
            .setContentText("This is a test notification");  

    Intent notificationIntent = new Intent(this, MainActivity.class);  
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,   

    // Add as notification  
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  

// Remove notification  
private void removeNotification() {  
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  

Solution 4

Notification Builder is strictly for Android API Level 11 and above (Android 3.0 and up).

Hence, if you are not targeting Honeycomb tablets, you should not be using the Notification Builder but rather follow older notification creation methods like the following example.

Solution 5

UPDATE android-N (march-2016)

Please visit Notifications Updates link for more details.

  • Direct Reply
  • Bundled Notifications
  • Custom Views

Android N also allows you to bundle similar notifications to appear as
a single notification. To make this possible, Android N uses the
existing NotificationCompat.Builder.setGroup() method. Users can
expand each of the notifications, and perform actions such as reply
and dismiss on each of the notifications, individually from the
notification shade.

This is a pre-existing sample which shows a simple service that sends
notifications using NotificationCompat. Each unread conversation from
a user is sent as a distinct notification.

This sample has been updated to take advantage of new notification
features available in Android N.

sample code.

Solution 6

I was having a problem building notifications (only developing for Android 4.0+).
This link showed me exactly what I was doing wrong and says the following:

Required notification contents

A Notification object must contain the following:

A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()

Basically I was missing one of these. Just as a basis for troubleshooting with this, make sure you have all of these at the very least. Hopefully this will save someone else a headache.

Solution 7

It works even in API 8
you can use this code:

 Notification n = 
   new Notification(R.drawable.yourownpicturehere, getString(R.string.noticeMe), 

PendingIntent i=PendingIntent.getActivity(this, 0,
             new Intent(this, NotifyActivity.class),
n.setLatestEventInfo(getApplicationContext(), getString(R.string.title), getString(R.string.message), i);
n.flags |= Notification.FLAG_AUTO_CANCEL;
n.flags |= Notification.DEFAULT_SOUND;
n.flags |= Notification.DEFAULT_VIBRATE;
n.ledARGB = 0xff0000ff;
n.flags |= Notification.FLAG_SHOW_LIGHTS;

// Now invoke the Notification Service
String notifService = Context.NOTIFICATION_SERVICE;
NotificationManager mgr = 
   (NotificationManager) getSystemService(notifService);
mgr.notify(NOTIFICATION_ID, n);

Or I suggest to follow an excellent tutorial about this

Solution 8

I have used

Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("Firebase Push Notification")

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


Solution 9

In case it helps anyone… I was having a lot of trouble with setting up notifications using the support package when testing against newer an older API’s. I was able to get them to work on the newer device but would get an error testing on the old device.
What finally got it working for me was to delete all the imports related to the notification functions. In particular the NotificationCompat and the TaskStackBuilder. It seems that while setting up my code in the beginning the imports where added from the newer build and not from the support package. Then when I wanted to implement these items later in eclipse, I wasn’t prompted to import them again. Hope that makes sense, and that it helps someone else out 🙂

Solution 10

          // This is a working Notification
       private static final int NotificID=01;
   b= (Button) findViewById(;
    b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            Notification notification=new       Notification.Builder(MainActivity.this)
                    .setContentTitle("Notification Title")
                    .setContentText("Notification Description")
            NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            notification.flags |=Notification.FLAG_AUTO_CANCEL;


Solution 11

Self-contained example

Same technique as in this answer but:

  • self-contained: copy paste and it will compile and run
  • with a button for you to generated as many notifications as you like and play with intent and notification IDs


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main extends Activity {
    private int i;
    protected void onCreate(Bundle savedInstanceState) {
        final Button button = new Button(this);
        button.setText("click me");
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                final Notification notification = new Notification.Builder(Main.this)
                        /* Make app open when you click on the notification. */
                                new Intent(Main.this, Main.class),
                        .setContentText(String.format("id = %d", Main.this.i))
                        // Starting on Android 5, only the alpha channel of the image matters.
                        // `android.R.drawable` resources all seem suitable.
                        // Color of the background on which the alpha image wil drawn white.
                final NotificationManager notificationManager =
                notificationManager.notify(Main.this.i, notification);
                // If the same ID were used twice, the second notification would replace the first one. 
                //notificationManager.notify(0, notification);

Tested in Android 22.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply