I want to push a notification every 12 hours at fixed times (lets say for an example, 9am and 9pm, every day). This is my current doWork() code:
@NonNull
@Override
public Result doWork() {
database.child("business_users").child(currentUserID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
user = snapshot.getValue(BusinessUser.class);
if(user.isNotifications()==true)
{
if(user.getRatingsCount() > user.getLastKnownRC())
{
theDifference = user.getRatingsCount() - user.getLastKnownRC();
notification();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
Log.i("BackgroundWork" , "notif sent");
return Result.success();
}
`
and this is the work creation code:
public void FirstTimeWork ()
{
PeriodicWorkRequest myWorkRequest =
new PeriodicWorkRequest.Builder(BackgroundWork.class, 12, TimeUnit.HOURS)
.setInitialDelay(1, TimeUnit.DAYS)
.addTag("notif")
.build();
}
I saw some people doing it with calendar but I don't understand how it works.