1

I am trying to create a simple notification in android. The purpose is to show a small icon on top of the phone screen when something is happening. Then I can tap on the notification which will either start the app or brings it in the foreground.

For just testing purposes I am following this hard-to-understand-and-not-simple example and also found this entry for the first error I get.

Anyway here is the code:

 Notification mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.icon1) .setContentTitle("My notification") .setContentText("Hello World!"); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(this, 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(this); // 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) getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. int mId = 1001; mNotificationManager.notify(mId, mBuilder.build()); 

and here is what I am importing:

... import android.app.Notification; import android.app.NotificationManager; .... import android.support.v7.app.ActionBar; //import android.support.v4.app.NotificationCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.app.NotificationCompat; import android.support.v7.widget.Toolbar; 

The current error is

Error:(117, 32) error: incompatible types required: Notification found: Builder 

How to fix this problem?

2
  • "Anyway here is the code" -- The docs appear to have it typed in correctly, whereas your opening lines attempt to assign a NotificationCompat.Builder to a Notification. Commented Dec 20, 2015 at 13:53
  • Neither works. The only thing that changes is the error message. Commented Dec 20, 2015 at 13:55

4 Answers 4

3

Change

Notification mBuilder 

with

NotificationCompat.Builder mBuilder 

Since you are instantiating new NotificationCompat.Builder(this) you can't assign it to a Notification object

Sign up to request clarification or add additional context in comments.

7 Comments

Error:(117, 32) error: incompatible types required: android.app.Notification.Builder found: android.support.v4.app.NotificationCompat.Builder
my bad you need NotificationCompat.Builder
Error:(117, 32) error: incompatible types required: android.support.v7.app.NotificationCompat.Builder found: android.support.v4.app.NotificationCompat.Builder
you have to be consistent with the import. All the imports have to be from the same package v4.app or v7.app. Check in the documentation which suits your needs better
I do not see any 'v4' imports. NotificationCompat is v7: import android.support.v7.app.NotificationCompat;
|
1

I had the same issue following Udacity tutorial on Android and found out that I was missing a cast. You should change your code to

 NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getContext()) .setSmallIcon(R.drawable.icon1) .setContentTitle(My notification") .setContentText("Hello World!"); 

You are using a NotificationCompat and you are assigning to a Notification

1 Comment

Weirdly, this solved my issue. But I don't understand why since .setContentText(..) and the other methods return a builder object, not a notification?
1

Continuing with answer from @Blackbelt, android.support.v7.app.NotificationCompat.Builder is a subclass of android.support.v4.app.NotificationCompat.Builder and its member functions like setSmallIcon(R.drawable.icon1) returns instances of android.support.v4.app.NotificationCompat.Builder which cannot be casted to android.support.v7.app.NotificationCompat.Builder. So, best practice would be:

import android.support.v7.app.NotificationCompat; . . . NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getContext()); mBuilder.setSmallIcon(R.drawable.icon1) .setContentTitle("My notification") .setContentText("Hello World!"); 

Comments

0

Use the below code snippet it would work for creating the notification in android

import android.app.NotificationManager; import android.support.v4.app.NotificationCompat; //build your notification here. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getContext()) .setSmallIcon(R.drawable.icon1) .setContentTitle("My notification") .setContentText("Hello World!") .setAutoCancel(true); //notification will be removed when once you enter application. 

Hope this helps you..

Cheers

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.