2017-10-04 46 views
2

我正在嘗試使用目標API 26和最小API 19建立通知。 我無法獲取將Channel ID作爲第二個參數的NotificationCompat.Builder構造函數。無法獲取NotificationCompat.Builder 2參數構造函數

這是我的通知類到目前爲止。 在最底部我想獲得通知生成器。

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

}

+0

請在這裏發佈您的'build.gradle'文件。 –

+0

我在嘗試,但沒有正確格式化。我們在找什麼? –

+0

您必須將支持庫設置爲26. +以獲得新的構造函數。 –

回答

1

您應該設置谷歌的支持庫,以26.1.0或更高,並檢查您的build.gradle。它應該是這樣的:

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

android { 
//... 
} 
+1

謝謝。我只想提一提,26.0.1也可以。 –