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

Vla*_*mir 51 notifications android android-notifications android-notification-bar

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

tha*_*sma 123

NotificationCompat.Builder是创建最简单的方式Notifications对所有的Android版本.您甚至可以使用Android 4.1提供的功能.如果您的应用在Android> = 4.1的设备上运行,则会使用新功能,如果在Android <4.1上运行,则通知将是一个简单的旧通知.

要创建一个简单的通知,请执行此操作(请参阅Android 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
Run Code Online (Sandbox Code Playgroud)

你必须至少设置smallIcon,contentTitlecontentText.如果您错过了一个通知将不会显示.

注意:在Gingerbread和下面你必须设置内容意图,否则IllegalArgumentException将被抛出.

要创建无效的intent,请使用:

final Intent emptyIntent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, NOT_USED, emptyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)

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

mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
Run Code Online (Sandbox Code Playgroud)

通知通过NotificationManager添加到栏中:

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(mId, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)

  • 它会自动显示在通知栏中吗?以及如何添加声音? (2认同)
  • 它是通知的ID,以便您稍后可以引用它来解除或替换它. (2认同)
  • 谢谢你`你必须至少设置smallIcon,contentTitle和contentText.如果你错过了一个通知将不会显示.这条线解决了我的头痛.没有意识到你必须设置所有这些. (2认同)

Sum*_*and 26

工作范例:

    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());
Run Code Online (Sandbox Code Playgroud)

  • 嗨,我试过这段代码,但我没有任何对 NotificationCompat.Builder 的引用......它是否缺少什么? (2认同)
  • @HUSNAINSARWAR,因为这发生在API> 21(棒棒糖及以上).要解决这个问题,请为`setSmallIcon(R.id.your_icon_small)`指定一个图标,并设置一个像这样的主图标`mBuilder.setLargeIcon(BitmapFactory .decodeResource(context.getResources(),R.drawable.icon))`for API > 21. (2认同)

Ank*_*ava 10

在 android 8.0 中显示通知

@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)

  public void show_Notification(){

    Intent intent=new Intent(getApplicationContext(),MainActivity.class);
    String CHANNEL_ID="MYCHANNEL";
    NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,"name",NotificationManager.IMPORTANCE_LOW);
    PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,0);
    Notification notification=new Notification.Builder(getApplicationContext(),CHANNEL_ID)
            .setContentText("Heading")
            .setContentTitle("subheading")
            .setContentIntent(pendingIntent)
            .addAction(android.R.drawable.sym_action_chat,"Title",pendingIntent)
            .setChannelId(CHANNEL_ID)
            .setSmallIcon(android.R.drawable.sym_action_chat)
            .build();

    NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    notificationManager.notify(1,notification);


}
Run Code Online (Sandbox Code Playgroud)


Dar*_*lez 8

我制作这个方法并且工作正常.(在android 6.0.1中测试过)

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());
}
Run Code Online (Sandbox Code Playgroud)