android - 如何在 Android 应用程序中一次只显示一个通知

标签 android notifications

我使用下面的代码来显示通知

private void SendNotification(String notificationTitle, String notificationMessage) 
{
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, notificationMessage,
        System.currentTimeMillis());

Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra("notif_id", "5100");

//Setting the single top property of the notification and intent.
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS; 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(MainActivity.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify((int)System.currentTimeMillis(), notification);
}

每次收到新通知时,应删除前一个通知并显示新通知。

任何线索都会有所帮助。

提前致谢

哦。

最佳答案

您每次都生成具有不同 ID 的通知 (System.currentTimeMillis()),请将其更改为单个 ID。

notificationManager.notify(0, notification);

Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

NotificationManager.html

关于android - 如何在 Android 应用程序中一次只显示一个通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16891895/

相关文章:

android - 通知未在特定时间和日期显示

iphone - 在后台运行时像 Pastebot 一样抓取 UIPasteboard

macos - 我可以在 OSX 中使用 iOS 的本地推送通知吗

android - 我可以在 Activity 和服务之间共享 ViewModel 吗?

Android:子类库不会通过 onKeyDown 事件滚动

android - Google Glass - 'Jump' 到 CardScrollView 中的卡片?

android - 在 Android 中显示 SVG 文件

ios - 尝试传回 userInfo 时 NSNotification 在 Swift 中中断

android - PayPal Android SDK 不适用于美元以外的其他货币

android - 推送通知不适用于 FCM