如何同时显示多个通知

时间:2014-05-09 14:51:59

标签: android notifications

我正在开发一个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);      

}

2 个答案:

答案 0 :(得分:3)

您需要为每个notification id传递不同的Notification。如果您传递相同的ID(例如,在您的情况下为0),则现有的notification将使用新数据进行更新

所以改变这个

  notificationManager.notify(0, notification);

,例如,设置一些唯一的id int notificationId = 0x10;

notificationManager.notify(++notificationId, notification);  

ID - 如果包含s ame id has already been posted by your application且尚未取消的通知,则为will be replaced by the updated information

答案 1 :(得分:0)

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

http://developer.android.com/reference/android/app/NotificationManager.html