diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/DateMatch.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/DateMatch.java index 73be127af..064cc0e15 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/DateMatch.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/DateMatch.java @@ -73,6 +73,8 @@ public void setMinute(Integer minute) { private Calendar buildCalendar(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); + cal.set(Calendar.MILLISECOND, 0); + cal.set(Calendar.SECOND, 0); return cal; } @@ -92,46 +94,46 @@ public long nextTrigger(Date date) { * Postpone trigger if first schedule matches the past */ private long postponeTriggerIfNeeded(Calendar current, Calendar next) { - int currentYear = current.get(Calendar.YEAR); - if (matchesUnit(Calendar.YEAR, current, next)) { - next.set(Calendar.YEAR, currentYear + 1); - this.unit = Calendar.YEAR; - } else if (matchesUnit(Calendar.MONTH, current, next)) { - next.set(Calendar.YEAR, currentYear + 1); - this.unit = Calendar.MONTH; - } else if (matchesUnit(Calendar.DAY_OF_MONTH, current, next)) { - next.set(Calendar.MONTH, current.get(Calendar.MONTH) + 1); - this.unit = Calendar.DAY_OF_MONTH; - } else if (matchesUnit(Calendar.HOUR_OF_DAY, current, next)) { - next.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH) + 1); - this.unit = Calendar.DAY_OF_MONTH; - } else if (matchesUnit(Calendar.MINUTE, current, next)) { - next.set(Calendar.HOUR_OF_DAY, current.get(Calendar.HOUR_OF_DAY) + 1); - this.unit = Calendar.MINUTE; + if (next.getTimeInMillis() <= current.getTimeInMillis() && unit != -1) { + Integer incrementUnit = -1; + if (unit == Calendar.YEAR || unit == Calendar.MONTH) { + incrementUnit = Calendar.YEAR; + } else if (unit == Calendar.DAY_OF_MONTH) { + incrementUnit = Calendar.MONTH; + } else if (unit == Calendar.HOUR_OF_DAY) { + incrementUnit = Calendar.DAY_OF_MONTH; + } else if (unit == Calendar.MINUTE) { + incrementUnit = Calendar.HOUR_OF_DAY; + } + + if (incrementUnit != -1) { + next.set(incrementUnit, next.get(incrementUnit) + 1); + } } return next.getTimeInMillis(); } - private boolean matchesUnit(Integer unit, Calendar current, Calendar next) { - return next.get(unit) < current.get(unit); - } - private Calendar buildNextTriggerTime(Date date) { Calendar next = buildCalendar(date); if (year != null) { next.set(Calendar.YEAR, year); + if (unit == -1) unit = Calendar.YEAR; } if (month != null) { next.set(Calendar.MONTH, month); + if (unit == -1) unit = Calendar.MONTH; } if (day != null) { next.set(Calendar.DAY_OF_MONTH, day); + if (unit == -1) unit = Calendar.DAY_OF_MONTH; } if (hour != null) { next.set(Calendar.HOUR_OF_DAY, hour); + if (unit == -1) unit = Calendar.HOUR_OF_DAY; } if (minute != null) { next.set(Calendar.MINUTE, minute); + if (unit == -1) unit = Calendar.MINUTE; } return next; } diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java index 7d5e461ae..1f55d0ec3 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java @@ -32,6 +32,7 @@ import org.json.JSONException; import org.json.JSONObject; +import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @@ -338,9 +339,12 @@ private void triggerScheduledNotification(Notification notification, LocalNotifi // Cron like scheduler DateMatch on = schedule.getOn(); if (on != null) { + long trigger = on.nextTrigger(new Date()); notificationIntent.putExtra(TimedNotificationPublisher.CRON_KEY, on.toMatchString()); pendingIntent = PendingIntent.getBroadcast(context, request.getId(), notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); - alarmManager.setExact(AlarmManager.RTC, on.nextTrigger(new Date()), pendingIntent); + alarmManager.setExact(AlarmManager.RTC, trigger, pendingIntent); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + Logger.debug(Logger.tags("LN"), "notification " + request.getId() + " will next fire at " + sdf.format(new Date(trigger))); } } diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/TimedNotificationPublisher.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/TimedNotificationPublisher.java index 260f544d7..835126260 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/TimedNotificationPublisher.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/TimedNotificationPublisher.java @@ -9,6 +9,7 @@ import android.content.Intent; import com.getcapacitor.Logger; +import java.text.SimpleDateFormat; import java.util.Date; /** @@ -42,8 +43,10 @@ private void rescheduleNotificationIfNeeded(Context context, Intent intent, int long trigger = date.nextTrigger(new Date()); Intent clone = (Intent) intent.clone(); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, clone, PendingIntent.FLAG_CANCEL_CURRENT); - alarmManager.set(AlarmManager.RTC, trigger, pendingIntent); + alarmManager.setExact(AlarmManager.RTC, trigger, pendingIntent); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + Logger.debug(Logger.tags("LN"), "notification " + id + " will next fire at " + sdf.format(new Date(trigger))); } } - + } \ No newline at end of file