这是我的问题:我有通知列表,希望在不同的时间显示它们,但它们同时出现在一起。
首先调用updateLabel函数:
私有空updateLabel(ArrayList notificationEntities){
int timer = 5000;
for (int i = 0; i < notificationEntities.size(); i++) {
Notification notification = addNotification(notificationEntities.get(i));
scheduleNotification(notificationEntities.get(i).getId(), notification, (long) timer);
timer += 10000;
}
}然后创建通知:
私人通知addNotification(NotificationEntity notificationEntity) {
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, default_notification_channel_id)
.setSmallIcon(R.drawable.ic_baseline_android_24)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification for " + notificationEntity.getType() + " with id " + notificationEntity.getId())
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.setVibrate(new long[]{0, 1000, 1000})
.setChannelId(NOTIFICATION_CHANNEL_ID);
return notification.build();
}然后是scheduleNotification:
私有无效scheduleNotification(int notificationId,通知通知,长延迟){
int requestID = (int) System.currentTimeMillis();
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, notificationId);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent
.getBroadcast(this, requestID, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, delay, pendingIntent);
}这是我的NotificationPublisher课程:
公共类NotificationPublisher扩展BroadcastReceiver {
public static final String NOTIFICATION = "myNotification";
public static final String NOTIFICATION_ID = "myNotification-id";
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel(ClientHomepage.NOTIFICATION_CHANNEL_ID, "Channel", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("This is just notification");
notificationManager.createNotificationChannel(channel);
}
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
}}
我的问题是所有的通知都是同时发出的,而不是一个接一个的。我尝试过更改requestID,但是它没有帮助,我也尝试过更改PendingIntent标志,这也没有帮助。希望你有什么建议。
发布于 2022-01-07 14:36:09
根据我的理解,您在这一行代码中添加了错误的时间。
scheduleNotification(notificationEntities.get(i).getId(),通知,(长)定时器);
而不是(long)timer,您必须添加System.currentTimeinMillis()+timer,它将解决您的问题
https://stackoverflow.com/questions/70622061
复制相似问题