NotificationCompat.Builder doesn't accept 2nd

2019-01-09 19:20发布

问题:

For some reason my NotificationCompat.Builder will not accept 2nd argument and I have no idea how to fix it. I saw some other answers but mostly the problem was in the gradle version, but mine is up-to-date as shown below:

if (Build.VERSION.SDK_INT >= 26) {
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Notification mNotification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Content Title")
                .setContentText("Content Text")
                .setSmallIcon(R.drawable.ic_check)
                .setContentIntent(mPendingIntent)
                .build();

            startForeground(1, mNotification);
            mNotification.notify();
        }

and these are my gradle files

build.gradle:project

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle:app

apply plugin: 'com.android.application'

    android {
        compileSdkVersion 26
        defaultConfig {
            applicationId "rs.dreamlight.parkomat"
            minSdkVersion 15
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            vectorDrawables.useSupportLibrary = true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }


    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:26.1.0'
        implementation 'com.android.support:support-v4:26.1.0'
        implementation 'com.android.support.constraint:constraint-layout:1.0.2'
        implementation 'com.android.support:support-vector-drawable:26.1.0'
        implementation 'com.google.code.gson:gson:2.8.4'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.1'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
        implementation 'com.android.support:design:26.1.0'
        implementation 'com.android.support:cardview-v7:26.1.0'
        implementation 'com.github.clans:fab:1.6.4'
    }

Any ideas?

回答1:

Please make sure that you are including the correct version of the NotificationCompat library: import android.support.v4.app.NotificationCompat;. Here is the official guide on Notification Channels, which are a new feature as of Android O: https://developer.android.com/training/notify-user/channels.



回答2:

TL;DR; API level of target dependency was incorrect in my build.gradle file.

Complete details: To begin with, in my import statement I was referring the correct version of the package as suggested by Matt:

import android.support.v4.app.NotificationCompat;

Then the question and Matt's answer collectively gave me the pointer to resolve my own particular issue which was giving exactly same compilation error as faced by OP. In my case the issue was in app module's build.gradle file.

My original dependency was as below:

implementation 'com.android.support:appcompat-v7:25.1.0'

I changed it to below to resolve my error:

implementation 'com.android.support:appcompat-v7:26.1.0'

Essentially, my target API level was incorrect as channelId parameter was introduced in API level 26 onward as mentioned here and here.

Interestingly enough, I saw only one compilation error in Logcat window. After reading details in this thread I went to checkout my app module's build.gradle file where I saw the root cause of the issue. There was a red squiggle shown for the implementation statement with below error message:

Error message title:

This support library should not use a different version (25) than the compileSdkVersion (26)

Error message detail:

There are some combinations of libraries, or tools and libraries, that are incompatible, or can lead to bugs. One such incompatibility is compiling with a version of the Android support libraries that is not the latest version (or in particular, a version lower than your targetSdkVersion).

The reason why it took me a while to debug this issue is that, this particular error related to wrong dependency version wasn't being shown anywhere in output logs either during build or Gradle sync. Android Studio should show such errors more promptly to the developer.