Notification Scheduler not working

By Mohammad Naim Dahee

I am trying to schedule a notification in Future in android. but It gives me the notification just right now.

scheduleNotification Method.

private void scheduleNotification(Notification notification, long delay) {
    Intent notificationIntent = new Intent(getContext(), NotificationReceiver.class);
    notificationIntent.putExtra(Constants.TimeTableTasksNotification.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(Constants.TimeTableTasksNotification.NOTIFICATION, notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    long futureInMillis = delay - (new Date()).getTime();
    long minutes = TimeUnit.MILLISECONDS.toMinutes(futureInMillis);
    Toast.makeText(context, "" + futureInMillis, Toast.LENGTH_SHORT).show();
    Toast.makeText(context, "" + minutes, Toast.LENGTH_SHORT).show();
    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}

getNotification Method.

private Notification getNotification(String message) {
    Notification.Builder builder = new Notification.Builder(getContext());
    builder.setContentTitle("Time To do the Task.");
    builder.setContentText(message);
    builder.setSmallIcon(R.drawable.logo);
    builder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    builder.setLights(Color.BLUE, 3000, 3000);
    return builder.build();
}

I Have the Date in this Format as String Date.

String inputDate = "09/05/2017 10:20";

Using The below code I am getting the time in Milliseconds .

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.ENGLISH);
        try {
            Date date = simpleDateFormat.parse(inputDate);
            long milliSeconds = date.getTime();
            scheduleNotification(getNotification("Notification Title."), milliSeconds);
        } catch (ParseException e) {
            Log.d("DialogAddScheduleTask", e.getMessage());
            e.printStackTrace();
        }

and Finally Here is my NotificationReceiver Class

    public class NotificationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = intent.getParcelableExtra(Constants.TimeTableTasksNotification.NOTIFICATION);
        int id = intent.getIntExtra(Constants.TimeTableTasksNotification.NOTIFICATION_ID, 0);
        notificationManager.notify(id, notification);
    }
}

All things are also set right in Manifest. I added permissions, and also I did registered my receiver.

But can not figure out why It does give me the notification just in time not in the future.

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273