android - 收到推送通知时添加新通知(不替换以前的)

标签 android notifications notificationmanager

我在我的应用程序中使用推送通知。我需要在发送推送通知时显示通知。如果我发送另一个通知(没有清除之前的通知),它将替换旧通知。

这是我使用的代码:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

int icon = R.drawable.ic_launcher;
CharSequence tickerText = "New notification Pending";
long time = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, time);
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;

// Context context = getApplicationContext();
CharSequence contentTitle = "Notifications";
CharSequence contentText = newMessage;
Intent notificationIntent = new Intent(this, LoginActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
        contentIntent);
mNotificationManager.notify(1, notification);

但我不想替换通知,我想将其添加为新通知。

最佳答案

您还可以使用 System.currentTimeMillis() 为您的通知分配唯一 ID。

int id = (int) System.currentTimeMillis();
mNotificationManager.notify(id, notification);

关于android - 收到推送通知时添加新通知(不替换以前的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17148076/

相关文章:

Laravel:如何为发送电子邮件的通知编写集成测试

android - PendingIntent 不适用于 FCM 通知

android - 无法使用警报管理器取消待定 Intent

android - 是否可以创建一个不生成提醒通知但会出现在锁屏中的通知

java - Android 中的计时器在延迟时无法正确重复

android - GCM 推送通知。已发布错误通知 - 无法扩展 : StatusBarNotification 的 RemoteViews

android - 在 OpenCV (Android) 中禁用自动白平衡

android - 从 Android 发送 AT 命令到 USB 端口

Android - 打开 SQLite 数据库

android - PdfRenderer 无法访问受密码保护的 pdf