【问题标题】:Can't get the NotificationCompat.Builder 2 arg Constructor无法获取 NotificationCompat.Builder 2 arg 构造函数
【发布时间】:2017-10-04 20:30:33
【问题描述】:

我正在尝试使用 Target API 26 和 min API 19 构建通知。 我无法获得将 Channel ID 作为第二个参数的 NotificationCompat.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");
}

}

【问题讨论】:

  • 请在此处发布您的build.gradle 文件。
  • 我正在尝试,但格式不正确。我们在寻找什么?
  • 您必须将支持库设置为 26.+ 才能拥有新的构造函数。
  • 谢谢,它是在 26.0.0-alpha1,但 26.0.1 成功了

标签: java android android-notifications


【解决方案1】:

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

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

android {
//...
}

【讨论】:

  • 谢谢。我只想提一下,26.0.1 也可以。
猜你喜欢
  • 1970-01-01
  • 2017-11-02
  • 1970-01-01
  • 2017-08-30
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多