So I am developing a simple app that takes in dates and names the user inputs and then reminds you when your friends' birthdays are. The one issue I cannot solve is how to check daily for which birthdays are today.
I have tried WorkManager and AlarmManager and NEITHER OF THEM will execute the code needed. All that needs to be done is execute a method in a java class, and the notifications will be built and sent.
Here is the code that sets the reminder. What's in the reminder is irrelevant, all that needs to be done is to run RemindWorker once a day
public void setReminder() {
Calendar currentDate = Calendar.getInstance();
Calendar dueDate = Calendar.getInstance();
dueDate.set(Calendar.HOUR_OF_DAY, 0);
dueDate.set(Calendar.MINUTE, 0);
dueDate.set(Calendar.SECOND, 0);
if(currentDate.after(dueDate)) {
dueDate.add(Calendar.Hour, 24);
}
long timeDiff = dueDate.getTimeInMillis() - currentDate.getTimeInMillis();
Log.d("ReminderTests", "Time until next alarm//alarm set: " + timeDiff);
OneTimeWorkRequest timedRemindRequest = new OneTimeWorkRequest.Builder(RemindWorker.class)
.addTag("CALENDAR_BASED_PERIODIC")
.setInitialDelay(timeDiff, TimeUnit.MILLISECONDS)
.build();
WorkManager.getInstance().enqueueUniqueWork("REMIND_WORK", ExistingWorkPolicy.REPLACE, timedRemindRequest);
}
}
This is first called when the app is opened. at the end of the RemindWorkr class, setReminder(); is called again, starting the loop.
When the phone is on and plugged into the computer (as well as the app open), the RemindWorker will fire flawlessly and remind the user. AS SOON as the app is exited (not even cleared, just I opened another app) nothing happens at all. No reminders will fire.