如何防止每次创建新通知时通知被覆盖?
How to prevent notifications being overwritten each time a new notification is created?
我试图在数据库中注册项目后的特定时间(6 小时或 12 小时)发送通知。当我只注册一个项目时,通知工作正常,但是如果我在很短的时间内注册了 2 个项目,第一个通知将被第二个覆盖。
我知道我必须将 ID 添加到待处理的意图中,也许还要将 ID 添加到实际通知中,但是我对 alarmManager 不是很熟悉 class 而且我不知道应该在哪里添加身份证。
如何让这两个通知相互独立?
NotificationHelper Class:
public static class NotificationHelper extends ContextWrapper {
public static final String channel1ID = "channel1ID";
public static final String channel1Name = "USER1";
private NotificationManager mManager;
public NotificationHelper(Context base) {
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannels();
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannels() {
NotificationChannel channel1 = new NotificationChannel(channel1ID, channel1Name, NotificationManager.IMPORTANCE_HIGH);
channel1.enableLights(true);
channel1.enableVibration(true);
channel1.shouldVibrate();
channel1.setShowBadge(true);
channel1.setLightColor(R.color.colorPrimary);
channel1.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.setUsage(USAGE_NOTIFICATION)
.build();
channel1.setSound(uri, audioAttributes);
getManager().createNotificationChannel(channel1);
}
public NotificationManager getManager() {
if (mManager == null) {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
public NotificationCompat.Builder getChannel1Notification() {
return new NotificationCompat.Builder(getApplicationContext(), channel1ID)
.setContentTitle("Dressing")
.setContentText("Please scan the dressing on your: " + (et_DressingPos.getText().toString().trim()))
.setSmallIcon(R.drawable.ic_cnoticiation_scan);
}
}
报警接收器Class:
@Override
public void onReceive(Context context, Intent intent) {
Camera2Register.NotificationHelper notificationHelper = new Camera2Register.NotificationHelper(context);
NotificationCompat.Builder nb = notificationHelper.getChannel1Notification();
Intent intent2 = new Intent(context, Camera2.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent2, 0);
nb.setContentIntent(pendingIntent);
notificationHelper.getManager().notify(1, nb.build());
}
}
startAlarmScan 和 startALarmChange 方法:
private void startAlarmScan() {
int requestCode = ("someString" + System.currentTimeMillis()).hashCode();
Long time = System.currentTimeMillis();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("randomRequestCode", requestCode);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0);
long milliseconds = 5000;
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, (time + milliseconds), pendingIntent);
//}
}
private void startAlarmChange() {
int requestCode = ("someString" + System.currentTimeMillis()).hashCode();
Long time = System.currentTimeMillis();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("randomRequestCode", requestCode);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0);
long milliseconds = 30000;
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, (time + milliseconds), pendingIntent2);
//}
}
我已将两个不同通知的时间延迟设置为 5 秒和 30 秒。如果在 30 秒内注册并 class 这两个 startAlarmScan 和 startAlarmChange 方法。第一个预定通知将被第二个覆盖。
我开始认为问题可能出在我的通知系统的结构上,是否需要接收器 class? Android.
的通知文档中没有提及它
您在此处使用常量值“1”作为您的通知 ID:notificationHelper.getManager().notify(1, nb.build());
来自文档:Here "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"
将此 1 更改为每个通知的唯一编号,您应该会看到它们全部通过。
编辑
你也做同样的事情就行了:
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent2, 0);
请求代码“1”用于比较未决意图使用 filterEquals(Intent intent)
This 问题讨论它。
我试图在数据库中注册项目后的特定时间(6 小时或 12 小时)发送通知。当我只注册一个项目时,通知工作正常,但是如果我在很短的时间内注册了 2 个项目,第一个通知将被第二个覆盖。
我知道我必须将 ID 添加到待处理的意图中,也许还要将 ID 添加到实际通知中,但是我对 alarmManager 不是很熟悉 class 而且我不知道应该在哪里添加身份证。 如何让这两个通知相互独立?
NotificationHelper Class:
public static class NotificationHelper extends ContextWrapper {
public static final String channel1ID = "channel1ID";
public static final String channel1Name = "USER1";
private NotificationManager mManager;
public NotificationHelper(Context base) {
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannels();
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannels() {
NotificationChannel channel1 = new NotificationChannel(channel1ID, channel1Name, NotificationManager.IMPORTANCE_HIGH);
channel1.enableLights(true);
channel1.enableVibration(true);
channel1.shouldVibrate();
channel1.setShowBadge(true);
channel1.setLightColor(R.color.colorPrimary);
channel1.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.setUsage(USAGE_NOTIFICATION)
.build();
channel1.setSound(uri, audioAttributes);
getManager().createNotificationChannel(channel1);
}
public NotificationManager getManager() {
if (mManager == null) {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
public NotificationCompat.Builder getChannel1Notification() {
return new NotificationCompat.Builder(getApplicationContext(), channel1ID)
.setContentTitle("Dressing")
.setContentText("Please scan the dressing on your: " + (et_DressingPos.getText().toString().trim()))
.setSmallIcon(R.drawable.ic_cnoticiation_scan);
}
}
报警接收器Class:
@Override
public void onReceive(Context context, Intent intent) {
Camera2Register.NotificationHelper notificationHelper = new Camera2Register.NotificationHelper(context);
NotificationCompat.Builder nb = notificationHelper.getChannel1Notification();
Intent intent2 = new Intent(context, Camera2.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent2, 0);
nb.setContentIntent(pendingIntent);
notificationHelper.getManager().notify(1, nb.build());
}
}
startAlarmScan 和 startALarmChange 方法:
private void startAlarmScan() {
int requestCode = ("someString" + System.currentTimeMillis()).hashCode();
Long time = System.currentTimeMillis();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("randomRequestCode", requestCode);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0);
long milliseconds = 5000;
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, (time + milliseconds), pendingIntent);
//}
}
private void startAlarmChange() {
int requestCode = ("someString" + System.currentTimeMillis()).hashCode();
Long time = System.currentTimeMillis();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("randomRequestCode", requestCode);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0);
long milliseconds = 30000;
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, (time + milliseconds), pendingIntent2);
//}
}
我已将两个不同通知的时间延迟设置为 5 秒和 30 秒。如果在 30 秒内注册并 class 这两个 startAlarmScan 和 startAlarmChange 方法。第一个预定通知将被第二个覆盖。
我开始认为问题可能出在我的通知系统的结构上,是否需要接收器 class? Android.
的通知文档中没有提及它您在此处使用常量值“1”作为您的通知 ID:notificationHelper.getManager().notify(1, nb.build());
来自文档:Here "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"
将此 1 更改为每个通知的唯一编号,您应该会看到它们全部通过。
编辑 你也做同样的事情就行了:
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent2, 0);
请求代码“1”用于比较未决意图使用 filterEquals(Intent intent)
This 问题讨论它。