我正在开发一个将生成通知的 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);
}
最佳答案
您需要为每个 Notification
传递不同的 notification id
。如果您传递相同的 ID(即您的情况下为 0),现有的通知
将使用新数据进行更新
所以改变这个:
notificationManager.notify(0, notification);
例如,设置一些唯一的 id int notificationId = 0x10;
notificationManager.notify(++notificationId, notification);
ID - 如果您的应用程序已发布具有相同 ID 的通知且尚未取消,则该通知将被更新后的信息替换。
关于android - 如何同时显示多个通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23567692/