2016-02-06 120 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的用途是什么? –