如何使用NotificationCompat.Builder创build通知?

我需要创build一个简单的通知,如果可能的话,它会显示在通知栏中,并伴有声音和图标? 我也需要它与Android 2.2兼容,所以我发现NotificationCompat.Builder适用于4以上的所有API。如果有更好的解决scheme,请随时提及它。

NotificationCompat.Builder是在所有Android版本上创buildNotifications最简单的方法。 您甚至可以使用Android 4.1提供的function。 如果您的应用程序在Android> = 4.1的设备上运行,则将使用新function,如果在Android <4.1上运行,则通知将是一个简单的旧通知。

要创build一个简单的通知(请参阅Android API API通知 ):

 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setContentIntent(pendingIntent); //Required on Gingerbread and below 

你必须至less设置smallIconcontentTitlecontentText 。 如果你错过了一个通知将不会显示。

注意:在姜饼和下面你必须设置内容的意图,否则会抛出IllegalArgumentException

要创build一个什么都不做的意图,请使用:

 final Intent emptyIntent = new Intent(); PendingIntent pendingIntent = PendingIntent.getActivity(ctx, NOT_USED, emptyIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

您可以通过构build器添加声音,即来自RingtoneManager的声音:

 mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 

通知通过NotificationManager添加到栏中:

 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(mId, mBuilder.build()); 

工作示例:

  Intent intent = new Intent(ctx, HomeActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder b = new NotificationCompat.Builder(ctx); b.setAutoCancel(true) .setDefaults(Notification.DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_launcher) .setTicker("Hearty365") .setContentTitle("Default notification") .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.") .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND) .setContentIntent(contentIntent) .setContentInfo("Info"); NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, b.build()); 

我做这个方法,工作正常。 (在Android 6.0.1中testing)

 public void notifyThis(String title, String message) { NotificationCompat.Builder b = new NotificationCompat.Builder(this.context); b.setAutoCancel(true) .setDefaults(NotificationCompat.DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.favicon32) .setTicker("{your tiny message}") .setContentTitle(title) .setContentText(message) .setContentInfo("INFO"); NotificationManager nm = (NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(1, b.build()); } 

你可以尝试这个代码,这对我工作正常NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

  Intent i = new Intent(noti.this, Xyz_activtiy.class); PendingIntent pendingIntent= PendingIntent.getActivity(this,0,i,0); mBuilder.setAutoCancel(true); mBuilder.setDefaults(NotificationCompat.DEFAULT_ALL); mBuilder.setWhen(20000); mBuilder.setTicker("Ticker"); mBuilder.setContentInfo("Info"); mBuilder.setContentIntent(pendingIntent); mBuilder.setSmallIcon(R.drawable.home); mBuilder.setContentTitle("New notification title"); mBuilder.setContentText("Notification text"); mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(2,mBuilder.build()); 

使用这个代码

  Intent intent = new Intent(getApplicationContext(), SomeActvity.class); PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), (int) System.currentTimeMillis(), intent, 0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext()) .setSmallIcon(R.drawable.your_notification_icon) .setContentTitle("Notification title") .setContentText("Notification message!") .setContentIntent(pIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, mBuilder.build());