【问题标题】:NotificationCompat.Builder only has deprecated constructorNotificationCompat.Builder 只有不推荐使用的构造函数
【发布时间】:2026-01-31 09:30:01
【问题描述】:

我正在尝试将推送通知添加到我的 Android 应用程序。目前最小 sdk 版本是 22,目标版本是 26。推送通知在使用 26 之前的 api 时有效,但在 api 26(Oreo) 上运行时没有通知显示。

研究告诉我,我需要使用新的构造函数NotificationCompat.Builder(Context, ChannelID),但我只有 depracated 选项可用,似乎无法弄清楚如何使用新版本。我怎样才能获得这个新的构造函数,同时还能让我的应用与旧的 api 版本兼容?

private void sendNotification(String title, String messageBody, String clickAction) {


    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setChannel(channelId)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(1 /* ID of notification */, notificationBuilder.build());


}

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
    applicationId "com.ventech.tempmonitor"
    minSdkVersion 22
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false

        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.sun.jersey:jersey-core:1.19.4'
compile 'com.android.support:design:26.+'
compile 'com.androidplot:androidplot-core:1.5.1'
compile 'com.google.code.gson:gson:2.8.1'
compile 'com.google.firebase:firebase-messaging:10.0.1'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'

【问题讨论】:

  • 你能贴出你的代码吗
  • 你更新了 build.gradle 并同步了 compile 'com.android.support:appcompat-v7:26.0.0' 吗?
  • 尝试 NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "default"); NotificationCompat.Builder(上下文上下文)构造函数在 API 级别 26.0.0 中已弃用
  • "build.gradle 已经编译 'com.android.support:appcompat-v7:26.+'" -- 首先,不要使用+。指定特定版本(例如,26.1.0)。其次,如果您使用的是该版本,您可以访问the correct constructor,它将处理旧设备。请解释“我只有可用的已弃用选项”和“我怎样才能获得这个新的构造函数”是什么意思。
  • 你能发布你的应用级 gradle 吗?感觉就像您使用的是旧版本的.. 东西。

标签: java android notifications push


【解决方案1】:

解决了。你们中的一个是正确的。

compile 'com.android.support:appcompat-v7:26.+' 并没有真正获取版本 26。我切换到 compile 'com.android.support:appcompat-v7:26.1.0' 现在它可以工作了。

【讨论】:

  • 哈哈,其实你还没有接近。
  • 老兄,在我自己解决之后,您将问题编辑为“正确答案”。你是个大胖子!!!!你的新答案仍然是错误的 OMGGGGG 走开
  • 我不知道,但我想我无法理解你想要什么:'(
【解决方案2】:

将我的项目升级到 Android O

buildToolsVersion "26.0.1"

Android Studio 中的 Lint 显示以下通知生成器方法已弃用的警告:

new NotificationCompat.Builder(context)

文档中提到,构建器方法 NotificationCompat.Builder(Context context) 已被弃用。我们必须使用具有 channelId 参数的构造函数:

NotificationCompat.Builder(Context context, String channelId)

完整答案是here

【讨论】:

  • 我想我们永远不会知道