AggieDev
AggieDev

Reputation: 5045

Getting error when trying to make notification

I am trying to make my app create a notification but this code gives me compile errors:

public void makeRing(Context context, boolean notify)
    {

        if (notify)
        {
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context)
                    //.setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!");
            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(context, MainActivity.class);

            // The stack builder object will contain an artificial back stack for the
            // started Activity.
            // This ensures that navigating backward from the Activity leads out of
            // your application to the Home screen.
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(MainActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                    );
            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            // mId allows you to update the notification later on (first number)
            mNotificationManager.notify(5954, mBuilder.build());

        }

It is the code taken directly from the Android Developers site. The errors are at the line that says "TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);", where create is underlined and it says 'the method create(Context) is undefined for the type TaskStackBuilder'.

Also, on the last line build() is underlined and it says 'the method build() is undefined for the type NotificationCompat.Builder.

How do I solve these?

Upvotes: 0

Views: 2012

Answers (2)

Marcos
Marcos

Reputation: 4643

I had the same problem using the support library. It seems that it doesn't implement that methods. Here is the code that works:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_action_search)
        .setContentTitle("New search")
        .setContentText("Que hay de nuevo, viejo");
Intent resultIntent = new Intent(this, Temporal.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.from(this);
stackBuilder.addParentStack(Temporal.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(112, builder.getNotification());

The changes are

TaskStackBuilder stackBuilder = TaskStackBuilder.from(this);

instead of

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

and

manager.notify(112, builder.getNotification());

instead of

manager.notify(112, mBuilder.build());

EDITED

Better solution: Just download the latest Android Support Library.

Upvotes: 3

Eran
Eran

Reputation: 393771

You probably haven't downloaded the required version of the support library. You can see HERE all the versions, and find out which one you need.

I believe this is the most recent version that contains changes in Notifications :

Support Package, revision 10 (August 2012)

Changes for v4 support library:

    Added support for notification features introduced in Android 4.1 (API level 16) with additions to NotificationCompat.

But you can probably download the most recent version, just to be sure.

Upvotes: 0

Related Questions