android - java android设置NotificationCompat.Builder的ID
问题描述
我目前正在使用以下代码创建警报:
public NotificationCompat.Builder getChannelNotification(AbstractEvent event) {
Intent activityIntent = new Intent(context, MainActivity.class);
Intent broadcastIntent = new Intent(context, NotificationReceiver.class);
broadcastIntent.putExtra("id",event.getId());
PendingIntent actionIntent = PendingIntent.getBroadcast(context, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
return new NotificationCompat.Builder(getApplicationContext(), channelID)
.setContentTitle(event.getTitle())
.setContentText("event begins in: " + event.getStartTime)
.setSmallIcon(R.drawable.android)
.setShortcutId("1")
.addAction(R.mipmap.ic_launcher, "Dismiss", actionIntent)
}
这很好地创建了警报,但是当我按下“解除”按钮时,我试图解除警报。
我正在尝试使用以下方法在另一个类中执行此操作:
NotificationManager manager = context.getSystemService(NotificationManager.class);
manager.cancel(id);
这很好,但是我似乎无法设置刚刚发出的警报的 ID。
如何设置/访问我刚刚发出的警报的 ID?
解决方案
您必须手动创建 id 并将其与通知一起设置并将其存储在sharedPreferences
NOTIFICATION_ID = notificationPreferences.getInt("notifnumber",0);
一旦你通知,你增加值并存储它
manager.notify(NOTIFICATION_ID, notification);
editor.putInt("notifnumber", NOTIFICATION_ID + 1);
editor.commit();
推荐阅读
- ios - 如何知道 AnchorEntity 动作序列何时完成?
- html - Bootstrap 将文本与复选框对齐
- visual-studio-code - 什么是 Visual Studio Code 的 Code Review 扩展
- css - 在地图中使用三元运算符,我想使用它来更改材料表的列宽
- amazon-web-services - DynamDB 在两个字段中保存自动生成的值
- javascript - 如何通知子元素作业已完成?
- c++ - 为什么我必须为 std::array 分配大小,而不必是普通数组?
- javascript - 如何为firebase触发电子邮件提供动态地址
- c# - 从 ASP.NET MVC 模型 C# 代码连接到启用 CORS 的 API
- reactjs - 我想将图像放在一个对象或数组中,并在需要时使用它们。我应该怎么办?