我正在尝试使用目标 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/