我正在制作一个应用程序 Medicine Reminder,它应该根据数据库中保存的日期和时间提醒药物消耗。提醒应以通知的形式进行。假设我有一种药物应该服用两次(例如在晚上 9:50 和晚上 9:55 进行快速测试)。通过单击按钮触发通知。
我的问题 - 只会显示第一个通知,而其他通知不会。
这是我的代码:
public void makeNotifications(Date now, String[] medicineNames, Date[]
takingTime) {
WorkContinuation continuation = null;
for (int i = 1; i < medicineNames.length + 1; i++) {
Data.Builder dataBuilder = new Data.Builder()
.putString(MEDICINE_NAME_KEY, medicineNames[i - 1])
.putLong(MEDICINE_TAKING_TIME_KEY, takingTime[i - 1].getTime());
OneTimeWorkRequest.Builder requestBuilder =
new OneTimeWorkRequest.Builder(NotificationWorker.class)
.setInputData(dataBuilder.build());
if (i == 1) {
long diff = Math.abs(takingTime[i - 1].getTime() - now.getTime());
requestBuilder.setInitialDelay(diff, TimeUnit.MILLISECONDS);
continuation = mWorkManager.beginUniqueWork(
MEDICINE_NOTIFICATION_KEY,
ExistingWorkPolicy.KEEP,
requestBuilder.build());
} else {
long diff = Math.abs(takingTime[i - 1].getTime() - now.getTime());
requestBuilder.setInitialDelay(diff, TimeUnit.MILLISECONDS);
continuation.then(requestBuilder.build());
}
}
continuation.enqueue();
}
您有什么建议/解释/解决方案吗?