如何使用Notification.Builder

我发现我正在使用不赞成使用的方法(notification.setLatestEventInfo())

它说使用Notification.Builder。

  • 我如何使用它?

当我尝试创build一个新实例时,它告诉我:

Notification.Builder cannot be resolved to a type 

这是在API 11,所以如果你正在开发任何早于3.0的东西,你应该继续使用旧的API。

更新 :NotificationCompat.Builder类已被添加到支持包,所以我们可以使用它来支持API级别v4及以上:

http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html

Notification.Builder API 11或NotificationCompat.Builder API 1

这是一个用法示例。

 Intent notificationIntent = new Intent(ctx, YourClass.class); PendingIntent contentIntent = PendingIntent.getActivity(ctx, YOUR_PI_REQ_CODE, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); NotificationManager nm = (NotificationManager) ctx .getSystemService(Context.NOTIFICATION_SERVICE); Resources res = ctx.getResources(); Notification.Builder builder = new Notification.Builder(ctx); builder.setContentIntent(contentIntent) .setSmallIcon(R.drawable.some_img) .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img)) .setTicker(res.getString(R.string.your_ticker)) .setWhen(System.currentTimeMillis()) .setAutoCancel(true) .setContentTitle(res.getString(R.string.your_notif_title)) .setContentText(res.getString(R.string.your_notif_text)); Notification n = builder.build(); nm.notify(YOUR_NOTIF_ID, n); 

除了所选的答案,还有一些来自Source Tricks的NotificationCompat.Builder类的示例代码:

 // Add app running notification private void addNotification() { NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Notifications Example") .setContentText("This is a test notification"); Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentIntent); // Add as notification NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(FM_NOTIFICATION_ID, builder.build()); } // Remove notification private void removeNotification() { NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.cancel(FM_NOTIFICATION_ID); } 

通知生成器严格适用于Android API Level 11及更高版本(Android 3.0及更高版本)。

因此,如果您没有定位蜂窝平板电脑,则不应该使用Notification Builder,而应按照以下示例中的旧通知创build方法进行操作。

我遇到了构build通知的问题(仅针对Android 4.0+开发)。 这个链接显示了我究竟做错了什么,并说:

 Required notification contents A Notification object must contain the following: A small icon, set by setSmallIcon() A title, set by setContentTitle() Detail text, set by setContentText() 

基本上我错过了其中的一个。 作为排除故障的基础,确保至less拥有所有这些。 希望这可以节省别人的头痛。

更新android-N(三月-2016)

请访问通知更新链接了解更多详情。

  • 直接回复
  • 捆绑的通知
  • 自定义视图

Android N还允许您将类似的通知绑定为单个通知。 为了实现这一点,Android N使用现有的NotificationCompat.Builder.setGroup()方法。 用户可以扩展每个通知,并从通知栏中单独执行诸如回复和解除每个通知等操作。

这是一个预先存在的示例,显示使用NotificationCompat发送通知的简单服务。 来自用户的每个未读对话都作为不同的通知发送。

此示例已更新,以利用Android N中提供的新通知function。

示例代码 。

万一它可以帮助任何人…我在使用支持包设置通知时遇到了很多麻烦,当testing更旧的API时。 我能够让他们在新设备上工作,但会在旧设备上进行错误testing。 终于为我工作的是删除所有与通知function相关的导入。 特别是NotificationCompat和TaskStackBuilder。 看起来,虽然在开始时设置我的代码,从较新的版本,而不是从支持包添加的导入。 然后当我想在日食中实现这些项目时,我没有提示再次导入它们。 希望这是有道理的,这有助于别人:)

它甚至在API 8中也可以使用这个代码:

  Notification n = new Notification(R.drawable.yourownpicturehere, getString(R.string.noticeMe), System.currentTimeMillis()); PendingIntent i=PendingIntent.getActivity(this, 0, new Intent(this, NotifyActivity.class), 0); n.setLatestEventInfo(getApplicationContext(), getString(R.string.title), getString(R.string.message), i); n.number=++count; n.flags |= Notification.FLAG_AUTO_CANCEL; n.flags |= Notification.DEFAULT_SOUND; n.flags |= Notification.DEFAULT_VIBRATE; n.ledARGB = 0xff0000ff; n.flags |= Notification.FLAG_SHOW_LIGHTS; // Now invoke the Notification Service String notifService = Context.NOTIFICATION_SERVICE; NotificationManager mgr = (NotificationManager) getSystemService(notifService); mgr.notify(NOTIFICATION_ID, n); 

或者我build议遵循一个关于这个优秀的教程

我用过了

 Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Firebase Push Notification") .setContentText(messageBody) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); 

我注意到, 这从API级别11(Android 3.0)的作品。

  // This is a working Notification private static final int NotificID=01; b= (Button) findViewById(R.id.btn); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Notification notification=new Notification.Builder(MainActivity.this) .setContentTitle("Notification Title") .setContentText("Notification Description") .setSmallIcon(R.mipmap.ic_launcher) .build(); NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); notification.flags |=Notification.FLAG_AUTO_CANCEL; notificationManager.notify(NotificID,notification); } }); } 

独立的例子

和这个答案一样的技巧,但是:

  • 自包含:复制粘贴,它会编译和运行
  • 用一个button为您生成尽可能多的通知,只要你喜欢和玩意图和通知ID

资源:

 import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Main extends Activity { private int i; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Button button = new Button(this); button.setText("click me"); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final Notification notification = new Notification.Builder(Main.this) /* Make app open when you click on the notification. */ .setContentIntent(PendingIntent.getActivity( Main.this, Main.this.i, new Intent(Main.this, Main.class), PendingIntent.FLAG_CANCEL_CURRENT)) .setContentTitle("title") .setAutoCancel(true) .setContentText(String.format("id = %d", Main.this.i)) // Starting on Android 5, only the alpha channel of the image matters. // https://stackoverflow.com/a/35278871/895245 // `android.R.drawable` resources all seem suitable. .setSmallIcon(android.R.drawable.star_on) // Color of the background on which the alpha image wil drawn white. .setColor(Color.RED) .build(); final NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(Main.this.i, notification); // If the same ID were used twice, the second notification would replace the first one. //notificationManager.notify(0, notification); Main.this.i++; } }); this.setContentView(button); } } 

在Android 22testing。