gpt4 book ai didi

android - 如何获取特定通知的 ID

转载 作者:行者123 更新时间:2023-11-29 19:15:00 25 4
gpt4 key购买 nike

您好,我已经创建了一个 Notification 类,我可以从中创建多个这样的通知:

int id=0;
id++;
notification = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setSmallIcon(icon)
.setContentText(dataNotes)
.setWhen(time)
.setAutoCancel(false)
.setOngoing(true)
.addAction(action)
.setStyle(new NotificationCompat.BigTextStyle().bigText(dataNotes))
.build();

nm.notify(id,notification);

所以,我的问题是如何获取我创建的每个通知的 ID。我想在我想取消特定通知时使用该 ID。我知道 StatusBarNotification 包含 getId() 获取 id 的方法,但我不知道如何实现它。任何人都可以帮助我。

最佳答案

只需将您的通知 ID 声明为静态即可。每次您创建通知时,都会为每个通知设置一个唯一的 ID。

每次您对通知执行操作时,这都会为您获取 Intent ID。

如下所示创建一个通知按钮以对每个通知执行一些操作。

public ActionButtonConfig getEditCityButton(CountryConfig countryConfig,
CityConfig cityConfig, Integer notificationId) {
ArrayList<Class> params = new ArrayList<>();
params.add(context.getClass());
params.add(countryConfig.getClass());
params.add(cityConfig.getClass());
params.add(notificationId.getClass());

ArrayList<Object> arguments = new ArrayList<>();
arguments.add(context);
arguments.add(countryConfig);
arguments.add(cityConfig);
arguments.add(notificationId);
return getActionButton(NotificationButton.ACTION, getParamArray(params),
arguments.toArray());

此操作按钮配置将为您提供启用您需要对通知执行的任何操作。

public static int notificationCount = 0;

void buildNotification(NotificationConfig notification) {
try {

buildNotification(//get items from notification config here and also notificationCount);
notificationCount++;
} catch (Exception e) {
Log.e("Notification builder err",e);
}
}

在上面的代码中,看到通知计数是静态的,每次生成通知时,通知计数都会更新,以前的值可以在以后的阶段使用。

进一步通知使用通知管理器生成通知,如您在最后一行所做的那样。

希望这能奏效。干杯

关于android - 如何获取特定通知的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43866434/

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