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

Replace

 import android.support.v7.app.NotificationCompat;

with

 import android.support.v4.app.NotificationCompat;

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:

  • v4
  • v7

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.


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;