-3

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.

2 Answers2

0

try this to schedule your notification

Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 6);
            calendar.set(Calendar.MINUTE, 0);
            int interval = 1000 * 60 * 60 * 24;

          Intent myIntent = new Intent(yourActivity.this, ScheduleNotification .class);
          pendingIntent = PendingIntent.getBroadcast(UserDashBoardActivity.this, 0, myIntent,0);
          /* Repeating on every 24 hours interval */
          AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
          alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);

for more information Scheduling Repeating Alarms

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

Check the value of futureInMillis

el2e10
  • 1,518
  • 22
  • 22
  • I am checking you can see it in the code. I am showing the value as a toast message . it gives the right value in millis and minutes both. – Mohammad Naim Dahee Jun 10 '17 at 06:10
  • If your app is giving notification right now can mean that you have specified a trigger value which has already past. – el2e10 Jun 10 '17 at 06:15
  • No time is not passed. With every test I am passing a new time in future. And while I am chechking futureInMilllis it gives a time in future. – Mohammad Naim Dahee Jun 10 '17 at 06:18