Android 如何获取特定通知的id
嗨,我已经创建了一个通知类,我可以从中创建多个通知,如下所示:Android 如何获取特定通知的id,android,Android,嗨,我已经创建了一个通知类,我可以从中创建多个通知,如下所示: int id=0; id++; notification = new NotificationCompat.Builder(this) .setContentTitle(title) .setSmallIcon(icon) .setContentText(dataNotes) .setWhen(time) .set
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
包含获取id的getId()
方法,但我不知道如何实现它。有人能帮我吗。只需将通知id声明为静态。每次创建通知时,都会为每个通知设置一个唯一的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);
}
}
这将在每次对通知执行操作时获取意图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);
}
}
在上面的代码中,可以看到通知计数是静态的,每次生成通知时,都会更新通知计数,并且可以在以后的阶段使用以前的值
进一步通知使用Notification Manager生成通知,如您在最后一行中所做的
希望这能奏效。干杯请考虑在创建ID时存储它们。然后你有一个巨大的通知ID列表,你可以随意使用。可能是更好的方法,我不熟悉通知的用法。@DoomsKnight hi我尝试过将所有id存储在arraylist中的方法,但我不知道如何使用它如果你有想要取消的id,你可以在通知管理器上调用
cancel
方法,使用该id。请参见此处的答案:很高兴这有帮助。