2016-02-06 122 views
-1

我想在我的服務中有startForeground(int, id, notification),但爲此我需要一個通知參數。對於過去的3小時裏,我一直在試圖創建一個通知,但它只是將不起作用:爲什麼不兼容的類型?

Notification notification = new NotificationCompat.Builder(this) 
       .setContentTitle("Notification title") 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentText("Notification text"); 

以下是錯誤:

enter image description here

我試圖刪除和取代我所有的進口,但沒有任何作品。我怎樣才能解決這個問題?

感謝,

Ruchir

回答

5

你需要調用.build()您的通知生成器來得到通知:

Notification notification = new NotificationCompat.Builder(this) 
       .setContentTitle("Notification title") 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentText("Notification text") // <---- NotificationCompat.Builder 
       .build(); // <-------- 

您正在嘗試一個NotificationCompat.Builder分配給Notification

+0

謝謝,我會在7分鐘內接受您的答案。我有一個關於'startForeground'的問題。什麼是ID? –

+0

@RuchirBaronia這是通知的唯一標識 –

+1

@RuchirBaronia:這是與[notify()']一起使用的相同ID(http://developer.android.com/reference/android/app/NotificationManager.html #notify(INT,%20android.app.Notification))。您可以使用它來識別此「通知」,以防您需要在「通知」仍未處理時更新其內容。 – CommonsWare

1

看看這裏:http://developer.android.com/reference/android/app/Notification.Builder.html

,你會看到有一個最終.build()在鏈的末端。然後返回通知對象。也就是,請嘗試:

Notification notification = new NotificationCompat.Builder(this) 
      .setContentTitle("Notification title") 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentText("Notification text") 
      .build(); 
+0

我有一個關於'startForeground'的問題。 int ID的用途是什麼? –