如何配置通知渠道以显示多个通知
How to configure notification channels to show multiple notifications
我已经为两个通知渠道编写了代码。但是一个通知会触发另一个通知,因此会显示两个通知。我已经为 pendingIntent
尝试了 FLAG_ONE_SHOT
但它是一样的。
NotificationHelper
class:
public class NotificationHelper extends ContextWrapper {
public static final String channel1ID = "channel1ID";
public static final String channel1Name = "Add transactions reminder";
public static final String channel2ID = "channel2ID";
public static final String channel2Name = "Due and overdue bills";
private NotificationManager mManager;
public NotificationHelper(Context base) {
super(base);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
createChannels();
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void createChannels() {
NotificationChannel channel1 = new NotificationChannel(channel1ID, channel1Name, NotificationManager.IMPORTANCE_DEFAULT);
channel1.enableLights(true);
channel1.enableVibration(true);
channel1.setLightColor(R.color.colorPrimary);
channel1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel1);
NotificationChannel channel2 = new NotificationChannel(channel2ID, channel2Name, NotificationManager.IMPORTANCE_DEFAULT);
channel2.enableLights(true);
channel2.enableVibration(true);
channel2.setLightColor(R.color.colorPrimary);
channel2.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel2);
}
public NotificationManager getManager() {
if (mManager == null) {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
public NotificationCompat.Builder getChannel1Notification(String title, String message) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
int reqCode = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return new NotificationCompat.Builder(getApplicationContext(), channel1ID)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.ic_notification_icon)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
}
public NotificationCompat.Builder getChannel2Notification(String title, String message) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
int reqCode = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return new NotificationCompat.Builder(getApplicationContext(), channel2ID)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.ic_notification_icon)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
}
}
AlertReceiver
class:
public class AlertReceiver extends BroadcastReceiver {
public static final String TAG = "AlertReceiver";
@Override
public void onReceive(Context context, Intent intent) {
NotificationHelper notificationHelper = new NotificationHelper(context);
//add transactions reminder
NotificationCompat.Builder nb1 = notificationHelper.getChannel1Notification(
notificationHelper.getString(R.string.trans_notify_title), notificationHelper.getString(R.string.notification_msg)
);
notificationHelper.getManager().notify(1, nb1.build());
//due and overdue bills
NotificationCompat.Builder nb2 = notificationHelper.getChannel2Notification(
notificationHelper.getString(R.string.bills_notify_title), notificationHelper.getString(R.string.notification_msg)
);
notificationHelper.getManager().notify(2, nb2.build());
}
}
第一条通知:
public void startAlarm(Calendar c) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlertReceiver.class);
int reqCode = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
Toast.makeText(this, getString(R.string.notification_set_at) + timeString + getString(R.string.everydayy), Toast.LENGTH_LONG).show();
}
第二次通知:
private void setNotifications(int dueDayOfYear) {
LocalDate dueDate = LocalDate.ofYearDay(LocalDate.now().getYear(), dueDayOfYear);
LocalDateTime prevDay = dueDate.minusDays(1).atStartOfDay().plusHours(14).plusMinutes(7);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlertReceiver.class);
int reqCode = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, prevDay.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(), pendingIntent);
}
(plusHours
和 plusMinutes
用于测试并确保在不同时间设置通知)
我认为 AlertReceiver
class 有问题,但我想不通。我该如何解决这个问题?
找到问题了。
是在AlertReceiver
class中调用notify()
的时候。我通过在调用它时生成一个唯一的 id 来解决它。
public void onReceive(Context context, Intent intent) {
NotificationHelper notificationHelper = new NotificationHelper(context);
int id = (int) System.currentTimeMillis(); //this is the fix
//add transactions reminder
NotificationCompat.Builder nb1 = notificationHelper.getChannel1Notification(
notificationHelper.getString(R.string.trans_notify_title), notificationHelper.getString(R.string.notification_msg)
);
notificationHelper.getManager().notify(id, nb1.build());
//due and overdue bills
NotificationCompat.Builder nb2 = notificationHelper.getChannel2Notification(
notificationHelper.getString(R.string.bills_notify_title), notificationHelper.getString(R.string.notification_msg)
);
notificationHelper.getManager().notify(id, nb2.build());
我已经为两个通知渠道编写了代码。但是一个通知会触发另一个通知,因此会显示两个通知。我已经为 pendingIntent
尝试了 FLAG_ONE_SHOT
但它是一样的。
NotificationHelper
class:
public class NotificationHelper extends ContextWrapper {
public static final String channel1ID = "channel1ID";
public static final String channel1Name = "Add transactions reminder";
public static final String channel2ID = "channel2ID";
public static final String channel2Name = "Due and overdue bills";
private NotificationManager mManager;
public NotificationHelper(Context base) {
super(base);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
createChannels();
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void createChannels() {
NotificationChannel channel1 = new NotificationChannel(channel1ID, channel1Name, NotificationManager.IMPORTANCE_DEFAULT);
channel1.enableLights(true);
channel1.enableVibration(true);
channel1.setLightColor(R.color.colorPrimary);
channel1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel1);
NotificationChannel channel2 = new NotificationChannel(channel2ID, channel2Name, NotificationManager.IMPORTANCE_DEFAULT);
channel2.enableLights(true);
channel2.enableVibration(true);
channel2.setLightColor(R.color.colorPrimary);
channel2.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel2);
}
public NotificationManager getManager() {
if (mManager == null) {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
public NotificationCompat.Builder getChannel1Notification(String title, String message) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
int reqCode = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return new NotificationCompat.Builder(getApplicationContext(), channel1ID)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.ic_notification_icon)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
}
public NotificationCompat.Builder getChannel2Notification(String title, String message) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
int reqCode = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return new NotificationCompat.Builder(getApplicationContext(), channel2ID)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.ic_notification_icon)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
}
}
AlertReceiver
class:
public class AlertReceiver extends BroadcastReceiver {
public static final String TAG = "AlertReceiver";
@Override
public void onReceive(Context context, Intent intent) {
NotificationHelper notificationHelper = new NotificationHelper(context);
//add transactions reminder
NotificationCompat.Builder nb1 = notificationHelper.getChannel1Notification(
notificationHelper.getString(R.string.trans_notify_title), notificationHelper.getString(R.string.notification_msg)
);
notificationHelper.getManager().notify(1, nb1.build());
//due and overdue bills
NotificationCompat.Builder nb2 = notificationHelper.getChannel2Notification(
notificationHelper.getString(R.string.bills_notify_title), notificationHelper.getString(R.string.notification_msg)
);
notificationHelper.getManager().notify(2, nb2.build());
}
}
第一条通知:
public void startAlarm(Calendar c) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlertReceiver.class);
int reqCode = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
Toast.makeText(this, getString(R.string.notification_set_at) + timeString + getString(R.string.everydayy), Toast.LENGTH_LONG).show();
}
第二次通知:
private void setNotifications(int dueDayOfYear) {
LocalDate dueDate = LocalDate.ofYearDay(LocalDate.now().getYear(), dueDayOfYear);
LocalDateTime prevDay = dueDate.minusDays(1).atStartOfDay().plusHours(14).plusMinutes(7);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlertReceiver.class);
int reqCode = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, prevDay.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(), pendingIntent);
}
(plusHours
和 plusMinutes
用于测试并确保在不同时间设置通知)
我认为 AlertReceiver
class 有问题,但我想不通。我该如何解决这个问题?
找到问题了。
是在AlertReceiver
class中调用notify()
的时候。我通过在调用它时生成一个唯一的 id 来解决它。
public void onReceive(Context context, Intent intent) {
NotificationHelper notificationHelper = new NotificationHelper(context);
int id = (int) System.currentTimeMillis(); //this is the fix
//add transactions reminder
NotificationCompat.Builder nb1 = notificationHelper.getChannel1Notification(
notificationHelper.getString(R.string.trans_notify_title), notificationHelper.getString(R.string.notification_msg)
);
notificationHelper.getManager().notify(id, nb1.build());
//due and overdue bills
NotificationCompat.Builder nb2 = notificationHelper.getChannel2Notification(
notificationHelper.getString(R.string.bills_notify_title), notificationHelper.getString(R.string.notification_msg)
);
notificationHelper.getManager().notify(id, nb2.build());