android - 如何同时显示多个通知

标签 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);      

}

最佳答案

您需要为每个 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/

相关文章:

android - 如何在android中获取broadcastreceiver的id

android - 在 Kotlin 中将位图保存为图像

swift - 如何检测用户何时没有点击通知? (Xcode 8, iOS 10)

notifications - 使用 Firebase 实时推送通知

javascript - chrome.notifications 的 iconUrl 错误

java - 一个奇怪的 RecyclerView 故障

java - 在 Android 中发送邮件抛出 java.lang.NoClassDefFoundError : javax. activation.DataHandler

android - mediacodec 与 mediaplayer 和 mediarecorder

android - 如何以编程方式删除正在进行的通知?

后台 iOS 日历监控 - 它是如何完成的?