Java – How to meet the scheduled time list in the alert manager to push notifications

How to meet the scheduled time list in the alert manager to push notifications… here is a solution to the problem.

How to meet the scheduled time list in the alert manager to push notifications

I’m trying to retrieve a list of times stored in Sqlite (it has hours and minutes) into the alert manager to perform reminders with notifications. My approach is to loop all the scheduled times stored in Sqlite into the alarm manager, execute notifications based on the stored list of times, but notifications don’t beep.

But when I specify a time (hour and minute) it works. Here’s a valid code example, but I don’t want this:

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 45);
Intent myIntent = new Intent(ListRemainder.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(ListRemainder.this, 0, myIntent, 0);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

But it doesn’t work when it comes to looping through several schedules, here’s the method I want, here is the code example, what am I missing?

//listOfTime is the one which contains the list of stored scheduled time in Sqlite.
for (int i = 0; i < listOfTime.size(); i++) {
    int hour = Integer.parseInt(listOfTime.get(i).toString().substring(0, 2));
    int minute = Integer.parseInt(listOfTime.get(i).toString().substring(3));
    System.out.print("Hour : " + hour + " Minute : " + minute);
    Log.d("MyActivity", "Alarm On");
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    Intent myIntent = new Intent(ListRemainder.this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(ListRemainder.this, 0, myIntent, 0);
    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}

Best Solution

The prototype of getBroadcast is as follows:

getBroadcast(Context context, int requestCode, Intent intent, int flags)

Setting a unique “requestCode” for each PendingIntent should allow multiple alerts to be scheduled:

PendingIntent.getBroadcast(context, uniqueValue, intent, PendingIntent.FLAG_UPDATE_CURRENT);

To ensure that users wake up for critical alerts:

void setAlarmClock (AlarmManager.AlarmClockInfo info, 
            PendingIntent operation)

From the documentation:
“Schedule an alarm that represents an alarm clock that will be used to notify the user when it goes off. The expectation is that when this alarm is triggered, the application will further wake up the device to tell the user about the alarm – turn to play a sound, vibration, etc. on the screen. Therefore, the information provided here is also often used to inform users of upcoming alerts, if appropriate.

Due to the nature of this alarm, similar to setExactAndAllowWhileIdle(int, long, PendingIntent), these alarms will be allowed to fire even if the system is in a low-power idle (also known as dozing) mode. “

To summarize:

  • Use setAlarmClock to wake the device to present information to the user.
  • Use setExactAndAllowWhileIdle to perform work in the background that must be done during hibernate. However, there can be a delay of approximately 1 minute to 15 minutes for these alerts to be triggered.
  • Use FCM to wake up devices in real time from remote applications. In practice, many applications should use FCM instead of setExactAndAllowWhileIdle, if possible.
  • If precise synchronization during dozing is not important, use setExact. Also, using setExact instead of setAlarmClock or setExactAndAllowWhileIdle…

Lutaaya’s final solution:

AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(),pendingIntent), pendingIntent);

Related Problems and Solutions