user3571631 user3571631 - 16 days ago 5
Android Question

scheduling notification android

Hello I am trying to generate notification at a certain time in Android. I am able to generate the notification, but it generates immediately while I want it to generate at a later time. Can anyone suggest where I went wrong.

calendar.set(Calendar.MONTH, 11);

calendar.set(Calendar.YEAR, 2016);

calendar.set(Calendar.DAY_OF_MONTH, 20);

calendar.set(Calendar.HOUR_OF_DAY, 2);

calendar.set(Calendar.MINUTE, 58);

calendar.set(Calendar.SECOND, 0);

calendar.set(Calendar.AM_PM, Calendar.PM);

long timeinmill = calendar.getTime().getTime() - System.currentTimeMillis();

Intent myIntent = new Intent(MakeAppointment.this, MyReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(MakeAppointment.this, 0, myIntent,0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, timeinmill, pendingIntent);

Answer

timeinmill = calendar.getTime().getTime(). Do not substract current time. See https://developer.android.com/reference/android/app/AlarmManager.html#RTC