gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:20:18 28 4
gpt4 key购买 nike

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

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com