I need to add push notifications in my Android application. The notification must be shown every day at certain time (for example at 1 PM). To do this i am using an AlarmManager. I am registering an alarm, when the app is starting for the first time. But i got a problem. If i am installing my application onto device, for example at 1.10 PM, then the alarm is running right after my app is started. But this is wrong, because I need this alarm to run in the next day, not in the current day. Can anyone help me and tell how to set daily alarm, that must start working on the day about from current day.
This is my code, which i am using for now
private void registerAMAlarmManger(){
mAMAlarmIntent = new Intent(this, AMAlarmReceiver.class);
mAMPendingIntent = PendingIntent.getBroadcast(this, 0, mAMAlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
SharedPreferences sPrefs = getSharedPreferences(Constants.PREFERENCES_NAME, Context.MODE_PRIVATE);
int amTime = sPrefs.getInt(Constants.MORNING_TIME, 9);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, amTime);
calendar.set(Calendar.MINUTE, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, mAMPendingIntent);
}
if the time has passed for the current date it executes the code of performing alarm .This need to be handled . I do it by following way
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, mytesthourofday);
calendar.set(Calendar.MINUTE,
Integer.parseInt(min_am_pm[0]));
// calendar.set(Calendar.AM_PM, am_pm_integer);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 24 * 60 * 60,
pendingIntent);