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

时间:2017-10-04 20:30:34

标签: java android android-notifications

我正在尝试使用Target API 26和min API 19构建通知。 我无法获得NotifyCompat.Builder构造函数,它将Channel ID作为第二个参数。

到目前为止,这是我的通知类。 在最底层,我想获得通知生成器。

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");
}

}

1 个答案:

答案 0 :(得分:2)

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

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

android {
//...
}