This is the more elaborate response.
As mentioned by the others, yes work manager doesn't work properly. It may do the work with a delay. some times 15 minutes and in some cases 10 to 12 hours. There is no workaround for that.
But, if you want to do a task on a specific hour and date, there is a way to do that. you should use an alarm manager, with a broadcast receiver and a foreground service.
So this is the IntentService class:
public class NotificationService extends IntentService {
//In order to send notification when the app is close
//we use a foreground service, background service doesn't do the work.
public NotificationService() {
super("NotificationService");
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
sendNotification(intent);
}
private void sendNotification(Intent intent){
Context context = NotificationService.this;
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification.Builder builder = new Notification.Builder(context)
.setTicker("Notification")
.setContentTitle("Important Message")
.setContentText("This is an example of a push notification using a Navigation Manager")
.setSmallIcon(R.drawable.ic_add)
.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
String channelId = "Your_channel_id";
NotificationChannel channel = new NotificationChannel(
channelId,
"Reminder to remind to review your notes",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Hello Dear friends"); //this is to test what this is
notificationManager.createNotificationChannel(channel);
builder.setChannelId(channelId);
}
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}}
this is the Receiver class:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("SHIT","YESSSS");
Intent service = new Intent(context, NotificationService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(service);
} else {
context.startService(service);
}
}}
and this is where I set the the Alarm on a specific date:
private void setNotificationAlarm(){
//Alarm Manager
Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR_OF_DAY,16);//set the alarm time
time.set(Calendar.MINUTE, 50);
time.set(Calendar.SECOND,0);
AlarmManager am =(
AlarmManager)getContext().getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getActivity(), AlarmReceiver.class);
i.setAction("android.intent.action.NOTIFY");
PendingIntent pi = PendingIntent.getBroadcast(getContext(), 0, i,
PendingIntent.FLAG_ONE_SHOT);
// am.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), 1000 * 60 * 10,
pi); // Millisec * Second * Minute
if (Build.VERSION.SDK_INT >= 23){
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,time.getTimeInMillis(),pi);
}
else{
am.set(AlarmManager.RTC_WAKEUP,time.getTimeInMillis(),pi);
}
}
And of course you have to declare your Service and BroadcastRecevier in the manifest.
This works like a charm.