java - 无法获取 NotificationCompat.Builder 2 arg 构造函数

标签 java android android-notifications

我正在尝试使用目标 API 26 和最低 API 19 构建通知。 我无法获得将 channel ID 作为第二个参数的 NotificationCompat.Builder 构造函数。

到目前为止,这是我的通知类。 在最底部,我想获得 Notification Builder。

public class NotificationHelper extends ContextWrapper {
private NotificationManager notificationManager;

public NotificationHelper(Context base) {
    super(base);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createChannel();
    }
}

@TargetApi(Build.VERSION_CODES.O)
public void createChannel() {
    NotificationChannel channel1 = new NotificationChannel("channel1", "Channel one", NotificationManager.IMPORTANCE_DEFAULT);
    channel1.enableLights(true);
    channel1.enableVibration(true);
    channel1.setLightColor(R.color.colorPrimary);
    channel1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

    getManager().createNotificationChannel(channel1);
}

public NotificationManager getManager() {
    if (notificationManager == null) {
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }
    return notificationManager;
}

public NotificationCompat.Builder() {
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(getApplicationContext(), "channel1");
}

最佳答案

您应该将 google 支持库设置为 26.1.0 或更高版本并检查您的 build.gradle。它应该是这样的:

apply plugin: 'com.android.application'
//...
repositories {
    maven { url 'https://maven.google.com' }
//..
}

android {
//...
}

关于java - 无法获取 NotificationCompat.Builder 2 arg 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46573656/

相关文章:

java - 为什么 Java 似乎不尊重我的类路径?

java - 什么是非最终变量?

android - fragment 中的 startActivityForResult 不工作

Android 后退按钮按下检查

java - 只要应用程序打开,AlarmManager 就会关闭

android - 如何从通知中删除时间计数器?

java/beans 验证 - 集合/映射不包含空值

java - Android 中的对齐文本

android - 如果应用程序位于前台,则通知 ParentStack 重新启动父级

java - 如何从 HashMap<String,Object> 中的 gson 获取字符串,如下所示