Android 如何同时显示多个通知

Android 如何同时显示多个通知,android,notifications,Android,Notifications,我正在开发一个Android应用程序,该应用程序将生成通知,并且运行良好,但问题是,当手机收到一个通知并收到另一个通知时,后面的通知会隐藏第一个通知。我想当应用程序收到许多通知时,所有的通知都应该显示,而不是只显示一个(后面的),请问我怎么做 下面是处理通知的代码 private static void generateNotification(Context context, String message) { int icon = R.drawable.ic_launcher;

我正在开发一个Android应用程序,该应用程序将生成通知,并且运行良好,但问题是,当手机收到一个通知并收到另一个通知时,后面的通知会隐藏第一个通知。我想当应用程序收到许多通知时,所有的通知都应该显示,而不是只显示一个(后面的),请问我怎么做

下面是处理通知的代码

  private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, Activity_SplashScreen.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);      

}

在最后一行
notificationManager.notify(0,notification)通知ID为
0
。如果要显示多个通知,则需要更改该ID。具有相同ID的所有通知都会相互覆盖


您需要为每个
通知传递不同的
通知id
。如果您传递相同的id(即,在您的情况下为0),现有的
通知将用新数据更新

因此,改变这一点:

  notificationManager.notify(0, notification);
例如,设置一些唯一的id int notificationId=0x10

notificationManager.notify(++notificationId, notification);  
ID-如果应用程序已发布具有相同ID的通知,但尚未取消,则该通知将被更新的信息替换