Kuldeep Kumar
Kuldeep Kumar

Reputation: 925

How to import android.support.v7.app.NotificationCompat.Builder class in Android Studio

I am trying to implement simple notifications in my android app. I am reffering this developer guide

But getting this error message :

Incompatible types.
Required: android.support.v7app.NotificationCompat.Builder
Found: android.support.v4.app.Notification.Compat.Builder

Error Message screenshot

For the following code snippet :

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

Here are my imports :

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;

I think the correct NotificationCompat class is imported. I am using Android Studio v2.1.2 for development. Please help me with this error message. I am new to android programming and java.

Upvotes: 31

Views: 37997

Answers (3)

Gopal Meena
Gopal Meena

Reputation: 427

Latest working solution 2020

If you have updated to Androidx then
replace

import android.support.v4.app.NotificationCompat
//or
import android.support.v7.app.NotificationCompat

with

import androidx.core.app.NotificationCompat;

Upvotes: 14

Meetarp
Meetarp

Reputation: 2471

The return type of those builder methods are returning the v4 version of NotificationCompat.Builder. The v7 NotificationCompat.Builder extends the v4 version and largely just inherits the methods from it, meaning the return types don't change.

Documentation:

If you need the v7 version (for the support of NotificationCompat.MediaStyle), just cast to it.

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

If not, swap your imports to use the v4 version.

Upvotes: 29

Dhruvi
Dhruvi

Reputation: 1981

Replace

 import android.support.v7.app.NotificationCompat;

with

 import android.support.v4.app.NotificationCompat;

Upvotes: 43

Related Questions